FFmpeg Video Compression Guide
Learn FFmpeg video compression with CRF, codecs, presets, resolution, bitrate, and FFHub API examples for cloud processing.
This guide explains how to compress videos using FFmpeg, with detailed explanations of each parameter.
Understanding the Basic Command
ffmpeg -i input.mp4 -b:v 1M output.mp4
Let's break down each part:
| Parameter | Meaning |
|---|---|
ffmpeg | The program we're running |
-i input.mp4 | -i means "input", followed by your source file |
-b:v 1M | -b:v means "video bitrate", 1M = 1 Megabit per second |
output.mp4 | The output filename (always comes last) |
What is bitrate? It's how much data is used per second of video. Higher bitrate = better quality but larger file. Lower bitrate = smaller file but lower quality.
Method 1: CRF (Recommended for Beginners)
CRF (Constant Rate Factor) is the easiest way to compress video. You just pick a quality level:
ffmpeg -i input.mp4 -c:v libx264 -crf 23 output.mp4
| Parameter | Meaning |
|---|---|
-c:v libx264 | -c:v means "video codec", libx264 is the H.264 encoder |
-crf 23 | Quality level (0-51). Lower = better quality, larger file |
CRF Value Guide
| CRF | Quality | When to Use |
|---|---|---|
| 18 | Excellent (nearly lossless) | Archive important videos |
| 23 | Good (default) | General use |
| 28 | OK | Share online, social media |
| 33 | Low | Previews, drafts |
Tip: Start with CRF 23. If the file is too big, try 28. If quality is too low, try 20.
Method 2: Target File Size
If you need a specific file size, use bitrate mode:
ffmpeg -i input.mp4 -c:v libx264 -b:v 2M output.mp4
Common bitrate values:
500K= 500 Kilobits/second (small file, lower quality)1M= 1 Megabit/second (medium)2M= 2 Megabits/second (good quality)5M= 5 Megabits/second (high quality)
Encoding Speed: Presets
The -preset option controls how fast FFmpeg encodes:
ffmpeg -i input.mp4 -c:v libx264 -crf 23 -preset fast output.mp4
| Preset | Speed | File Size |
|---|---|---|
ultrafast | Very fast | Largest |
fast | Fast | Larger |
medium | Normal (default) | Normal |
slow | Slow | Smaller |
veryslow | Very slow | Smallest |
Tip: Use fast for quick results, slow for final delivery.
Reduce Resolution
Smaller resolution = much smaller file:
ffmpeg -i input.mp4 -vf scale=-2:720 -c:v libx264 -crf 23 output.mp4
| Parameter | Meaning |
|---|---|
-vf scale=-2:720 | -vf means "video filter", scale resizes the video |
-2 | Auto-calculate width to keep aspect ratio |
720 | Target height in pixels (720p) |
Common resolutions:
scale=-2:2160→ 4Kscale=-2:1080→ 1080p (Full HD)scale=-2:720→ 720p (HD)scale=-2:480→ 480p (SD)
Compress Audio Too
Add audio compression to reduce file size further:
ffmpeg -i input.mp4 -c:v libx264 -crf 23 -c:a aac -b:a 128k output.mp4
| Parameter | Meaning |
|---|---|
-c:a aac | -c:a means "audio codec", aac is a common audio format |
-b:a 128k | Audio bitrate: 128 Kilobits/second |
Audio bitrate guide:
320k→ High quality music192k→ Good quality128k→ Standard (recommended)96k→ OK for speech
Complete Examples
Best Balance (Recommended)
Good quality, reasonable file size:
ffmpeg -i input.mp4 -c:v libx264 -crf 23 -preset medium -c:a aac -b:a 128k output.mp4
Smallest File Size
For sharing online when quality is less important:
ffmpeg -i input.mp4 -vf scale=-2:720 -c:v libx264 -crf 28 -preset slow -c:a aac -b:a 96k output.mp4
Best Quality
For archiving when you want to preserve quality:
ffmpeg -i input.mp4 -c:v libx264 -crf 18 -preset slow -c:a aac -b:a 192k output.mp4
Using FFHub API
Send your command to FFHub to process in the cloud:
curl -X POST https://api.ffhub.io/v1/tasks \
-H "Authorization: Bearer YOUR_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"command": "ffmpeg -i https://your-storage.com/input.mp4 -c:v libx264 -crf 23 -c:a aac -b:a 128k output.mp4"
}'
Note: Replace the input URL with your actual video URL. FFHub will process it and give you a download link for the result.
Quick Reference
| Goal | Command |
|---|---|
| Basic compression | ffmpeg -i input.mp4 -c:v libx264 -crf 23 output.mp4 |
| Smaller file | ffmpeg -i input.mp4 -c:v libx264 -crf 28 output.mp4 |
| Higher quality | ffmpeg -i input.mp4 -c:v libx264 -crf 18 output.mp4 |
| Resize to 720p | ffmpeg -i input.mp4 -vf scale=-2:720 -c:v libx264 -crf 23 output.mp4 |
| With audio | ffmpeg -i input.mp4 -c:v libx264 -crf 23 -c:a aac -b:a 128k output.mp4 |