How to Compress Video with FFmpeg: Complete Command Guide (2026)
Master FFmpeg video compression with copy-paste commands for every use case — from quick size reduction to professional-grade encoding.
Why Use FFmpeg for Video Compression?
FFmpeg is the Swiss Army knife of video processing. It's free, open-source, and runs on every platform. While GUI tools like Compresto make compression point-and-click simple on Mac, FFmpeg gives you complete control over every compression parameter.
Here's what makes FFmpeg powerful for video compression:
- Universal format support - MP4, MOV, MKV, AVI, WebM, and 100+ formats
- Advanced codec options - H.264, H.265/HEVC, VP9, AV1
- Precise control - Fine-tune bitrate, quality, resolution, frame rate
- Batch processing - Script compression for hundreds of files
- No quality loss (if done right) - Compress 50-80% without visible degradation
The learning curve is steep, but this guide gives you copy-paste commands for every common scenario.
Basic FFmpeg Compression Command
The simplest way to compress a video is using the CRF (Constant Rate Factor) method with H.264:
ffmpeg -i input.mp4 -c:v libx264 -crf 23 -c:a aac -b:a 128k output.mp4
What this does:
-i input.mp4- Input file-c:v libx264- Use H.264 video codec (most compatible)-crf 23- Quality level (18-28 is the sweet spot)-c:a aac- Use AAC audio codec-b:a 128k- Audio bitrate at 128 kbpsoutput.mp4- Output file
This command typically reduces file size by 40-60% with minimal quality loss. For a 1GB video, expect 400-600MB output.
Understanding CRF Values (Quality vs Size)
CRF controls quality in FFmpeg. Lower values = better quality + larger files. Higher values = more compression + quality loss.
| CRF Value | Quality | Use Case | File Size |
|---|---|---|---|
| 18 | Visually lossless | Archiving, professional work | ~80% of original |
| 23 | High quality (default) | General use, web streaming | ~50% of original |
| 28 | Medium quality | Social media, email sharing | ~30% of original |
| 32 | Lower quality | Preview clips, extreme compression | ~15% of original |
Finding your ideal CRF:
Test different values on a 10-second clip:
# Test CRF 20
ffmpeg -i input.mp4 -ss 00:00:10 -t 00:00:10 -c:v libx264 -crf 20 -c:a copy test-crf20.mp4
# Test CRF 23
ffmpeg -i input.mp4 -ss 00:00:10 -t 00:00:10 -c:v libx264 -crf 23 -c:a copy test-crf23.mp4
# Test CRF 26
ffmpeg -i input.mp4 -ss 00:00:10 -t 00:00:10 -c:v libx264 -crf 26 -c:a copy test-crf26.mp4
Compare the results and pick the lowest acceptable quality. This saves hours of trial and error.
H.264 vs H.265 vs VP9: Which Codec to Choose?
Each codec has different compression efficiency and compatibility trade-offs:
| Codec | FFmpeg Library | Compression | Speed | Compatibility | Best For |
|---|---|---|---|---|---|
| H.264 | libx264 | Good | Fast | Universal (all devices) | General use, sharing |
| H.265/HEVC | libx265 | Excellent | Slow | Modern devices only | 4K video, archiving |
| VP9 | libvpx-vp9 | Excellent | Very slow | Web browsers, YouTube | Web streaming |
| AV1 | libaom-av1 | Best | Extremely slow | Limited support | Future-proofing |
H.264 Compression (Best Compatibility)
ffmpeg -i input.mp4 -c:v libx264 -preset slow -crf 22 -c:a aac -b:a 128k output.mp4
Use H.264 when you need maximum compatibility. Every device since 2005 can play H.264 video.
H.265/HEVC Compression (Best Quality-to-Size Ratio)
ffmpeg -i input.mp4 -c:v libx265 -preset medium -crf 26 -c:a aac -b:a 128k output.mp4
H.265 achieves 30-50% better compression than H.264 at the same quality. A CRF of 26 in H.265 looks similar to CRF 23 in H.264, but at half the file size.
Important: H.265 playback requires modern hardware (2015+ for most devices). iPhones support it natively, but some Android phones and older computers struggle.
VP9 Compression (Best for Web)
ffmpeg -i input.mp4 -c:v libvpx-vp9 -crf 30 -b:v 0 -c:a libopus -b:a 96k output.webm
VP9 is YouTube's preferred codec. It matches H.265 compression but with better browser support. Encoding is 5-10x slower than H.264.
Preset Options: Speed vs Compression
The -preset flag controls encoding speed and compression efficiency:
# Faster encoding, larger file
ffmpeg -i input.mp4 -c:v libx264 -preset ultrafast -crf 23 output.mp4
# Balanced (default)
ffmpeg -i input.mp4 -c:v libx264 -preset medium -crf 23 output.mp4
# Slower encoding, smaller file
ffmpeg -i input.mp4 -c:v libx264 -preset slow -crf 23 output.mp4
Available presets: ultrafast, superfast, veryfast, faster, fast, medium, slow, slower, veryslow
Practical advice: Use slow for final outputs. The 2-3x longer encoding time saves 10-15% file size. Use faster or fast when testing or processing hundreds of files.
Compressing to a Target File Size
Sometimes you need a video under a specific size limit (e.g., 25MB for email, 8MB for Discord).
Two-pass encoding for target bitrate:
# Calculate target bitrate
# Formula: (target_size_MB * 8192) / duration_seconds = bitrate_kbps
# Example: 25MB video, 60 seconds = (25 * 8192) / 60 = 3413 kbps
# First pass
ffmpeg -i input.mp4 -c:v libx264 -b:v 3400k -pass 1 -an -f mp4 /dev/null
# Second pass
ffmpeg -i input.mp4 -c:v libx264 -b:v 3400k -pass 2 -c:a aac -b:a 96k output.mp4
Two-pass encoding analyzes the entire video first, then encodes with optimized bitrate distribution. Complex scenes get more bits, simple scenes get fewer bits. The result is better quality at the target file size compared to single-pass encoding.
Quick single-pass alternative:
ffmpeg -i input.mp4 -c:v libx264 -b:v 3400k -maxrate 3400k -bufsize 6800k -c:a aac -b:a 96k output.mp4
Single-pass is faster but less efficient. Use it for quick tests or when encoding time matters more than quality.
Compressing While Resizing
Combining compression with resolution reduction maximizes file size savings. Reducing 4K to 1080p can cut file size by 75%.
# 4K to 1080p
ffmpeg -i input.mp4 -vf scale=1920:1080 -c:v libx264 -crf 23 -c:a aac -b:a 128k output.mp4
# Maintain aspect ratio (height auto-calculated)
ffmpeg -i input.mp4 -vf scale=1280:-2 -c:v libx264 -crf 23 -c:a aac -b:a 128k output.mp4
The -2 tells FFmpeg to calculate height automatically while keeping it divisible by 2 (required for H.264 encoding).
For more resolution control, check out our guide on how to resize videos with FFmpeg.
Batch Compressing Multiple Videos
Bash/Zsh (Mac/Linux)
# Compress all MP4 files in current directory
for i in *.mp4; do
ffmpeg -i "$i" -c:v libx264 -crf 23 -c:a aac -b:a 128k "compressed_${i}"
done
PowerShell (Windows)
Get-ChildItem -Filter *.mp4 | ForEach-Object {
ffmpeg -i $_.FullName -c:v libx264 -crf 23 -c:a aac -b:a 128k "compressed_$($_.Name)"
}
Recursive directory processing (Mac/Linux)
find . -type f -name "*.mp4" -exec sh -c '
output="compressed/$(basename "{}")"
mkdir -p compressed
ffmpeg -i "{}" -c:v libx264 -crf 23 -c:a aac -b:a 128k "$output"
' \;
This finds all MP4 files in subdirectories and saves compressed versions to a compressed folder.
Advanced Compression Techniques
Hardware Acceleration (10x Faster Encoding)
Modern GPUs can encode video 5-15x faster than CPU encoding:
NVIDIA GPUs (NVENC):
ffmpeg -i input.mp4 -c:v h264_nvenc -preset slow -crf 23 -c:a copy output.mp4
AMD GPUs (AMF):
ffmpeg -i input.mp4 -c:v h264_amf -quality quality -rc cqp -qp 23 -c:a copy output.mp4
Apple Silicon Macs (VideoToolbox):
ffmpeg -i input.mp4 -c:v h264_videotoolbox -b:v 5000k -c:a copy output.mp4
Hardware encoding is faster but produces 10-20% larger files at equivalent quality. Use it when processing time is critical.
Stripping Metadata to Save Space
Video files often contain metadata (GPS coordinates, camera settings, timestamps) that add unnecessary bloat:
ffmpeg -i input.mp4 -map_metadata -1 -c:v copy -c:a copy output.mp4
This can save 1-5MB on files from phones or cameras.
Compressing Audio Without Re-encoding Video
If your video is already well-compressed but audio is taking up space:
ffmpeg -i input.mp4 -c:v copy -c:a aac -b:a 96k output.mp4
The -c:v copy flag copies the video stream without re-encoding (instant), while compressing audio from 256kbps to 96kbps.
Handling Different Input Formats
FFmpeg automatically detects input format, but output container affects compatibility:
MOV to MP4 (without re-encoding)
ffmpeg -i input.mov -c:v copy -c:a copy output.mp4
AVI to MP4 (with compression)
ffmpeg -i input.avi -c:v libx264 -crf 23 -c:a aac -b:a 128k output.mp4
MKV to MP4
ffmpeg -i input.mkv -c:v copy -c:a aac -b:a 128k output.mp4
When to Use a GUI Tool Instead
FFmpeg is powerful but has a steep learning curve. If you're on Mac and need to:
- Compress videos without memorizing commands
- See real-time preview of quality changes
- Batch process with drag-and-drop
- Use hardware acceleration automatically
Consider Compresto — a native Mac app built for video, image, and PDF compression. It uses FFmpeg under the hood but with an intuitive interface. Perfect for occasional compression tasks without terminal commands.
Troubleshooting Common Issues
"Unknown encoder 'libx264'"
Your FFmpeg build doesn't include x264 support. Install a full build:
Mac: brew install ffmpeg
Windows: Download from ffmpeg.org or use gyan.dev builds
Linux: sudo apt install ffmpeg (Ubuntu/Debian)
Video plays but audio is missing
Add audio codec explicitly:
ffmpeg -i input.mp4 -c:v libx264 -crf 23 -c:a aac -b:a 128k output.mp4
"height not divisible by 2"
H.264 requires even dimensions. Use -2 in scale filter:
ffmpeg -i input.mp4 -vf scale=1280:-2 -c:v libx264 -crf 23 output.mp4
Encoding takes forever
Use a faster preset or hardware acceleration:
# Faster CPU encoding
ffmpeg -i input.mp4 -c:v libx264 -preset faster -crf 23 output.mp4
# Or use GPU (NVIDIA example)
ffmpeg -i input.mp4 -c:v h264_nvenc -preset fast -crf 23 output.mp4
Quick Reference: Common Commands
# Standard compression (good quality, ~50% smaller)
ffmpeg -i input.mp4 -c:v libx264 -crf 23 -c:a aac -b:a 128k output.mp4
# High compression (smaller files, lower quality)
ffmpeg -i input.mp4 -c:v libx264 -crf 28 -c:a aac -b:a 96k output.mp4
# Best quality-to-size (H.265)
ffmpeg -i input.mp4 -c:v libx265 -crf 26 -c:a aac -b:a 128k output.mp4
# Fast compression (quick, larger files)
ffmpeg -i input.mp4 -c:v libx264 -preset faster -crf 23 -c:a copy output.mp4
# Compress + resize to 1080p
ffmpeg -i input.mp4 -vf scale=1920:1080 -c:v libx264 -crf 23 -c:a aac -b:a 128k output.mp4
# Target file size (two-pass, 3400kbps example)
ffmpeg -i input.mp4 -c:v libx264 -b:v 3400k -pass 1 -an -f mp4 /dev/null && \
ffmpeg -i input.mp4 -c:v libx264 -b:v 3400k -pass 2 -c:a aac -b:a 96k output.mp4
# Copy video, compress audio only
ffmpeg -i input.mp4 -c:v copy -c:a aac -b:a 96k output.mp4
# Hardware acceleration (NVIDIA GPU)
ffmpeg -i input.mp4 -c:v h264_nvenc -preset slow -crf 23 -c:a copy output.mp4
Final Tips
-
Always keep original files - Compression is irreversible. Never overwrite your source video.
-
Test on a clip first - Use
-ssand-tflags to compress a 10-second sample before processing the full video. -
CRF beats bitrate - For single-pass compression, CRF produces better quality than fixed bitrate in most scenarios.
-
Audio matters - 128kbps AAC is transparent for most content. Don't go below 96kbps unless file size is critical.
-
Slower presets = smaller files - If you have time, use
-preset slow. The extra encoding time pays off in file size savings. -
Match codec to use case - H.264 for compatibility, H.265 for archiving, VP9 for web streaming.
FFmpeg compression becomes intuitive after a few runs. Bookmark this guide, copy the commands that match your needs, and adjust CRF values to find your quality sweet spot. And if you want the same power without the terminal, Compresto brings one-click compression to Mac.