>FFmpegLab
FFmpeg Guide

Video Compression with FFmpeg – Complete Guide to Reducing File Size

Master CRF, codec selection, presets, two-pass encoding, and hardware acceleration to reduce file size while preserving quality.

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

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]:

Core Concepts: How Video Compression Works

Video compression works by removing redundant information. There are two main types of redundancy:

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]:

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:

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 17 (Largest file, best quality)

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:

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.

🎬
H.264 (libx264)
Most Compatible
Fast encoding, plays everywhere. Default choice for most users.
🎥
H.265 (libx265)
Best Compression
25‑50% smaller files[reference:16]. Slower, less compatible.
🚀
AV1 (libaom / SVT-AV1)
Emerging
Royalty‑free, best compression, extremely slow[reference:17].
📺
VP9 (libvpx-vp9)
Web Optimized
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]:

ffmpeg -i input.mp4 -c:v libx264 -crf 23 -preset medium -c:a aac -b:a 128k output.mp4

What this does:

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:

ffmpeg -i input.mp4 -c:v libx264 -crf 18 -preset slow -pix_fmt yuv420p -c:a aac -b:a 192k -movflags +faststart output.mp4

For smaller files (streaming, social media):

ffmpeg -i input.mp4 -c:v libx264 -crf 28 -preset fast -c:a aac -b:a 96k output.mp4

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:

  1. First pass — Analyzes the video and creates a statistics file[reference:32]
  2. 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:

# First pass (no output file, just statistics) ffmpeg -i input.mp4 -c:v libx264 -b:v 2M -pass 1 -f mp4 /dev/null

# 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.

ffmpeg -i input.mp4 -c:v h264_nvenc -preset p4 -cq 23 -c:a aac -b:a 128k output.mp4

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].

ffmpeg -i input.mp4 -c:v h264_qsv -global_quality 23 -c:a aac -b:a 128k output.mp4

AMD AMF / VAAPI

AMD's hardware encoder is available via AMF (Windows) or VAAPI (Linux)[reference:40].

ffmpeg -i input.mp4 -c:v h264_amf -quality speed -c:a aac -b:a 128k output.mp4

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:

When to use software encoding:

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:

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:

To remove audio entirely (e.g., for silent videos):

ffmpeg -i input.mp4 -c:v copy -an output.mp4

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].

ffmpeg -i input.mp4 -vf "scale=1280:720" -c:v libx264 -crf 23 output.mp4

Reducing Frame Rate

Fewer frames per second = smaller file[reference:46].

ffmpeg -i input.mp4 -vf "fps=30" -c:v libx264 -crf 23 output.mp4

Using a 10‑bit Pixel Format

Encoding with 10‑bit depth can improve compression efficiency, even for 8‑bit sources[reference:47].

ffmpeg -i input.mp4 -c:v libx265 -crf 26 -pix_fmt yuv420p10le output.mp4

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].

ffmpeg -i input.mp4 -c:v libx264 -crf 23 -movflags +faststart output.mp4

Platform‑Specific Recommendations

YouTube

YouTube recommends H.264 with AAC audio[reference:49]:

ffmpeg -i input.mp4 -c:v libx264 -crf 18 -preset slow -pix_fmt yuv420p -c:a aac -b:a 192k -movflags +faststart output.mp4

Recommended bitrates for YouTube:

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.

ffmpeg -i input.mp4 -c:v libx264 -crf 26 -preset fast -vf "scale=1080:1920" -c:a aac -b:a 96k output.mp4

Web Streaming

For websites, prioritise compatibility and fast start:

ffmpeg -i input.mp4 -c:v libx264 -crf 23 -preset medium -movflags +faststart -c:a aac -b:a 128k output.mp4

Archival / Masters

For long‑term storage, use the highest quality settings:

ffmpeg -i input.mp4 -c:v libx265 -crf 18 -preset veryslow -pix_fmt yuv420p10le -c:a aac -b:a 192k output.mp4

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:

Next time you need to compress a video, you'll know exactly which settings to use.

✦  Fresh from the render queue

Better FFmpeg workflows, delivered.

Get practical commands, new templates, and deep-dive guides for the edits that are usually hardest to get right.

✓  Copy-pasteable commands    ✓  Editor templates    ✓  No noise
One useful email at a time.