Resize videos with different widths to a fixed height preserving aspect ratio with ffmpeg

17

5

I'd like to convert a lot of video files to flash video for our company's website. I have a requirement that all of the videos must be in 360p format, so their size would be Nx360.

FFMpeg uses -s argument to specify target resolution as WxH. I don't know Width, as it depends on source file aspect ratio. If source is 640x480, target will be 480x360. If source is 848x480, target will be 636x360.

Is there a way to do it with some switch of ffmpeg? That it will preserve aspect ratio and I'll only specify the height of target video?

I could easily solve it by making a program that will launch ffprobe to get source video size, calculate aspect ratio and then calculate a new width.

Axarydax

Posted 2010-10-19T13:07:45.260

Reputation: 1 127

Answers

15

You could try adding this video filter:

-vf "scale=-1:360" 

-1 in this case means variable / unknown, thus this filter resizes the video to preserve the aspect ratio of the input, keeping 360 as the height.

For me this achieved the same result you are looking for.

user65600

Posted 2010-10-19T13:07:45.260

Reputation: 166

https://video.stackexchange.com/q/21567/18935 – Akash Dubey – 2017-06-01T10:52:45.127

6

Don't have enough points to comment on an existing answer yet, but this is following user65600's answer and going further when specific codecs require a width/height that is divisible by 2 (e.g. libx264)

When you use -1 (variable/unknown), it can return an odd #. To guarantee an even #, you have to use something like trunc(ow/a/2)*2, which'll automatically calculate the closest even # while preserving the aspect ratio.

-vf "scale=trunc(ow/a/2)*2:360"

Source: https://ffmpeg.org/trac/ffmpeg/ticket/309

Toland H

Posted 2010-10-19T13:07:45.260

Reputation: 171

3

-vf "scale=trunc(ow/a/2)*2:360" doesn't work because of "self-referencing" error.

Instead, the following works :

-vf "scale=-1:360, scale=trunc(iw/2)*2:360"

Self-referencing is thus avoided by two consecutive scaling, and the rounding is done in the second step. Cute, isn't it? :)

ddystill

Posted 2010-10-19T13:07:45.260

Reputation: 31

Simple filtergraph 'scale=-1:480; scale=trunc(iw/2)*2:480' does not have exactly one input and output. Error opening filters! – digitalPBK – 2014-04-03T15:20:52.333