Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have a html 5 video player which is 700px wide, 400px high. I'm trying to use avconv to use ffmpeg to resize (while retaining the aspect ratio) and making sure it fits into my player.

Input can be a file of any size, so I need to resize the larger ones but center the smaller ones with black bars. So far I've tried: -s and -aspect, I've seen pad being used with the -vf switch but don't understand how it works enough to get what I need.


This is a rough idea of what I need. I'm not sure if it's even possible. It's almost like CSS's max-width/max-height. I realise this may just be a jumble of words but if anyone happens to understand what I'm talking about, I'd appreciate help, Thanks.

avconv command:

avconv -y -i control.avi -vcodec libx264 -b 2000k -bufsize 20M -acodec aac -strict experimental -ar 44100 -ab 256k bigbuck_out.mp4

share|improve this question
I'll write up an answer later, but I thought I'd mention the following: Re-encoding a video just to add fixed pillar- or letterboxing is a terrible idea. Not only will you lose quality in that process (or unnecessarily increase file size), you will also make it impossible to get rid of the bars later. Ideally your player should simply take care of that and resize the video while displaying it. What is your use case if I may ask? – slhck Feb 6 '13 at 12:05
We stash the original file in an S3 bucket, so that's no problem, and we're re-encoding anyway as we accept a large scope of video types, all of these have to end up as h.264 mp4 files suitable for streaming through a browser. So you'd suggest resizing the video and then making html/css pillar/letterboxes? Thanks – Jamie Taylor Feb 6 '13 at 12:23
up vote 45 down vote accepted

Here's the command that'd add pillar- or letterboxing for a fixed output width. It's a tad long, but you'll have to specify the padding some way.

First, in your shell define output width and height:


Then run the command:

ffmpeg -i in.mp4 -filter:v "scale=iw*min($width/iw\,$height/ih):ih*min($width/iw\,$height/ih), pad=$width:$height:($width-iw*min($width/iw\,$height/ih))/2:($height-ih*min($width/iw\,$height/ih))/2" out.mp4

This is stripped down to the bare essentials needed to resize and pad—add your other video and audio options as you see fit. Note that the numbers for width and height have to be divisible by 2 in order to work for most codecs.

Here's the explanation of what's going on:

  • Scaling:
    • First we need to figure out whether to scale by width or height.
    • To do this, we divide the output width by the input width, and output height by input height. This will give us the scale factors for each dimension.
    • We then check which one is lower (with min()) and choose only that factor for resizing.
    • Finally, we multiply both input width and height by that factor (iw*min(…):ih*min(…)).
  • Padding:
    • $width:$height is the output width and height
    • To figure out where to place the resulting video, we need to subtract the scaled width from the maximum output width, and the scaled height from the maximum output height.
    • The scaled widths and heights are the expressions from the scale filter.
    • We divide the resulting offset by 2 to add borders at both sides.
share|improve this answer
@JamieTaylor Consider adding -movflags +faststart to slhck's example. It will allow your progressively downloaded video to begin playback before it is completely downloaded, and also see the FFmpeg and x264 Encoding Guide and FFmpeg and AAC Encoding Guide for more encoding instructions and examples. – LordNeckbeard Feb 6 '13 at 18:08
You could shorten the pad filter to this, I think: pad=$width:$height:(ow-iw)/2:(oh-ih)/2" – jdi Dec 18 '14 at 1:19
@jdi This won't work if you are downscaling, because it'll result in negative values for ow-iw. – slhck Feb 9 '15 at 21:06
Thanks for the starting point! However, to get a proper aspect ratio for videos with a sample aspect ratio != 1, every occurance of "iw" needs to be multiplied by "sar", so replacing EVERY occurance of "iw" with "(sar*iw)" in the filter expression works for me. – schieferstapel Nov 20 '15 at 16:26

It seems to me that you need to do this in three steps:

  1. Check the input aspect ratio
  2. Scale videos with a DAR > 7/4 width-wise (change the width to 700, and scale the height to keep the aspect ratio), and scale those with DAR < 7/4 height-wise
  3. Pad the video so that it fits in the 700:400 space.

FFmpeg/avconv can do the scaling/padding with video filters in a single step, transcoding only once. For example, to take a 16:9 video, scale it width-wise, and then letterbox the results:

ffmpeg -i input.avi -filter:v 'scale=700:-1,pad=700:400:(ow-iw)/2:(oh-ih)/2' \
-c:v libx264 -b:v 2000k -bufsize 20M -c:a aac -strict experimental -ar 44100 -b:a 256k output.mp4

...but for the first step (detecting the aspect ratio and comparing it to the 7:4 you require) you'll have to use a script of some kind.

ffprobe input.avi 2>&1 | sed -n '/Video:/s/.*DAR \([0-9]*:[0-9]*\)].*/\1/p'

...will get you the video's aspect ratio, which will look like '16:9' or '4:3'. In a bash script, I'd use something like:


##  Get the aspect ratio in the form x/y
dar=$(ffprobe test0.mp4 2>&1 | sed -n '/Video:/s/.*DAR \([0-9]*:[0:9]*\)].*/\1/p' | sed 's|:|/|')
##  use bc to do x/y*100 (bash can't handle floats)
DAR=$(bc <<< 'scale=2; $dar*100')

##  ${DAR%.00} will remove the trailing .00 left by bc
if [ ${DAR%.00} -ge 175 ]; then
    ffmpeg -i "$1" -filter:v 'scale=700:-1,pad=700:400:(ow-iw)/2:(oh-ih)/2' \
    -c:v libx264 -b:v 2000k -bufsize 20M -c:a aac -strict experimental -ar 44100 -b:a 256k "${1%.*}.mp4
    ffmpeg -i "$1" -filter:v 'scale=-1:400,pad=700:400:(ow-iw)/2:(oh-ih)/2' \
    -c:v libx264 -b:v 2000k -bufsize 20M -c:a aac -strict experimental -ar 44100 -b:a 256k "${1%.*}.mp4

exit 0

Obviously you'll have to adapt it to your needs.

share|improve this answer
It's possible to do that in a single step though, see my answer. – slhck Feb 6 '13 at 14:38

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.