Home > Net >  How to code Laravel FFMpeg video conversion for specific parameters?
How to code Laravel FFMpeg video conversion for specific parameters?

Time:10-07

I am using Laravel 9.x running php 8.1.x. I have some code that I use to convert videos that have a context type other than video/mp4 to a H264 format. The reason is to make a video format that is compatible with Apple (iOS), Android and most web browsers. This code has worked so far on every video we have used it on except for one. My current code is

        $ffmpeg = FFMpeg::create();
        $video = $ffmpeg->open($videoFilename);
        $format = new X264();
        $format->setAudioCodec("aac"); 
        $video->save($format, $newFilename);

So in an attempt to debug what else needs to be done I started manually running ffmpeg commands on my computer against a copy of the video in question. I have finally figured out what additional parameters are required to successfully convert that video so it plays on all devices. The manual command is as follows:

ffmpeg -i input.mov -pix_fmt yuv420p -b:v 4000k -c:v libx264 output.mp4

I am about 90% sure the parameters that dealt with the specifics of this video were the following:

-pix_fmt yuv420p -b:v 4000k

My problem now is I can not figure out what changes to my php code I need to make that would be the equivalent to those additional command line parameters. Also I am wondering as I am typing this question, is there a way for me to "auto-detect" when a video requires this additional parameters or not? (maybe that is a second post/question)

CodePudding user response:

Assuming you are using this package php-ffmpeg/php-ffmpeg

Have you tried the setAdditionalParameters method ?

It would give something like this :

$format->setAdditionalParameters(explode(' ', '-pix_fmt yuv420p -b:v 4000k'));

Integrated solution :

$ffmpeg = FFMpeg::create();
$video = $ffmpeg->open($videoFilename);
$format = new X264();
$format->setAudioCodec("aac");
$format->setAdditionalParameters(explode(' ', '-pix_fmt yuv420p -b:v 4000k'));
$video->save($format, $newFilename);
  • Related