Maintaining aspect ratio with FFmpeg

aspect-ratioffmpegvideo-processing

I need to convert a bunch of video files using FFmpeg. I run a Bash file that converts all the files nicely, however there is a problem if a file converted is not in 16:9 format.

As I am fixing the size of the screen to -s 720×400, if the aspect ratio of the original is 4:3, FFmpeg creates a 16:9 output file, screwing up the aspect ratio.

Is there a setting that allows setting an aspect ratio as the main parameter, with size being adjusted (for example, by fixing an X or Y dimension only)?

Best Answer

-vf "scale=640:-1"

works great until you will encounter error

[libx264 @ 0x2f08120] height not divisible by 2 (640x853)

So most generic approach is use filter expressions:

scale=640:trunc(ow/a/2)*2

It takes output width (ow), divides it by aspect ratio (a), divides by 2, truncates digits after decimal point and multiplies by 2. It guarantees that resulting height is divisible by 2.

Credits to ffmpeg trac

UPDATE

As comments pointed out simpler way would be to use -vf "scale=640:-2". Credits to @BradWerth for elegant solution

Related Topic