進入解壓後檔案的bin目錄下,可以看到相應的.exe檔案。将bin目錄添加到環境變量path中,注意以分号結尾。
驗證是否安裝成功:
運作cmd指令,在控制台輸入指令:ffmpeg -version,結果如下所示:
安裝成功!
項目在使用之前請安裝好 FFMpeg
使用composer快速安裝 > composer require php-ffmpeg/php-ffmpeg。
使用時請配置 ffmpeg,ffprobe 的執行檔案絕對路徑。我定義了一些測試用的視訊音頻檔案$path = [
'ffmpeg.binaries' => 'D:\ffmpeg\bin\ffmpeg.exe',
'ffprobe.binaries' => 'D:\ffmpeg\bin\ffprobe.exe',
];
$v1 = './static/common/mv.mp4';
$a1 = './static/common/a1.mp3';
$ffmpeg = FFMpeg::create($path);
//視訊提取第幾秒圖檔
$video = $ffmpeg->open($v1);
$frame = $video->frame(Coordinate\TimeCode::fromSeconds(2));//提取第幾秒的圖像
$frame->save('./uploads/uid2/image/image.jpg');
//視訊轉音頻
$video = $ffmpeg->open($v1);
$audio_format = new Audio\Mp3();
$video->save($audio_format, './uploads/uid2/audio/audio.mp3');
//給視訊加水印圖檔 (視訊的編碼 aac, libvo_aacenc, libfaac, libmp3lame)
libfdk_aac$video = $ffmpeg->open($v1);$watermarkPath = './static/common/water.jpg';
$absolute = ['x' => 50,'y' => 100];
$relative = [
'position' => 'relative',
'bottom' => 50,
'right' => 50];
$video->filters()->watermark($watermarkPath, $relative);
$video->save(new Video\X264('aac'), './uploads/uid2/video/1080_new.mp4');
//注意:x264 類預設使用 libfaac 為編碼器,但我安裝的是 aac,是以這裡要指定為 aac,不然會報錯
//可選項 relative(相對定位) / absolute(絕對,預設)
//若為 relative,有四個參數可選,top 、bottom、left、right ,分别對應四個方位
//若為 absolute,直接填寫 x 和 y 坐标即可
//視訊添加水印$video = $ffmpeg->open($v1);
$format = new Video\X264('aac');
$format->setKiloBitrate(1000)->setAudioChannels(2)->setAudioKiloBitrate(256);
$format->on('progress', function ($video, $format, $percentage)
{
echo "$percentage % 進度";
});
$video->save($format, './uploads/uid2/video/1080_new.avi');
//擷取視訊時長
$ffprobe = FFProbe::create($path);
$videoInfo = $ffprobe->format($v1);//也可以使用 get 擷取特定值,第二個參數為預設值(若該參數不存在将傳回此預設值)
$duration = $ffprobe->format($v1)->get('duration',100);
echo "
";print_r($videoInfo);
echo "
";
echo '視訊時長:'.$duration;
//音頻轉換格式
$audio = $ffmpeg->open($a1);
$format = new Audio\Flac();
$format->on('progress', function ($audio, $format, $percentage) {
echo "$percentage % 進度";
});
$format->setAudioChannels(2)->setAudioKiloBitrate(256);
$audio->save($format, './uploads/uid2/audio/1080_new.flac');
// 生成音頻波形
$audio = $ffmpeg->open($a1);
$waveform = $audio->waveform(640, 120, array('#00FF00'));
$waveform->save('./uploads/uid2/audio/image.png');//必須儲存為 png 格式複制代碼