php中使用 ffmpeg(部分代码)

PHP技术
470
0
0
2022-09-16

1.下载php-ffmpeg

composer require php-ffmpeg/php-ffmpeg

2.php开启的一些禁用函数: proc_open, exec, system

3.部分代码(以tp5.1为例)

//linux
$config = [
    'ffmpeg.binaries'  => '/usr/bin/ffmpeg',
    'ffprobe.binaries' =>  '/usr/bin/ffprobe',
    'timeout' => 3600,
    'ffmpeg.threads' => 0,
];
//window
$config = [
    'ffmpeg.binaries' => 'D:\ffmpeg\bin\ffmpeg.exe',//ffmpeg中bin目录下的ffmpeg路径 
    'ffprobe.binaries' => 'D:\ffmpeg\bin\ffprobe.exe',//ffmpeg中bin目录下的ffprobe路径 
    'timeout' => 3600,
    'ffmpeg.threads' => 0,
];
$ffprobe = FFProbe::create($config);
$ffmpeg = FFMpeg::create($config);
/* 提取第一帧为背景图 */
$img_dir = Env::get('root_path').'/public/static/video/11_res.jpg';//公共文件
$video->frame(\FFMpeg\Coordinate\TimeCode::fromSeconds(0))->save($img_dir);
copy($img_dir,$video_img);//复制文件
/* 视频转码 */
$video->save(new\FFMpeg\Format\Video\X264(),'F:\mywork\newbeita\public\static\video\ceshi.mp4');
/* 获取视频大小 */
$videoDuration =$ffprobe->format('/static/video/407efafdc8c5f0a1245a2eca227f315a.mp4')->get('duration');