Maintaining aspect ratio with FFmpeg

-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


For example:

1920x1080 aspect ratio 16:9 => 640x480 aspect 4:3:

ffmpeg -y -i import.media -aspect 16:9 scale=640x360,pad=640:480:0:60:black output.media

aspect ratio 16:9 , size width 640pixel => height 360pixel:
With final output size 640x480, and pad 60pixel black image (top and bottom):

"-vf scale=640x360,pad=640:480:0:60:black"