Compressing video with ffmpeg

I was looking into what was taking up space in my Dropbox account and found some older videos, each a few gigabytes in size, where I didn’t really care if there was a small loss of quality to make them much smaller. I found some options with ffmpeg that worked pretty well

ffmpeg -i input_file -vcodec libx265 -crf 34 output_file

This will re-encode the file using x265. The “-crf” option specifies the amount of compression, with a range from 0-51: “0” being lossless and “23” the default. With the default I was able to halve the storage space required with negligible video loss. I upped it to “34”, as above, and while there was a bit of loss in video clarity, it didn’t matter for the video content. The result? A file originally ~ 2.4 GB in size was reduced to ~ 300 MB.

I had a folder of such videos, so used this Bash one-liner to process each file and output a new processed file.

for file in ./*.mp4; do ffmpeg -i ${file} -vcodec libx265 -crf 34 ${file%.*}_processed.mp4; done

${file%.*}_processed.mp4 means from the original file, keep the filename (removing the extension) and replaced it with “_processed.mp4”.

Kristian Golding

View more posts from this author