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

  • 4
    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
  • 1
    As the owner of the accepted answer I'd suggest you un-accept my post and accept LordNeckbeard's answer instead, as it is the most recent and much simpler. – slhck Nov 7 '16 at 15:15
  • @slhck, I was considering it, with your blessing, I'll make it so. – Jamie Taylor Nov 7 '16 at 15:54
up vote 35 down vote accepted

A simple method is to use the force_original_aspect_ratio option in the scale filter.

original image
Original image. Represents a 640x480, 4:3 aspect ratio video.

In these examples the original image will be scaled to fit into a 16:9 aspect ratio output while preserving the original aspect ratio. To do this you can either:

Pillarbox or letterbox

pillarboxed image
Pillarboxed image. Fitting a 640x480 (4:3) input into a 1280x720 (16:9) output.

ffmpeg -i input -vf "scale=1280:720:force_original_aspect_ratio=decrease,pad=1280:720:(ow-iw)/2:(oh-ih)/2" output
  • This will upscale the image. If you want to avoid upscaling see the example below.

  • Letterboxing would occur instead if the input aspect ratio is wider than the output aspect ratio. For example, an input with a 2.35:1 aspect ratio fit into a 16:9 output will result in letterboxing.

Same as above but without upscaling

no upscaling
640x480 (4:3) input into 1280x720 (16:9) output without upscaling.

ffmpeg -i input -vf "scale='min(1280,iw)':min'(720,ih)':force_original_aspect_ratio=decrease,pad=1280:720:(ow-iw)/2:(oh-ih)/2" output


enter image description here
Cropped image. 4:3 input aspect ratio, 16:9 output aspect ratio.

ffmpeg -i input -vf "scale=1280:720:force_original_aspect_ratio=increase,crop=1280:720" output

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.
  • 3
    @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
  • 1
    @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
  • @slhck Hey, I am trying in android but it doesn't seem to be working,can you help me with it !!! ffmpeg -i input.mp4 -filter:v scale=640*min(700/640,400/360):360*min(700/640,400/360),pad=700:400:(700-640*min(700/640,400/360))/2:(400-360*min(700/640,400/360))/2 output.mp4 – Nisarg Oct 20 '16 at 6:08

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.

  • 1
    It's possible to do that in a single step though, see my answer. – slhck Feb 6 '13 at 14:38
  • @slhck Now it's easier. See answer with guitar-mullet. – LordNeckbeard Nov 5 '16 at 18:32
  • @LordNeckbeard Thanks! I recently saw that option, makes it much easier indeed. – slhck Nov 7 '16 at 15:14

Your Answer


By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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