Video compression is an essential skill for content creators, filmmakers, and anyone who works with digital video. While high-quality raw footage can produce stunning results, the large file sizes are impractical for storage, sharing, and streaming. Fortunately, FFmpeg provides powerful tools to compress video files while preserving visual quality. This guide will walk you through effective compression techniques that minimize file size without sacrificing the viewing experience.
If you're looking for somewhere to host and stream your videos for you, Mux's Video API has everything you need to manage video for your application.
Why compress video files?
There are numerous reasons to compress your video files:
- Storage optimization: Reduce the space required on hard drives and cloud storage.
- Faster uploads: Send compressed files to clients or upload to hosting platforms more quickly.
- Improved streaming: Smaller files lead to smoother playback and less buffering.
- Reduced bandwidth costs: Pay less for hosting and content delivery.
- Email and messaging: Meet size limits for video attachments.
- Mobile viewing: Create mobile-friendly versions that consume less data.
- Archive management: Store more content in the same amount of space.
Let's explore how to use FFmpeg to compress videos while maintaining visual quality.
Understanding video compression fundamentals
Before diving into FFmpeg commands, it's helpful to understand some basic concepts that affect video quality and file size:
Codecs
A codec (encoder-decoder) is the algorithm used to compress and decompress video data. Modern codecs like H.264, H.265 (HEVC), VP9, and AV1 offer increasingly efficient compression. H.264 remains the most widely compatible, while H.265 and AV1 provide better compression but may have compatibility or processing limitations.
Bitrate
Bitrate is the amount of data processed per unit of time, typically measured in kilobits per second (kbps) or megabits per second (Mbps). Higher bitrates generally mean better quality but larger files.
Resolution
The dimensions of your video (width × height) significantly impact file size. Common resolutions include:
- 3840×2160 (4K)
- 1920×1080 (1080p)
- 1280×720 (720p)
Frame rate
Frame rate is the number of frames displayed per second (fps). Common frame rates are 24, 25, 30, 50 and 60 fps. Higher frame rates create smoother motion but increase file size.
Basic video compression with FFmpeg
The simplest way to compress a video file is to re-encode it with a lower bitrate:
ffmpeg -i input_video.mp4 \
-c:v libx264 -crf 23 \
-c:a aac -b:a 128k \
output_video.mp4Breakdown of the command:
- -i input_video.mp4: Specifies the input video file
- -c:v libx264: Uses the H.264 video codec
- -crf 23: Sets the Constant Rate Factor (quality level) to 23 (lower = better quality, higher = smaller file)
- -c:a aac: Uses AAC for audio
- -b:a 128k: Sets the audio bitrate to 128 kbps
- output_video.mp4: Name of the compressed output file
The CRF value is the most important parameter for quality control. The range is 0-51, where:
- 0 is lossless (largest files)
- 18-23 is considered "visually lossless" (good balance)
- 23-28 is standard range for online content
- 28+ shows noticeable quality loss (smallest files)
If you're delivering your video over the internet, then you will likely want to use an adaptive bitrate format like HLS. Mux can do that for you.
Learn more about Mux VideoAdvanced techniques for quality preservation
Two-pass encoding
Two-pass encoding analyzes the video in the first pass, then uses that information in the second pass for more efficient compression:
ffmpeg -i input_video.mp4 \
-c:v libx264 -b:v 1000k \
-pass 1 \
-f mp4 /dev/null
ffmpeg -i input_video.mp4 \
-c:v libx264 -b:v 1000k \
-pass 2 \
-c:a aac \
-b:a 128k \
output_video.mp4For Windows, replace /dev/null with NUL.
Adjusting the encoding preset
FFmpeg provides presets that balance encoding speed against compression efficiency:
ffmpeg -i input_video.mp4 \
-c:v libx264 \
-crf 23 \
-preset slower \
-c:a aac -b:a 128k \
output_video.mp4Presets from fastest to slowest (and increasing compression efficiency):
- ultrafast
- superfast
- veryfast
- faster
- fast
- medium (default)
- slow
- slower
- veryslow
- placebo (rarely used due to diminishing returns)
Slower presets produce smaller files at the same quality level but take longer to encode.
Using more efficient codecs
H.265 (HEVC) typically offers about 50% better compression than H.264 at the same quality level:
ffmpeg -i input_video.mp4 \
-c:v libx265 \
-crf 28 \
-c:a aac \
-b:a 128k \
output_video.mp4Note that HEVC typically requires a CRF value about 4-6 points higher than H.264 to achieve similar visual quality.
For VP9 codec (used by YouTube and supported by most browsers):
ffmpeg -i input_video.mp4 \
-c:v libvpx-vp9 \
-crf 30 \
-b:v 0 \
-c:a libopus \
-b:a 128k \
output_video.webmAdaptive quality targeting
To target a specific file size while maintaining the best possible quality:
ffmpeg -i input_video.mp4 \
-c:v libx264
-b:v 1000k
-maxrate 1500k
-bufsize 2000k \
-c:a aac \
-b:a 128k \
output_video.mp4This approach uses:
- -b:v 1000k: Target average bitrate
- -maxrate 1500k: Peak bitrate limit
- -bufsize 2000k: Controls how quickly the bitrate can vary
Video scaling techniques
Reducing resolution
Lowering the resolution is one of the most effective ways to reduce file size:
ffmpeg -i input_video.mp4 \
-c:v libx264 \
-crf 23 \
-vf "scale=1280:720" \
-c:a aac \
-b:a 128k \
output_720p.mp4Use -2 to maintain the aspect ratio while ensuring even dimensions:
ffmpeg -i input_video.mp4 \
-c:v libx264 \
-crf 23 \
-vf "scale=1280:-2" \
-c:a aac \
-b:a 128k \
output_720p.mp4Reducing frame rate
If your original video is 60fps, reducing to 30fps can significantly decrease file size:
ffmpeg -i input_video.mp4 \
-c:v libx264 \
-crf 23 \
-r 30 \
-c:a aac \
-b:a 128k \
output_30fps.mp4Balancing compression strategies for different scenarios
Web delivery
For online video that needs to load quickly:
ffmpeg -i input_video.mp4 \
-c:v libx264 \
-crf 23 \
-r 30 \
-c:a aac \
-b:a 128k \
output_30fps.mp4The -tune fastdecode option optimizes for playback on devices with limited processing power.
Archival quality
For long-term storage where quality preservation is critical:
ffmpeg -i input_video.mp4 \
-c:v libx264 \
-crf 18 \
-preset veryslow \
-c:a aac \
-b:a 192k \
archive_quality.mp4Low bandwidth environments
For users with slow internet connections:
ffmpeg -i input_video.mp4 \
-c:v libx264 \
-crf 27 \
-vf "scale=854:480" \
-c:a aac \
-b:a 96k \
low_bandwidth.mp4Mux tips for evaluating compression results
- A/B comparison: Always compare the original and compressed videos side by side.
- Check dark scenes: Compression artifacts are most visible in dark or gradient-heavy scenes.
- Watch for banding: Look for color banding in skies or other smooth gradients.
- Motion test: Fast-moving scenes often show compression issues more readily.
- Various devices: Test your compressed video on multiple devices and screen sizes.
- Use Mux Data for measuring performance in the wild.
Choosing the right compression approach
Different compression strategies have various benefits and drawbacks:
CRF-based compression:
- Benefits:
- Maintains consistent quality throughout the video
- Simpler command syntax
- Automatically adjusts bitrate based on content complexity
- Drawbacks:
- File size can be unpredictable
- Not ideal when targeting specific file sizes
Bitrate-based compression:
- Benefits:
- More predictable output file sizes
- Better for streaming with bandwidth constraints
- Drawbacks:
- Quality may vary across different scenes
- Complex scenes may look worse than simple ones
- Often requires two-pass encoding for best results
Resolution reduction:
- Benefits:
- Dramatic file size reduction
- Often unnoticeable on smaller screens
- Drawbacks:
- Visible quality loss on large displays
- Not ideal for content with fine details
Final recommendations
For most general-purpose video compression needs:
- Start with H.264 and CRF 23
- Use the "medium" or "slow" preset
- Keep the original resolution if possible
- Adjust audio to 128-192 kbps AAC
This approach typically yields a good balance between quality and file size for most content.
For more advanced requirements:
- Use H.265 for about 50% better compression if compatibility isn't an issue
- Consider two-pass encoding for more predictable file sizes
- Experiment with different presets to find your ideal quality/time tradeoff
Remember that the quality of your output depends on the quality of your input files and the compression choices you make. Always test your compressed videos thoroughly before distributing them.
If you're looking for an API to handle video compression and optimization automatically, check out the Mux Video API, which handles transcoding, storage, and delivery.
Video compression FAQs
What CRF value should I use for different quality needs?
CRF 18-20 for near-lossless archival quality, CRF 23 for high-quality web video (YouTube/Vimeo standard), CRF 28 for acceptable quality at smaller file sizes, and CRF 32+ only when file size is critical and quality loss is acceptable. Start with CRF 23 and adjust based on results—lower values if quality suffers, higher if file size needs reduction. Remember that H.265 needs CRF values 4-6 points higher than H.264 for equivalent quality.
How much can I compress a video before quality becomes unacceptable?
This depends heavily on content complexity and viewing conditions. Videos with lots of motion or detail compress less efficiently than static scenes. As a rule of thumb, you can typically achieve 50-70% file size reduction with CRF 23-25 H.264 before quality degradation becomes noticeable on most displays. Test compressions at different CRF values and compare side-by-side to find your threshold.
Should I reduce resolution or increase compression for smaller files?
Both approaches work, but resolution reduction has more dramatic impact. Dropping from 1080p to 720p typically reduces file size by 50-60% and is barely noticeable on smaller screens. Increasing CRF from 23 to 28 might reduce size by 30-40% but affects quality on all display sizes. For mobile-first content, resolution reduction is often the better choice. For large-screen viewing, stick with native resolution and adjust CRF.
What's the difference between H.264 and H.265 for compression?
H.265 (HEVC) achieves approximately 50% better compression than H.264 at equivalent quality—a 1080p video that's 100MB in H.264 would be roughly 50MB in H.265. However, H.265 encoding takes 2-3x longer and isn't supported on all devices, especially older hardware. Use H.264 for maximum compatibility, H.265 when file size is critical and you know viewer devices support it.
Why does slower preset create smaller files?
Slower presets allow the encoder more time to analyze each frame and make better compression decisions—finding optimal motion vectors, reference frames, and partition modes. The actual video quality at a given CRF stays similar, but slower presets achieve that quality with fewer bits. The tradeoff is encoding time: "veryslow" might take 5-10x longer than "fast" but produces 10-20% smaller files at the same CRF.
Can I compress an already compressed video further?
Yes, but each compression pass introduces additional quality loss (generation loss). If the source is already heavily compressed, further compression will make artifacts more visible. Check the source bitrate with ffprobe—if it's already low (under 2 Mbps for 1080p), further compression will significantly degrade quality. When possible, always compress from the highest quality source available rather than re-compressing compressed videos.
How do I compress video for specific file size limits?
Use two-pass encoding with target bitrate calculated from desired file size: (target_size_MB × 8192) / duration_seconds = bitrate_kbps. For example, for a 50MB file of a 60-second video: (50 × 8192) / 60 = 6827 kbps. Subtract audio bitrate (typically 128-192 kbps) from this to get video bitrate. Two-pass encoding ensures you hit the target size while maximizing quality within that constraint.
Does audio compression significantly affect overall file size?
Audio typically represents only 5-10% of total file size for video. Reducing audio from 192 kbps to 128 kbps might save 2-3% overall. Focus compression efforts on video first. However, for very short clips or audio-heavy content (podcasts with minimal video), audio bitrate matters more. For speech-only content, 96 kbps AAC or even 64 kbps is often acceptable and helps with file size.