Friday, June 18, 2021

Making video files smaller with ffmpeg

This is actually a note to myself so that I remember how to make a video file smaller in size using ffmpeg.

The first method is to change the constant rate factor. With H.264, a CRF of 28 is at the lower end of quality.
ffmpeg -i input.mp4 -crf 28 output.mp4
 
Another way is to change the frame rate. For example, to set a frame rate of 30 fps.
ffmpeg -i input.mp4 -r 30 output.mp4
 
Combining the two, setting a CRF of 28 and a frame rate of 30.
ffmpeg -i input.mp4 -crf 28 -r 30 output.mp4
 
Another method is to scale the video to a smaller screen size. For example, 1920x1080 can be scaled to 1280x720 to make the video file smaller.
ffmpeg -i input.mp4 -vf scale=1280:720 output.mp4
 
If you set the height as -1, it will maintain the aspect ratio.
ffmpeg -i input.mp4 -vf scale=1280:-1 output.mp4
 
You can also combine the entire thing (scale to 1280x720, CRF 28, frame rate 30 fps) with this.
ffmpeg -i input.mp4 -vf scale=1280:720 -crf 28 -r 30 output.mp4
 
For reference, when I used a CRF of 28 and frame rate of 30, a 1920x1080 video with a size of around 790 MB was reduced to a size of 222 MB. When I simply scaled the video to 1280x720, the file size was reduced to 245 MB. When I used CRF of 28, frame rate of 30 fps, and scaled to 1280x720, it was reduced to 105 MB.

Update: I added bash functions to my .bash_aliases file to convert 1920x1080 fps60 MP4 files to 1280x720 fps30 and 1920x1080 fps30.
 
vid1080p30() { ffmpeg -i "$1".mp4 -crf 28 -r 30 "$1"_1080p30.mp4 ; }
vid720p() { ffmpeg -i "$1".mp4 -vf scale=1280:-1 -crf 28 -r 30 "$1"_720p.mp4 ; }
 
To use, if the original file is videofile1.mp4, then
vid1080p30 videofile1
or
vid720p videofile1

No comments: