51

I have videos with different resolution. I want that all of them will be in resolution of 480x320. I tried the command:

ffmpeg -i %s_ann.mp4 -vf scale=480x320,setsar=1:1 %s_annShrink.mp4' %(dstfile,dstfile)

but the output of the videos are files with the size of 0 kb.

What I'm doing wrong?

1
  • 1
    Impossible to provide an answer without the complete console output from your command.
    – llogan
    Commented Dec 21, 2015 at 18:44

4 Answers 4

53

I guess that really there are two questions here...

  1. How do I batch convert files?
  2. How do I auto scale a video?

How do I batch convert files?

These scripts ought to do the trick...

Windows

for %%i in (*.mp4) do (
    ffmpeg -y -i "%%i" << TODO >> "%%~ni_shrink.mp4"
)

Linux (UNTESTED!)

for i in *.mp4; 
do
    ffmpeg -y -i "$i" << TODO >> "${i%.mp4}_shrink.mp4";
done

(I'm not too sure about the output file expansion in the Linux script, worth validating that.)


How do I auto scale a video?

This is a little trickier. As you have your command, the aspect ratio is potentially going to get messed up. Options here...

  1. Scale the video, ignore aspect ratio. Result = distorted video
  2. Scale the video, keep aspect ratio so that the scaled height (or width) is adjusted to fit. Result = optimal
  3. Scale the video, keep aspect ratio and pad with black bars so that the video size is exactly 480x320. Result = wasted/increased file size
  4. Crop the input before scaling so that it "fills" the 480x320 resolution. Result = incomplete video

Option 2 would be the preferred solution, otherwise you are (probably unnecessarily) increasing the output file size. Option 3, I'll give a partially tested solution. Option 4 I'm not even going to touch.

Option 2: Scale the video, keep aspect ratio so that height is adjusted to fit

ffmpeg -y -i "%%i" -vf scale=480:-2,setsar=1:1 -c:v libx264 -c:a copy "%%~ni_shrink.mp4"

Option 3: Scale the video, keep aspect ratio and pad with black bars so that the video size is exactly 480x320

ffmpeg -y -i "%%i" -vf "[in]scale=iw*min(480/iw\,320/ih):ih*min(480/iw\,320/ih)[scaled]; [scaled]pad=480:320:(480-iw*min(480/iw\,320/ih))/2:(320-ih*min(480/iw\,320/ih))/2[padded]; [padded]setsar=1:1[out]" -c:v libx264 -c:a copy "%%~ni_shrink.mp4"
3
  • what if i wanted a fixed height and the width to fit? do i put -vf scale=-2:320,setsar=1:1 ?
    – Sodj
    Commented Jun 14, 2018 at 11:15
  • 12
    If you could give us a link that lets us understand what each token in the command means, that would be even better
    – Harsha
    Commented May 11, 2020 at 6:57
  • I'm interested in distorted video (option 1). Working with an input video that's too narrow -- it squeezes things, needs to be corrected. Commented Jun 23, 2020 at 21:32
45

You can use ffmpeg tool for it and enter command

ffmpeg -i input.mp4 -vf scale=480:320 output_320.mp4

or if you want to changing the video aspect ratio please use setdar

ffmpeg -i input.mp4 -vf scale=480:320,setdar=4:3 output_320.mp4

1
  • 5
    That doesn't work. The correct command format is ffmpeg -i input.mp4 -vf "scale=480:320" output_320.mp4 Commented Aug 4, 2019 at 20:16
8

I didn't manage to make video scaling work correctly from previous answers, so having looked into this myself, I wanted to add a current and more detailed answer to these.

Scaling:

Reference: https://trac.ffmpeg.org/wiki/Scaling - please review this for anything not covered here. This covers the scaling command very clearly with examples. The scale command works for images and videos.

The most basic scaling command below will simply resize the video to the required size in pixels. This can result in a distorted video if input and output aspect ratios are different.

ffmpeg -i input.mov -vf scale=320:240 output.mp4

To maintain the aspect ration, you can specify just the width, followed by -1 which will calculate the height to maintain the aspect ratio of the input:

ffmpeg -i input.mov -vf scale=320:-1 output.mp4

Compressing

If you also want to compress the video size, you'll find that the libx265 codec is often recommended. HOWEVER this does NOT work with the scaling command: in my tests I found that the color channels were positioned differently so there were visible ghosted overlays.

Instead use the libx264 codec and add a -crf flag. This should be the default for .mp4 outputs but I recommend specifying it to future proof your scripts.

ffmpeg -i input.mov -vf scale=1920:-2 -vcodec libx264 -crf 20 output.mp4

Note: you'll also need to use -2 in the scale not -1. This ensures that the aspect ratio always conforms to the codec requirements.

Testing

I ran some tests using a 4K RAW video shot on a Canon 5Div:

Original file is 2,128,665 KB

  • Scaling to1920:-2 at -crf 0 results in a 847,266 KB file with slight quality loss due only to rescaling
  • Scaling to1920:-2 at -crf 1 results in a 747,738 KB file with slight quality loss due only to rescaling
  • Scaling to1920:-2 at -crf 10 results in a 191,491 KB file slight quality loss due only to rescaling
  • Scaling to1920:-2 at -crf 20 results in a 19,735 KB file with further quality loss but still fairly good
  • Scaling to1920:-2 at -crf 30 results in a 4,537 KB file with even further quality loss, use as preview quality only
3

You can use the following command for upscaling and downscaling:

ffmpeg -i input.mp4 -vf scale=640x480:flags=lanczos -c:v libx264 -preset slow -crf 21 output_compress_480p.mp4

Upscaling Video Using FFMPEG

ffmpeg -i input.mp4 -vf scale=1920x1080:flags=lanczos -c:v libx264 -preset slow -crf 21 output_compress_1080p.mp4

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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