tp6+FFMpeg實現視頻水印及大小剪切

以前項目中用到上傳視頻時,須要獲取視頻大小,視頻第一幀及給視頻添加水印問題,找了不少資料,結合在項目中的實際運用,在此記錄一下。php

1.FFMpeg安裝

  1. 下載擴展git

    網上能夠找到,我這裏放到百度雲盤 連接:百度雲盤下載 提取碼:t5zzgithub

  2. composer 安裝php-ffmpegcomposer

    github上安裝 php-ffmpegide

注意:以上不是二選一,是二者都須要哦ui

# composer 安裝
composer require php-ffmpeg/php-ffmpeg

2.獲取上傳文件大小和時長

# ffmpeg和ffprobe爲上面下載的擴展在你項目中的路徑
$file = '你上傳的視頻路徑';
$ffprobe = \FFMpeg\FFProbe::create([
     'ffmpeg.binaries'  => '/usr/bin/ffmpeg',	# 你本身安裝的位置
     'ffprobe.binaries' => '/usr/bin/ffprobe'	# 你本身安裝的位置
 ]);
 # 獲取視頻時長
 $video_time = $ffprobe->format($file)->get('duration',0)/60;
 # 獲取視頻大小(單位可能不許確)
 $video_size = $ffprobe->format($file)->get('size',0)/1000000;

3.視頻提取第幾幀作縮略圖

$ffmpeg = \FFMpeg\FFMpeg::create([
    'ffmpeg.binaries'  => '/usr/bin/ffmpeg',	# 你本身安裝的位置
    'ffprobe.binaries' => '/usr/bin/ffprobe'	# 你本身安裝的位置
]);
$video = $ffmpeg->open($url_video);
$frame = $video->frame(\FFMpeg\Coordinate\TimeCode::fromSeconds(1));    # 提取第1秒的圖像
$url_image = "static/".md5(uniqid().rand(1,999)).'.png';	# 生成圖片路徑
$frame->save($url_image);	# 保存圖片
dump($url_image);die;	# 獲取視頻截取的圖片

4.視頻添加水印

$ffmpeg = \FFMpeg\FFMpeg::create([
    'ffmpeg.binaries'  => '/usr/bin/ffmpeg',	# 你本身安裝的位置
    'ffprobe.binaries' => '/usr/bin/ffprobe'	# 你本身安裝的位置
]);
# $video_url 爲視頻路徑
$video = $ffmpeg->open($video_url);
$watermarkPath = 'static/images/sy.gif';	# 水印圖片
$sy_url_video = "static/".md5(uniqid().rand(1,999)).'.mp4';	# 新生成的待水印視頻名稱
$video->filters()->watermark($watermarkPath, array(
        'position' => 'relative',# 視頻定位
        'top' => -20,
        'left' => -5,
));
$video->save(new \FFMpeg\Format\Video\X264('libfdk_aac'), $sy_url_video);	# 保存視頻
dump($sy_url_video);die;	# 得出帶水印的視頻

到此,完整的php操做ffmpeg就完成了,今天先這裏,後期有機會,補全其餘功能。url

相關文章
相關標籤/搜索