Reputation: 1664
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?
Upvotes: 57
Views: 139439
Reputation: 1297
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.
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
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.
I ran some tests using a 4K RAW video shot on a Canon 5Div:
Original file is 2,128,665 KB
1920:-2
at -crf 0
results in a 847,266 KB file with slight quality loss due only to rescaling1920:-2
at -crf 1
results in a 747,738 KB file with slight quality loss due only to rescaling1920:-2
at -crf 10
results in a 191,491 KB file slight quality loss due only to rescaling1920:-2
at -crf 20
results in a 19,735 KB file with further quality loss but still fairly good1920:-2
at -crf 30
results in a 4,537 KB file with even further quality loss, use as preview quality onlyUpvotes: 9
Reputation: 31
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
Upvotes: 3
Reputation: 739
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
Upvotes: 46
Reputation: 1025
I guess that really there are two questions here...
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.)
This is a little trickier. As you have your command, the aspect ratio is potentially going to get messed up. Options here...
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"
Upvotes: 57