FFmpeg recipe
FFmpeg: Compress an MP4 to a Target File Size
When you must hit a specific size (e.g. 25 MB for email), two-pass bitrate encoding is the way. Calculate target bitrate, then run two passes.
Command
# duration=60s, target=24MB → bitrate ≈ 24*8/60 = 3200 kbps total
ffmpeg -y -i input.mp4 -c:v libx264 -b:v 3000k -pass 1 -an -f mp4 /dev/null && \
ffmpeg -i input.mp4 -c:v libx264 -b:v 3000k -pass 2 -c:a aac -b:a 128k -y output.mp4Prefer no terminal?
Use the Video Compressor in your browser
What each flag does
| -b:v | Target video bitrate. Computed from desired size and duration: bitrate_kbps ≈ size_MB × 8000 / duration_seconds. |
|---|---|
| -pass 1 | First pass: analyze the video to build a bitrate distribution map. No output written. |
| -pass 2 | Second pass: encode using the map for optimal bit allocation. |
| -an | No audio in pass 1 to save time. |
Notes & gotchas
- Two-pass is ~2x slower than single-pass CRF but hits target sizes accurately.
- On Windows, use NUL instead of /dev/null in pass 1.
Related recipes
Compress (H.264) · MP4
Compress an MP4 with FFmpeg using H.264 + CRF. Quality-first, content-aware compression with copy-paste command.
Compress (HEVC) · MP4
Compress an MP4 with FFmpeg using H.265 (HEVC). ~50% smaller at equivalent quality vs H.264.
Trim · MP4
Trim an MP4 with FFmpeg using stream-copy for lossless, instant output. Copy-paste command with explanations.
Trim (frame-accurate) · MP4
Frame-accurate MP4 trim with FFmpeg. Re-encodes for exact-frame precision — use when -c copy keyframe snapping is not OK.