Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

linux

ffmpeg

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

like image 245
Darius Avatar asked Aug 28 '12 04:08

Darius


2 Answers

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)

like image 78
markshep Avatar answered Nov 02 '22 11:11

markshep


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.

like image 31
av501 Avatar answered Nov 02 '22 10:11

av501