Published on

Compressing Video Files with FFmpeg

Authors
  • avatar
    Name
    Hieu Cao
    Twitter

Introduction

Video compression is an essential step to optimize video files for storage, sharing, and streaming. FFmpeg, a powerful multimedia framework, makes this task straightforward. In this blog, we will discuss how to compress video files effectively using FFmpeg.


Why Compress Video Files?

There are several reasons to compress video files:

  1. Save Storage Space: Reduce file size to save disk space.
  2. Improve Sharing: Smaller files upload and download faster.
  3. Optimize Streaming: Lower bandwidth requirements for online playback.
  4. Compatibility: Create videos with resolutions and formats that work across devices.

How to Compress Videos with FFmpeg

Step 1: Basic Compression Command

To compress a video file:

ffmpeg -i input-video.mp4 -vcodec libx264 -crf 23 output-video.mp4
  • -i input-video.mp4 specifies the input file.
  • -vcodec libx264 uses the H.264 codec, which is efficient and widely supported.
  • -crf 23 sets the quality level (lower values mean higher quality and larger files; typical range is 18–28).
  • output-video.mp4 is the compressed output file.

Step 2: Adjust Compression Quality

The Constant Rate Factor (CRF) controls the quality:

  • Higher Quality:

    ffmpeg -i input-video.mp4 -vcodec libx264 -crf 18 output-video.mp4
    
  • Lower Quality (Smaller File):

    ffmpeg -i input-video.mp4 -vcodec libx264 -crf 28 output-video.mp4
    

Conclusion

With FFmpeg, compressing video files is efficient and customizable. Whether you’re optimizing for storage, sharing, or streaming, these commands provide a flexible toolkit to meet your needs. Start experimenting with different settings to find the perfect balance between quality and file size for your projects!