Reducing the file size of a video (compress) has several advantages. Obviously it reduces the amount of storage needed and although storage seems limitless nowadays a lot of services only allow a limited amount of storage per file or in total. Furthermore for streaming certainly with limited bandwidth a compressed video provides a better experience for the user.
My go to tool for compressing a video is FFmpeg. FFmpeg is the Swiss army knife for video and audio conversion (and much more). I won’t even attempt to describe the possibilities but just take a look at the FFmpeg wiki page to appreciate the possibilities. It’s also worth noting that FFmpeg is free and open source software and that it is available for Linux, OSX and Windows and can be downloaded here. Before I forget, FFmpeg is a command line tool so perhaps it’s not for everyone.
How to use FFmpeg for compression
Getting back to the task of compressing a video. This can be achieved in several ways in FFmpeg. The way that works best for my videos is to use the CRF option. CRF stands for constant rate factor and can be used to compress a video while maintaining a good quality. It is used in combination with the x264 or x265 encoder. A typical command to compress a video looks like this:
ffmpeg -i input.mp4 -vcodec libx264 -crf 24 output.mp4
This command takes the file input.mp4, uses the x264 codec to produce a file output.mp4. The -crf 24 determines the amount of compression where the number 24 can vary from 0-51 where 0 is lossless and 51 is the worst quality possible. I used numbers between 18 and 28 to achieve good quality videos at strongly reduced file size.
To reduce the file size even further we can use the -preset option. A slower preset provides an even better compression. The default value is medium, I also tried slow and veryslow. For the latter the command looks like this:
ffmpeg -i input.mp4 -preset veryslow -vcodec libx264 -crf 24 output.mp4
To conclude this blog post I’ve inserted a table below where I compare the file size of a sample file that I compressed with the methods described above.
|crf 24, slow||11.8|
|crf 24, very slow||11.1|
Now remember this is just one video and one may get very different results with a different video (for this also read the link to the CRF guide below) but the trend is clear. To demonstrate the quality of the resulting videos I’ll provide here the original and the most compressed one so that you can compare the difference.
Making a clip can save time
Now someone on Mastodon tipped me that instead of compressing the full video one can create a small clip first. This can safe valuable time assessing whether the chosen compression options do achieve the desired result. To do this the options -ss and -t should be used where -ss is the starting time (hh:mm:ss) of the clip and -t the duration of the clip in seconds. Here are two example.
ffmpeg -i input.mp4 -vcodec libx264 -crf 24 -t 10 output.mp4
ffmpeg -i input.mp4 -vcodec libx264 -crf 24 -ss 00:02:30 -t 10 output.mp4
In the first example a ten second clip is created starting from the beginning of the video. In the second example a ten second clip is created starting from 2 minutes and 30 seconds.
Here are useful links for reference and further reading:
H.264 Video Encoding Guide: http://trac.ffmpeg.org/wiki/Encode/H.264
CRF Guide: https://slhck.info/video/2017/02/24/crf-guide.html