[FFmpeg-user] Automatically scaling videos and adding padding

Francesco Turco fturco at fastmail.fm
Sun Jun 12 13:53:28 CEST 2011


Hello,

I need to convert some random videos to MPEG files with a 720x576
resolution in order for my TV decoder to play them. That is, I usually
need to scale movies and add padding because source files are almost
never 720x576 already. For now, I use the following command:

ffmpeg -i input.avi -b 1536k -ab 192k -vf
scale='gte(iw/ih\,720/576)*720+lt(iw/ih\,720/576)*((576*iw)/ih):lte(iw/ih\,720/576)*576+gt(iw/ih\,720/576)*((720*ih)/iw)',pad='720:576:(720-gte(iw/ih\,720/576)*720-lt(iw/ih\,720/576)*((576*iw)/ih))/2:(576-lte(iw/ih\,720/576)*576-gt(iw/ih\,720/576)*((720*ih)/iw))/2:black'
-acodec mp2 -vcodec mpeg2video -f mpeg -r 25 -ar 48k output.mpeg

As you can see, the main problem is the -vf option, which is by far too
complex. In plain English, it does the following things:

- It automatically scales the source video in order for it to be put
inside a 720x576 rectangle without altering its original aspect ratio
(at this point the video is less than or equal to 720 pixels in width
and less than or equal to 576 pixels in height)
- It automatically adds padding around the scaled video, either at
top/bottom or at left/right, because the output file should be 720x576
exactly

The basic idea is that iw/ih is the aspect ratio of the source file,
which is compared to 720/576, the desired aspect ratio. There are three
possibilities: iw/ih is less than, equal to or greater than 720/576.
Each case is treated separately, both when scaling the video and when
adding padding.

My question is: there is some other way to accomplish this? A better
formula perhaps?

Thank you.


More information about the ffmpeg-user mailing list