DEV Community

Javid Jamae
Javid Jamae

Posted on • Originally published at ffmpeg-micro.com

How to Compress Video with FFmpeg Without Losing Quality

Originally published at ffmpeg-micro.com

You've got a 500MB video that needs to be 50MB. The catch? You can't make it look terrible. This is the most common FFmpeg problem developers run into, and the solution is simpler than you'd think.

FFmpeg gives you precise control over the quality-to-size tradeoff through a setting called CRF (Constant Rate Factor). Get it right, and you'll cut file sizes by 50-80% with no visible quality loss.

What Is CRF and Why Does It Matter for Video Compression?

CRF stands for Constant Rate Factor. It tells the encoder how much quality you're willing to sacrifice for a smaller file. Lower CRF = higher quality = bigger file. Higher CRF = lower quality = smaller file.

For H.264 (libx264), the CRF scale runs from 0 (lossless) to 51 (worst). The sweet spot for most developers is CRF 18-28:

  • CRF 18: Visually lossless. You won't see the difference from the original. Files are still large.
  • CRF 23: The default. Good balance between quality and size. Most projects should start here.
  • CRF 28: Noticeable quality loss on close inspection, but perfectly fine for web delivery and social media.

Every 6 CRF points roughly doubles or halves the file size. So going from CRF 18 to CRF 24 cuts your file size by about 75%.

How to Compress Video with FFmpeg Using CRF

The basic command is one line:

ffmpeg -i input.mp4 -c:v libx264 -crf 23 -c:a aac -b:a 128k output.mp4
Enter fullscreen mode Exit fullscreen mode

That takes your input video, re-encodes it with H.264 at CRF 23, and compresses the audio to 128kbps AAC. For most 1080p content, this drops a 500MB file down to roughly 50-100MB.

Want better quality? Lower the CRF:

ffmpeg -i input.mp4 -c:v libx264 -crf 18 -preset slow -c:a aac -b:a 192k output.mp4
Enter fullscreen mode Exit fullscreen mode

Presets control how much time FFmpeg spends optimizing compression. Slower presets produce smaller files at the same quality. In practice, slow gives you about 5-10% better compression than medium with a meaningful increase in encoding time.

H.264 vs H.265 vs AV1: Which Codec Should You Use?

The codec you choose has a bigger impact on file size than almost any other setting.

Codec FFmpeg encoder Size reduction vs H.264 Browser support
H.264 libx264 Baseline Universal
H.265 libx265 25-50% smaller Partial
AV1 libsvtav1 30-50% smaller Chrome, Firefox, Edge

H.264 is the safe default. H.265 is great when you control playback. AV1 is the future but encoding is slow.

Two-Pass Encoding for Target File Size

Sometimes you need to hit a specific file size. Two-pass encoding analyzes the video first, then encodes with a target bitrate:

# Pass 1: Analyze
ffmpeg -i input.mp4 -c:v libx264 -b:v 3200k -pass 1 -an -f null /dev/null

# Pass 2: Encode
ffmpeg -i input.mp4 -c:v libx264 -b:v 3200k -pass 2 -c:a aac -b:a 128k output.mp4
Enter fullscreen mode Exit fullscreen mode

Common Gotchas

  • CRF values mean different things for different codecs
  • Hardware encoding trades quality for speed
  • Don't re-encode already compressed video
  • Container format matters (MP4 vs WebM vs MKV)

Compress Video with the FFmpeg Micro API

If you don't want to manage FFmpeg servers, FFmpeg Micro handles compression with a single API call:

curl -X POST https://api.ffmpeg-micro.com/v1/transcodes \
  -H "Authorization: Bearer YOUR_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "inputs": [{"url": "https://example.com/video.mp4"}],
    "outputFormat": "mp4",
    "options": [
      {"option": "-c:v", "argument": "libx264"},
      {"option": "-crf", "argument": "23"}
    ]
  }'
Enter fullscreen mode Exit fullscreen mode

Read the full guide with more examples at ffmpeg-micro.com/blog.

Top comments (0)