#快速安装的脚本
wget https://raw.githubusercontent.com/q3aql/ffmpeg-install/master/ffmpeg-install
chmod a+x ffmpeg-install
./ffmpeg-install --install release
composer require pbmedia/laravel-ffmpeg
'providers' => [
Pbmedia\LaravelFFMpeg\FFMpegServiceProvider::class
];
'aliases' => [
'FFMpeg' => Pbmedia\LaravelFFMpeg\FFMpeg::class
];
php artisan vendor:publish --provider="ProtoneMedia\LaravelFFMpeg\Support\ServiceProvider"
FFMPEG_BINARIES=/usr/bin/ffmpeg
FFPROBE_BINARIES=/usr/bin/ffprobe
public function handle()
{
$format = new FFMpeg\Format\Video\X264('aac', 'libx264');
FFMpeg::fromDisk('public')
->open('video.mp4')
->exportForHLS()
->toDisk('public')
->addFormat($format)
->save('video.m3u8');
}
public function handle()
{
$lowBitrate = (new FFMpeg\Format\Video\X264('aac', 'libx264'))->setKiloBitrate(250);
$midBitrate = (new FFMpeg\Format\Video\X264('aac', 'libx264'))->setKiloBitrate(500);
$highBitrate = (new FFMpeg\Format\Video\X264('aac', 'libx264'))->setKiloBitrate(1000);
FFMpeg::fromDisk('public')
->open('video.mp4')
->exportForHLS()
->toDisk('public')
->addFormat($lowBitrate)
->addFormat($midBitrate)
->addFormat($highBitrate)
->save('video.m3u8');
}
需要Laravel 6.0以上的版本,我用的5.8,再加上作者暂时没有测试过,所以没有试用这个方案。
composer require "pbmedia/laravel-ffmpeg:v7.x-dev"
$format = new \FFMpeg\Format\Video\X264('libfaac', 'h264_cuvid');
$format->setInitialParameters(['-vsync', 0, '-hwaccel', 'cuvid']);
FFMpeg::fromDisk('videos')
->open('steve_howe.mp4')
->export()
->toDisk('converted_videos')
->inFormat($format)
->save('small_steve.mkv');
/**
* {@inheritDoc}
*/
public function getAvailableVideoCodecs()
{
return array('libx264', 'h264_cuvid'); //增加h264_cuvid
}
php-ffmpeg 0.15版本以下,修改代码增加commads的参数
/**
* Return base part of command.
*
* @return array
*/
protected function basePartOfCommand(){
if (evc('GPU_NVIDIA_SWITCH')) {//是否使用GPU转码
$commands = array('-y', '-vsync', 0, '-hwaccel', 'cuvid');
} else {
$commands = array('-y', '-i', $this->pathfile);
}
return $commands;
}
php-ffmpeg 0.15版本以上不需要修改直接使用setInitialParameters方法
$format->setInitialParameters(['-vsync', 0, '-hwaccel', 'cuvid']);
下面是相关代码
/**
* Return base part of command.
*
* @param FormatInterface $format
* @return array
*/
protected function basePartOfCommand(FormatInterface $format)
{
$commands = array('-y');
// If the user passed some initial parameters
if ($format instanceof VideoInterface) {
if (null !== $format->getInitialParameters()) {
foreach ($format->getInitialParameters() as $initialParameter) {
$commands[] = $initialParameter;
}
}
}
$commands[] = '-i';
$commands[] = $this->pathfile;
return $commands;
}