Search code examples
phpffmpegffmpeg-php

Passing negative values for dimensions with PHP FFMpeg


I'm working on a legacy project that uses FFMpeg to process videos on upload. The code I inherited uses escapeshellarg() to run FFMpeg from the command line, which is problematic because it doesn't provide any means of keeping track of progress, so I'm rewriting it using PHP-FFMpeg.

I'm a bit stumped at the point of resizing the videos. The aspect ratio needs to remain the same, so the dimensions passed to the command line for the scale were -vf scale=-1:720, and my interpretation of the documents is that you need to pass the dimensions through as follows:

$dimension = new FFMpeg\Coordinate\Dimension('-1', '720');
$video->filters()->resize($dimension)->synchronize();

However, the constructor of the Dimension class only accepts integers, and it's unclear how I'd go about specifying a negative dimension here. Google hasn't been very useful, nor have the existing issues on the project.

Any idea how I could pass through the required dimensions? I don't often use FFMpeg so I'm not terribly familiar with it.


Solution

  • Figured this out in the end. Turns out the second argument to resize() is the mode, which determines how to scale the two dimensions. If you set this to scale the width to fit the height, it should work as expected:

    $dimension = new FFMpeg\Coordinate\Dimension('-1', '720');
    $mode = FFMpeg\Filters\Video\ResizeFilter::RESIZEMODE_SCALE_HEIGHT;
    $video->filters()->resize($dimension, $mode)->synchronize();