ffmpeg安装及使用
准备
1. ffmpeg: https://www.ffmpeg.org/releases/ffmpeg-snapshot.tar.bz2
2. x264:git clone git://git.videolan.org/x264.git
3. yasm: http://www.tortall.net/projects/yasm/releases/yasm-1.3.0.tar.gz
4. nasm:
5. freetype: http://www.linuxfromscratch.org/blfs/view/svn/general/freetype2.html
6. fribidi: http://www.linuxfromscratch.org/blfs/view/svn/general/fribidi.html
7. fontconfig: http://www.linuxfromscratch.org/blfs/view/svn/general/fontconfig.html
8. harfbuzz: https://www.freedesktop.org/software/harfbuzz/release/harfbuzz-1.2.7.tar.bz2
cd /opt/src , cd /usr/local
分别在 src、local 创建以上四个文件夹
mkdir ffmpeg x264 yasm nasm
一、 Linux
下安装
cd /opt/src/yasm
wget http://www.tortall.net/projects/yasm/releases/yasm-1.3.0.tar.gz
tar -jxvf yasm-1.3.0.tar.gz
cd yasm-1.3.0
./configure --prefix=/usr/local/yasm
make
make install
#配置环境变量
vim /etc/profile
export YASM=/usr/local/yasm
export PATH=${YASM}/bin:${PATH}
source /etc/profile
#查看版本 测试是否安装成功
yasm --version
#查看 nasm 是否安装
#nasm --version
#下载 最新版 nasm.bz2
cd /opt/src/nasm
tar -jxvf 文件名
cd nasm-2.13
./configure --prefix=/usr/local/nasm
make
make install
#配置环境变量
export NASM=/usr/local/nasm
export PATH=${NASM}/bin:${PATH}
source /etc/profile
# 下载 x264
cd /opt/src/x264
tar -jxvf 文件名
cd 文件名
./configure --prefix=/usr/local/x264 --enable-shared
make
make install
# 配置环境变量
export PKG_CONFIG_PATH=/usr/local/x264/lib/pkgconfig
export PATH=${PKG_CONFIG_PATH}/bin:${PATH}
source /etc/profile
tar xvf *
cd harfbuzz
./configure --prefix=/usr/local/harfbuzz
make
make install
wget url
tar xvf freetype
cd freetype
./configure --prefix=/usr/local/freetype --disable-static
make
make install
wget url
tar xvf fribidi
cd fribidi
./configure --prefix=/usr/local/fribidi
make
make install
wget url
tar xvf fontconfig
cd fontconfig
./configure --prefix=/usr/local/fontconfig
make
make install
vim /etc/profile
export PKG_CONFIG_PATH=/usr/local/x264/lib/pkgconfig:/usr/local/freetype/lib/pkgconfig:/usr/local/harfbuzz/lib/pkgconfig:/usr/local/fribidi/lib/pkgconfig:/usr/local/libass/lib/pkgconfig
export PATH=${PKG_CONFIG_PATH}/bin:${PATH}
#生效
source /etc/profile
vim /etc/ld.so.conf
#添加一下内容
/usr/local/ffmpeg/lib
/usr/local/x264/lib
/usr/local/freetype/lib
/usr/local/firbidi/lib
/usr/local/harfbuzz/lib
/usr/local/libass/lib
#生效
ldconfig
tar -jxvf ffmpeg-4.1
cd ffmpeg
./configure --prefix=/usr/local/ffmpeg --enable-libx264 --enable-gpl --enable-libass --enable-fontconfig
make
make install
#配置环境变量
export FFMPEG=/usr/local/ffmpeg
export PATH=${FFMPEG}/bin:${PATH}
source /etc/profile
ffmpeg --version
安装好 x264
vim /etc/ld.so.conf
在该文件夹中添加
/usr/local/ffmpeg/lib
/usr/local/x264/lib
#配置生效
ldconfig
PHP
安装扩展包
composer require php-ffmpeg/php-ffmpeg
composer 阿里云镜像源报错则在 composer.json中加入
"php-ffmpeg/php-ffmpeg": "^0.18.0"
代码示例
$this->ffmpeg = FFMpeg::create(array(
'ffmpeg.binaries' => '/usr/local/ffmpeg/bin/ffmpeg',
'ffprobe.binaries' => '/usr/local/ffmpeg/bin/ffprobe',
'timeout' => 3600, // The timeout for the underlying process
'ffmpeg.threads' => 12, // The number of threads that FFMpeg should use
));
$video = $this->ffmpeg->open($url_1);
$frame = $video->frame(TimeCode::fromSeconds(2));
$frame->save(public_path().'/upload/image.jpg');
$clip = $video->clip(TimeCode::fromSeconds(1),TimeCode::fromSeconds(15));
$clip->save(new X264(), public_path().'/upload/video.mp4');
$new_file_name = date('YmdHis').'.mp4';
$newFile = public_path() . '/upload/'.$new_file_name.'';
$video->concat(array($url_1,$url_2,$url_3))->saveFromSameCodecs($newFile,false);
ps:
如果视频地址为url,使用上述方法则会报错:Protocol 'http' not on whitelist 'file,crypto'!
解决视频地址为url:
$ffmpeg_exec = '/usr/local/ffmpeg/bin/ffmpeg -f concat -safe 0 -protocol_whitelist "file,http,https,rtp,udp,tcp,tls" -i '.$filelist.' -c copy '.$newFile.'';
exec($ffmpeg_exec,$output,$result);
ps:
$filelist.txt 文件内容格式为:
file 'http://www.xxxx.com/x.mp4'
file 'http://www.xxxx.com/x1.mp4'
...
若遇到exec() 返回值为 127时,则需要查看文件执行权限
$video = $this->ffmpeg->open($url_1);
$new_file_name = date('YmdHis').'.mp4';
$newFile = public_path() . '/upload/'.$new_file_name.'';
$clip = $video->clip(TimeCode::fromSeconds(1),TimeCode::fromSeconds(15));
$clip->save(new X264(), $newFile);
$new_video_file = time().'_'.rand(100,999).'.mp4';
$new_video_path = public_path().'/upload/' . $new_video_file;
$video_srt_path = public_path().'/upload/test.srt';
$ffmpeg_exec = '/usr/local/ffmpeg/bin/ffmpeg -i '.$newFile.' -vf subtitles='.$video_srt_path.' -y '.$new_video_path.' ';
exec($ffmpeg_exec,$output,$code);
if ($code == 0){
echo 'success';
}