Uncompressed video is enormous. A single minute of 4K footage can easily exceed 10 GB. To store, share, and stream video, you need compression — the process of reducing file size while preserving as much visual quality as possible.
FFmpeg gives you complete control over video compression. You can choose the codec, adjust quality settings, balance speed against file size, and even leverage hardware acceleration for lightning‑fast encodes.
This guide walks you through everything you need to know about video compression with FFmpeg — from basic commands to advanced techniques used by professionals.
Key takeaways
- CRF (Constant Rate Factor) is the easiest way to control quality vs. file size. Lower = better quality, higher = smaller file.
- H.264 (libx264) is the most compatible codec. H.265 (libx265) offers 25‑50% better compression at the cost of speed.
- Presets balance encoding speed and compression efficiency. Use the slowest preset you have patience for.
- Two‑pass encoding is used when you need to hit a specific file size or bitrate target.
- Hardware acceleration (NVENC, QSV, VAAPI) can speed up encoding by 5‑10×.
The Gap: Balancing Quality and File Size
Most FFmpeg tutorials show you one command and stop. But compression is a balancing act — you're trading file size for quality and encoding time. The "right" settings depend on your use case: archival, streaming, social media, or mobile delivery.
As the Cloudinary guide explains: "FFmpeg provides us with complete control over video compression through options such as resolution, bitrate, and codec settings"[reference:0]. But with so many options, it's easy to get lost.
This guide gives you a practical framework for making the right choices.
Why Video Compression Matters
Video compression is essential for several reasons[reference:1]:
- Save storage space — compressed videos take up significantly less space on your hard drive or cloud storage.
- Faster uploads and sharing — smaller files upload faster and are easier to email or share.
- Smoother streaming — compressed videos consume less bandwidth, reducing buffering for viewers.
- Lower bandwidth costs — if you host videos yourself, smaller files mean lower delivery costs.
- Mobile-friendly — compressed videos consume less cellular data, making them ideal for mobile viewers.
Core Concepts: How Video Compression Works
Video compression works by removing redundant information. There are two main types of redundancy:
- Spatial redundancy — within a single frame, adjacent pixels are often similar. Compression algorithms can group similar pixels together.
- Temporal redundancy — between consecutive frames, most of the image doesn't change. Compression algorithms only store the differences between frames.
Different codecs use different algorithms to find and remove redundancy, which is why some codecs compress better than others.
Constant Rate Factor (CRF) – The Quality Knob
CRF (Constant Rate Factor) is the most important setting for video compression. It controls the balance between quality and file size[reference:2].
The CRF scale is 0 to 51, where[reference:3]:
- 0 — Lossless (enormous files)
- 17-18 — Visually lossless (virtually indistinguishable from the original)[reference:4]
- 23 — Default, good balance of quality and size[reference:5]
- 28 — More compression, noticeable artifacts
- 51 — Worst quality
As the FFmpeg wiki explains: "Use this mode if you want to retain good visual quality and don't care about the exact bitrate or filesize of the encoded file"[reference:6].
Recommended CRF values:
- 17-18 — Archival, near‑lossless quality
- 20 — High quality, slightly better than default[reference:7]
- 23 — Default, good for general use[reference:8]
- 26-28 — For streaming or when file size is critical[reference:9]
As one guide notes: "Lower values mean higher quality. 23 produces some compression artifacts but is often good enough. Choose 20 if you want better quality with still reasonably small video files"[reference:10].
↓
CRF 23 (Default — good balance)
↓
CRF 28 (Smallest file, acceptable quality)
Presets – Speed vs. Compression
The preset determines how much CPU time the encoder spends on compression. Slower presets produce smaller files at the same quality — but they take longer to encode[reference:11].
Available presets, from fastest to slowest[reference:12]:
ultrafast → superfast → veryfast → faster → fast → medium → slow → slower → veryslow → placebo
As the FFmpeg wiki advises: "Use the slowest preset you have patience for. Ignore placebo as it provides insignificant returns for a significant increase in encoding time"[reference:13].
Recommended presets:
ultrafast— Fastest, but largest files. Use for testing.veryfast— Good for live streaming[reference:14]medium— Default, reasonable balance[reference:15]slow— Better compression, good for most usesveryslow— Best compression, use for archival
Choosing a Codec: H.264, H.265, AV1, and VP9
The codec determines the compression algorithm. Different codecs offer different trade-offs between compatibility, compression efficiency, and encoding speed.
Fast encoding, plays everywhere. Default choice for most users.
25‑50% smaller files[reference:16]. Slower, less compatible.
Royalty‑free, best compression, extremely slow[reference:17].
Royalty‑free, good for web, slower than H.264[reference:18].
H.264 (libx264) – The Gold Standard
H.264, also known as AVC, is the most widely supported video codec. It works on virtually every device, browser, and platform[reference:19]. It offers excellent quality at reasonable file sizes and encodes quickly.
As the FFmpeg wiki states: "This guide assumes that you will be using the libx264 encoder, which currently offers the best possible quality at fast encoding speeds"[reference:20].
Use H.264 when: You need maximum compatibility, you're encoding for the web, or you want fast encoding.
H.265 (libx265) – Better Compression, Slower Speed
H.265, also known as HEVC, offers 25‑50% better compression than H.264 — meaning significantly smaller files for the same quality[reference:21][reference:22]. The gains are most noticeable at 1080p and higher resolutions[reference:23].
However, H.265 encoding is much slower than H.264, and device support is not as universal.
Use H.265 when: File size is critical, you're encoding for archival, or your target platform supports HEVC (e.g., modern smartphones, smart TVs).
AV1 – The Future of Video
AV1 is a royalty‑free codec that offers even better compression than H.265. It's supported by Netflix, YouTube, and major browsers. However, encoding is extremely slow — often 10‑100× slower than H.264[reference:24].
Use AV1 when: You have time to spare and want the absolute smallest files for web delivery.
VP9 – Google's Web‑Focused Codec
VP9 is Google's royalty‑free codec, used by YouTube for high‑quality streams. It offers better compression than H.264 but is slower to encode[reference:25].
Use VP9 when: You're encoding for the web and want royalty‑free compression.
Basic Compression Command
Here's the simplest command to compress a video with FFmpeg[reference:26][reference:27]:
What this does:
-c:v libx264— Use the H.264 codec-crf 23— Quality setting (default)[reference:28]-preset medium— Balance between speed and compression[reference:29]-c:a aac— Use AAC audio-b:a 128k— Audio bitrate of 128 kbps
As one guide notes: "A great starting point is to use the H.264 codec with CRF"[reference:30].
For higher quality, archive‑ready compression:
For smaller files (streaming, social media):
Two-Pass Encoding – Targeting a Specific File Size
CRF encoding doesn't let you control the exact file size. If you need to hit a specific file size or bitrate target — for example, a 100 MB limit for email — you need two‑pass encoding[reference:31].
Two‑pass encoding works by:
- First pass — Analyzes the video and creates a statistics file[reference:32]
- Second pass — Uses the statistics to allocate bits efficiently[reference:33]
As the FFmpeg wiki explains: "In order to create more efficient encodes when a particular target bitrate should be reached, you should choose two-pass encoding"[reference:34].
Two‑pass encoding example for H.264:
# Second pass (actual output) ffmpeg -i input.mp4 -c:v libx264 -b:v 2M -pass 2 -c:a aac -b:a 128k output.mp4
As noted in the FFmpeg docs: "In pass 1 and 2, use the `-pass 1` and `-pass 2` options, respectively"[reference:35].
Calculating target bitrate:
bitrate = (target_file_size_in_bits) / (video_duration_in_seconds)
For example, a 10‑minute video (600 seconds) targeting 200 MB:
(200 MiB × 8192) / 600 seconds = ~2730 kBit/s total bitrate
Hardware Acceleration – NVENC, QSV, and VAAPI
Software encoding (libx264, libx265) is slow but offers the best quality. Hardware acceleration uses dedicated chips on your GPU to encode video much faster — often 5‑10× faster than CPU encoding[reference:36].
However, hardware encoders typically produce slightly larger files at the same quality compared to software encoders.
NVIDIA NVENC
NVENC is NVIDIA's hardware encoder, available on most NVIDIA GPUs (Kepler architecture and newer)[reference:37]. It offers excellent speed and good quality.
Intel QSV (Quick Sync Video)
QSV is Intel's hardware encoder, integrated into most Intel CPUs[reference:38]. It's low‑power and widely available[reference:39].
AMD AMF / VAAPI
AMD's hardware encoder is available via AMF (Windows) or VAAPI (Linux)[reference:40].
As one guide explains: "Hardware acceleration, via NVENC, VAAPI, or Quick Sync, isn't optional if latency matters"[reference:41].
When to use hardware acceleration:
- Live streaming — you need real‑time encoding
- Batch processing — you need to encode many videos quickly
- Rapid prototyping — you want to test settings quickly
When to use software encoding:
- Archival — you want the best possible quality‑per‑bit
- Professional delivery — quality is paramount
- You have time — software encoding is slower but better
Audio Compression – Don't Forget the Sound
Video compression is only half the story. Audio can also take up significant space. Compressing the audio track can reduce file size without affecting video quality.
Common audio codecs and bitrates:
- AAC — The standard for MP4 files. Use
-c:a aac -b:a 128kfor good quality[reference:42]. - MP3 — Widely compatible. Use
-c:a libmp3lame -b:a 128k. - Opus — Best quality‑per‑bit, ideal for web. Use
-c:a libopus -b:a 96k.
As the FFmpeg AAC guide notes: "For audible transparency, use 64kb/s for each channel (so 128kb/s for stereo)"[reference:43].
Recommended audio bitrates:
- 96 kbps — Acceptable for web, podcasts
- 128 kbps — Good quality, standard for most uses
- 192 kbps — High quality, near‑transparent
- 256+ kbps — Audiophile quality
To remove audio entirely (e.g., for silent videos):
Advanced Techniques: Downscaling, Frame Rate, and More
Beyond codec, CRF, and presets, you can further reduce file size with these techniques[reference:44]:
Downscaling (Reducing Resolution)
Lower resolution = fewer pixels = smaller file[reference:45].
Reducing Frame Rate
Fewer frames per second = smaller file[reference:46].
Using a 10‑bit Pixel Format
Encoding with 10‑bit depth can improve compression efficiency, even for 8‑bit sources[reference:47].
Web Optimisation: -movflags +faststart
This moves the moov atom (metadata) to the front of the file, allowing videos to start playing before they're fully downloaded. Essential for web video[reference:48].
Platform‑Specific Recommendations
YouTube
YouTube recommends H.264 with AAC audio[reference:49]:
Recommended bitrates for YouTube:
- 1080p: 8‑12 Mbps
- 4K (2160p): 35‑45 Mbps
Social Media (TikTok, Instagram, Reels)
Social media platforms have strict file size and resolution limits. Use a higher CRF (lower quality) to meet size limits.
Web Streaming
For websites, prioritise compatibility and fast start:
Archival / Masters
For long‑term storage, use the highest quality settings:
Frequently Asked Questions (FAQ)
What is the best CRF value for video compression?
The best CRF value depends on your quality needs. For near‑lossless archival, use CRF 17‑18. For general use, CRF 23 is the default and offers a good balance. For streaming or smaller files, CRF 26‑28 works well. CRF 0 is mathematically lossless but produces enormous files.[reference:50]
Should I use H.264 or H.265 for compression?
H.265 (HEVC) offers 25‑50% better compression than H.264, meaning smaller files for the same quality[reference:51]. However, H.265 encoding is much slower and has less device compatibility. Use H.264 for maximum compatibility; use H.265 for archival or when file size is critical.
What is the difference between CRF and bitrate encoding?
CRF (Constant Rate Factor) maintains consistent quality across the video by allocating more bits to complex scenes and fewer to simple ones[reference:52]. Bitrate encoding targets a specific data rate, resulting in consistent file size but variable quality. CRF is generally preferred for most use cases.
Does hardware acceleration reduce compression quality?
Hardware encoders (NVENC, QSV, AMF) are faster but typically produce slightly larger files at the same quality compared to software encoders (libx264, libx265). For professional archival, use software encoding. For rapid transcoding or streaming, hardware acceleration is a great choice.
How do I compress a video for YouTube or social media?
Use H.264 with CRF 18‑23, preset slow, yuv420p pixel format, AAC audio at 128‑192kbps, and add -movflags +faststart for web streaming[reference:53]. For YouTube specifically, 1080p at 8‑12 Mbps or 4K at 35‑45 Mbps is recommended.
What is two‑pass encoding and when should I use it?
Two‑pass encoding runs the encoder twice to analyse the video and allocate bits more efficiently[reference:54]. Use it when you need to hit a specific file size or bitrate target — for example, if you need a video under 100 MB for email or a platform with upload limits.
Final Word
Video compression is a balancing act between quality, file size, and encoding time. FFmpeg gives you the tools to control all three.
Start with the basics: H.264, CRF 23, preset medium. Then experiment: lower the CRF for better quality, use a slower preset for smaller files, or try H.265 for even better compression.
Key takeaways:
- CRF is your quality knob — lower = better quality, higher = smaller file
- Presets trade speed for compression — use the slowest you can tolerate
- Codecs balance compatibility and efficiency — H.264 for universal playback, H.265 for smaller files
- Two‑pass encoding targets specific file sizes
- Hardware acceleration speeds up encoding at a slight quality cost
- Don't forget audio — it can take up significant space
Next time you need to compress a video, you'll know exactly which settings to use.