Search code examples
ffmpegmp4resolutionaspect-ratio

How to have ffmpeg calculate the width after modifying by set height ratio?


After finding the resolution was the source of all my webm to MP4 codec conversion errors on my Android. I came across another problem. To break down my question:

What all MP4 resolutions are supported on most devices? I know they go by p from certain height value for them to work. 720p /?X720 for example.

Is there an easier way to calculate the width based from the height change while keeping the ratio?

ffmpeg -i old.webm -t 00:00:03 -s 320x240 new.mp4

I currently use the -s setting perimeter from my testing example.


Solution

  • The standard ratio for widescreens is 16:9 and the typical list of resolutions used is

    256x144, 426x240, 640x360, 854x480, 960x540, 1024x576, 1280x720, 1920x1080, 2560x1440, 3840x2160
    

    If you directly use the scale filter, you can tell ffmpeg to maintain the aspect ratio:

    ffmpeg -i old.webm -t 00:00:03 -vf scale=-2:240 new.mp4
    

    In the command above, scale=-2:240 tells ffmpeg to scale to height 240, select a width so that aspect ratio is maintained, and then adjust that value so that the width and height is a multiple of 2 (required for H.264/MP4). If you did not care whether the value was even, you could just use -1.