FFMPEG scaling, how to set scale so width AND height don't exceed a certain amount?

10,747

Solution 1

To avoid encoding black bands into the video (which the -aspect option does) you can pass a couple of expressions to the scale video filter instead, like this:

-vf scale='if(gt(a,320/500),320,-1)':'if(gt(a,320/500),-1,500)'

Note that the single quotes need to be received by ffmpeg so if you're running the command under a shell you'll need to escape them (e.g. with double quotes round the whole scale=... argument).

(Credit: ffmpeg trac)

Solution 2

You don't need to use vf scale. Just give -s widthxheight and it will scale to that size. For aspect ratio use -aspect Eg. I have a 320x240 file that I want to convert to 360x240 with black bands on the side

ffmpeg -i input.mp4 -acodec copy -vcodec mpeg4 -s 360x240 -aspect 4:3 out.mp4

That's it.

Share:
10,747
Darius
Author by

Darius

What is MVC and where do I get some?

Updated on June 09, 2022

Comments

  • Darius
    Darius almost 2 years

    I have 2 videos, one is 500 pixels by 100 pixels (just an example, like something recorded sideways on an iphone). And a 1980 x 400 pixels videos. I need the video to convert maintaining aspect ratios. I know of the -vf scale filter such as -vf scale=-1:320, but that only takes the width and scales the height accordingly. My 500 x 100 video would be 320px wide and 1600 pixels tall. That's bad, I need it to be max 500 pixels tall and max width of 320 (just example sizes).

    How would I configure the -vf scale function to do that?

    Using latest ffmpeg 0.11

    Recap: scale any video to max 500 height : 320 width while keeping aspect ratio

  • Darius
    Darius over 11 years
    Appreciate the response. The black bands add to the filesize though, wouldn't keeping the original file without the blackbands produce a smaller file?
  • av501
    av501 over 11 years
    Well blackbands dont really take much to encode. So dont worry about them. Besides they came in only because we asked the encoder to create them. If you want to maintain the aspect ratio you are going to get black bands.