Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

popov's avatar
Level 1

Resize video using FFMpeg, respect aspect ratio

Hi guys, I hope you are well.

For the past a couple of days I'm exploring FFMpeg with "pbmedia/laravel-ffmpeg" package for Laravel.

I struggle to get it to resize every video file to be maximum 1200px width and height to be set automatically in order to keep the Aspect ration of the video.

Currently I have:

$format = (new X264)->setKiloBitrate(1000);
$format->setAdditionalParameters([
	 '-crf',
	'23',
 	'-preset',
	'faster',
	'-movflags',
	'+faststart'
]);

FFMpeg::open($file)
	->addFilter(function (VideoFilters $filters) {
		$filters->resize(new \FFMpeg\Coordinate\Dimension(1280, 720));
	})
	->export()
	->inFormat($format)
	->toDisk('s3')
	->save($filePath);

Initially I thought FFMpeg will respect the ratio automatically when I tested with a couple of videos, but when when I took a video myself using iPhone in Portrait position with original dimensions of "2160 × 3840" the result was a video with the dimensions of "1280 × 720" which are set, but the video was squashed a lot which is not the result I'm looking for.

Ideally i would like the videos to be scaled down to a maximum width of 1200px setting up the height automatically

I was trying:

"$filters->custom('scale=1200:-1');" 

Error when using custom:

"ffmpeg failed to execute command '/opt/homebrew/bin/ffmpeg' '-y' '-i' '/private/var/tmp/phpkn21ak' '-threads' '12' '-vcodec' 'libx264' '-acodec' 'aac' '-b:v' '1000k' '-refs' '6' '-coder' '1' '-sc_threshold' '40' '-flags' '+loop' '-me_range' '16' '-subq' '7' '-i_qfactor' '0.71' '-qcomp' '0.6' '-qdiff' '4' '-trellis' '1' '-b:a' '128k' '-crf' '23' '-preset' 'faster' '-movflags' '+faststart' '-vf' '[in]scale=1200:-1[out]' '-pass' '1' '-passlogfile' '/var/tmp/ffmpeg-passes676078d05dc32/pass-676078d05dd0a' '/var/tmp/1758857c47c04467/videos/14/qUkHIQ6T4sM0n0Ni9pdMYexFo1uUT2i4Csq9ie1H.mp4':"

and also

"$filters->scale(new Dimension(1200, null));"

And the error when using scale:

"Call to undefined method FFMpeg\Filters\Video\VideoFilters::scale() "

Apologise for the long post, I appreciate your support.

1 like
2 replies
popov's avatar
Level 1

Got it, like most of the times one number can make a massive difference.

Here is how I get it to work for future references:

$filters->custom('scale=1200:-2');
jj15's avatar

EDIT: I just saw your reply that you solved it. There's more than one way to do things so I'll leave mine for reference as well. 😁

I've also been experimenting with the same package for a couple of weeks, it for sure has a learning curve just like FFmpeg itself (but it's also a wrapper on another pure PHP package, which can make things a bit more confusing).

The built-in resize() filter requires both width and height to be passed to it. You can pass in a third parameter, $mode, which will control how the resizing is done.

By default, the mode is set to "fit", which can cause distortion. You can try another mode like "height" or one of the other available class constants which will preserve the aspect ratio.

1 like

Please or to participate in this conversation.