DEV Community

GoToolsOnline
GoToolsOnline

Posted on

Hitting a target file size when compressing video: the bitrate math

"Make this video under 25 MB" sounds simple, but quality sliders (CRF) don't let you aim at a size. Size is decided by bitrate × duration, so to hit a target you work backwards from the size.

The formula

total_kbps  = target_MB * 8 * 1024 / duration_seconds
video_kbps  = total_kbps * 0.95 - audio_kbps
Enter fullscreen mode Exit fullscreen mode

The * 0.95 leaves ~5% headroom for the MP4 container and encoder overshoot. Example: 25 MB, 120 s clip, 96 kbps AAC audio:

25 * 8 * 1024 / 120 = 1706 kbps total
1706 * 0.95 - 96   ≈ 1525 kbps for video
Enter fullscreen mode Exit fullscreen mode

Two-pass encoding

Single-pass bitrate mode tends to overshoot or waste bits. Two-pass lets the encoder see the whole clip first:

ffmpeg -y -i in.mp4 -c:v libx264 -b:v 1525k -preset medium -pass 1 -an -f mp4 /dev/null
ffmpeg -i in.mp4 -c:v libx264 -b:v 1525k -preset medium -pass 2 \
  -c:a aac -b:a 96k -movflags +faststart out.mp4
Enter fullscreen mode Exit fullscreen mode

When the bitrate gets too low

If video_kbps drops below roughly 600–800 kbps for 1080p, it will look blocky. Better options than starving the encoder:

  • Scale down: -vf scale=-2:720 (720p needs far fewer bits).
  • Cap frame rate: -r 30 for 60 fps phone footage.
  • Mono / lower audio: -ac 1 -b:a 64k for speech, or -an if there's no useful sound.

Gotchas

  • Phone videos carry a rotation flag; modern ffmpeg applies it automatically, but check the output on a phone.
  • HDR (HLG/PQ) iPhone clips look washed out on SDR screens unless tone-mapped.
  • Always check the final size and refuse/retry if it's over the target; that 5% headroom isn't a guarantee.

Disclosure: I run GoToolsOnline. Its free video compressor does this target-size math for you (8/10/16/20/25/50 MB or custom) with resolution, frame-rate and audio options: https://gotoolsonline.com/tools/video-compress

Top comments (0)