Change video resolution ffmpeg

liorko picture liorko · Dec 21, 2015 · Viewed 44.3k times · Source

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?

Answer

Liam Dempsey picture Liam Dempsey · Dec 21, 2015

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"