18

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.

3 Answers 3

16

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.

1
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

1
  • 2
    This answer was correct in 2013 and still works now. But for many years now you can force that a value is divisible by 2 just by using -2 instead of -1. (any -n value forces divisibility by n)
    – Chris
    Commented Sep 24, 2020 at 1:08
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? :)

2
  • Simple filtergraph 'scale=-1:480; scale=trunc(iw/2)*2:480' does not have exactly one input and output. Error opening filters!
    – digitalPBK
    Commented Apr 3, 2014 at 15:20
  • @digitalPBK your problem is you're using a ; instead of ,. With the semi-colon you're making separate statements whereas with the comma you're chaining them together. You just want to use a ,, but for education purposes, you could make that work by just specifying the inputs/outputs like this: scale=-1:480[tmp_video]; [tmp_video] scale=trunc(iw/2)*2:480
    – Chris
    Commented Sep 24, 2020 at 1:06

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .