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.mp4
Breakdown 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.mp4
For 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.mp4
Presets 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.mp4
Note 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.webm
Adaptive 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.mp4
This 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.mp4
Use -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.mp4
Reducing 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.mp4
Balancing 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.mp4
The -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.mp4
Low 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.mp4
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.