Using ffmpeg to Compress Videos

Common settings for compressing video for the internet

Video is a very common form of media on the internet these days. However, most are compressed as they're uploaded in order to reduce storage space. Here, you will see how to do the compression yourself using a tool called ffmpeg.

According to recent figures, 5 billion videos are watched on YouTube everyday; 300 hours of video are uploaded every minute. These can take up a lot of storage space, so most platforms compress the videos that are uploaded onto them, in one way or another, without visibly degrading their quality.

The exact settings or method of compression used by different platforms, such as YouTube, is often not known. However, if, for some reason, you are looking to compress videos locally, the following commands will give you compressed videos that are of similar quality as those commonly found on the internet.

To see what changes have been made to a video file, you may find mediainfo a useful tool. It displays numerous properties, including format, duration, bitrate, codec ID, width, and height, etc. To get a standalone up-to-date version of ffmpeg, go to FFmpeg Static Builds.

Frames Per Second

One of the most common ways to compress is to reduce the number of frames per second (fps). To compress video.mp4 to fps=15:

ffmpeg -i video.mp4 -r 15 video_fps15.mp4

If video.mp4 has fps=30 and plays for 10 s, it has 300 frames. video_fps15.mp4, with fps=15, also plays for 10 s, but has only 150 frames because every other frame has been removed by ffmpeg.

Reduced frame size

Another way to compress is to reduce the resolution of the frames. For example,

ffmpeg -i video.mp4 -vf scale=iw*.5:ih*.5  video_small.mp4

reduces the original width and height by 50% (the area by 75%).

Reduced overall encoding quality

Finally, you can simply compress by reducing the overall encoding quality of the video:

ffmpeg -i video.mp4 -crf 28 video_crf28.mp4

The crf scale technically goes from 0 to 51, but, according to the documentation, a sensible range is 17-28, with 17, or 18, considered virtually lossless. In general, a value greater than 23 is recommended.

Further reading


See also

comments powered by Disqus