Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change video resolution ffmpeg

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?

like image 706
liorko Avatar asked Dec 21 '15 08:12

liorko


People also ask

How do I lower video quality with FFmpeg?

Download and set up FFmpeg on your computer, use command line to compress video with FFmpeg by changing video codec format, lowering down bitrate, cutting video length, etc. For example, set CRF in FFmpeg to reduce video file size (ffmpeg -i input. mp4 -vcodec libx264 -crf 24 output.

How do I change the bitrate in FFmpeg?

To set the bitrate target in FFmpeg, use the -b:v code (bitrate:video) below : ffmpeg -i Test_1080p. MP4 -c:v libx264 -b:v 5000k Test_DR_5M.

Does FFmpeg upscale video?

How to easily upscale a 1080p video to 4k resolution using FFmpeg and a one-line command. The speed of the video upscale will depend on the video length, bitrate, your CPU and what settings you use for the upscale process.


1 Answers

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" 
like image 122
Liam Dempsey Avatar answered Sep 19 '22 12:09

Liam Dempsey