Laravel 下使用 FFmpeg 处理多媒体文件 (Ubuntu 16.04 LTS)
安装 FFmpeg 命令
下面是 Ubuntu 16 LTS 的安装方法
sudo add-apt-repository ppa:djcj/hybrid
sudo apt-get update
sudo apt-get install ffmpeg
安装成功后测试:
$ ffmpeg -version
PHP-FFMpeg
PHP-FFMpeg 是 FFMpeg 和 FFProbe 命令的 PHP 面对对象 Wrapper.
安装
项目根目录下:
$ composer require php-ffmpeg/php-ffmpeg
使用
创建 FFMpeg\FFMpeg
对象:
$ffmpeg = \FFMpeg\FFMpeg::create(array(
'ffmpeg.binaries' => '/usr/bin/ffmpeg',
'ffprobe.binaries' => '/usr/bin/ffprobe',
'timeout' => 3600, // The timeout for the underlying process
'ffmpeg.threads' => 12, // The number of threads that FFMpeg should use
));
原文档
$ffmpeg = FFMpeg\FFMpeg::create(array(
'ffmpeg.binaries' => '/opt/local/ffmpeg/bin/ffmpeg',
'ffprobe.binaries' => '/opt/local/ffmpeg/bin/ffprobe',
'timeout' => 3600, // The timeout for the underlying process
'ffmpeg.threads' => 12, // The number of threads that FFMpeg should use
), $logger);
根据自己的 ffmpeg 与 ffprobe 位置创建 $ffmpeg
我的项目中, 是将视频截图
// $ffmpeg 获取 $url 的视频
$video = $ffmpeg->open($url);
// 在视频 1 秒的地方截图
$frame = $video->frame(\FFMpeg\Coordinate\TimeCode::fromSeconds(1));
// 生成$filename
$filename = $id . '_' . time() . '_' . str_random(10) . '.' . 'jpg';
// 图片保存,并且移动到 /storage/images/
$frame->save($filename);
\File::move(public_path().'/'.$filename, public_path()."/storage/images/".$filename);
总结
参考 Ubuntu 14LTS Laravel 下使用 FFMpeg 处理多媒体文件:
文档:
https://github.com/PHP-FFMpeg/PHP-FFMpeg
Laravel 5.5 的使用中, 自己使用 FFMpeg 与文档有些出入,所以和大家一起分享学习的问题。
推荐文章: