C.W.K.
Stream
Lesson 06 of 10 · published

Two-Pass Encoding — Hitting an Exact File Size

~12 min · two-pass, bitrate, size-target

Level 0Viewer
0 XP0/73 lessons0/15 achievements
0/100 XP to next level100 XP to go0% complete

When you need a precise file size

Single-pass CRF gives you a quality target with unpredictable file size. Single-pass bitrate gives you a target bitrate but the encoder allocates bits naively across the timeline (boring stretches get the same as action sequences). Two-pass gives you a target file size and intelligent bit allocation — pass 1 analyzes the whole clip, pass 2 encodes with that map.

Use two-pass when: uploading to a service with a hard size cap (Discord 25 MB, old social media), DVD/Blu-ray authoring, archival where you've decided 'this 2-hour show fits in 4 GB'.

The math

Target bitrate (kbps) = (target size in MB × 8192) ÷ duration in seconds − audio bitrate. Example: 25 MB cap on a 5-minute clip with 128 kbps audio: (25 × 8192) ÷ 300 − 128 ≈ 555 kbps video.

Code

Two-pass libx264·bash
# Pass 1 — analysis only, no audio, output to /dev/null
ffmpeg -y -i in.mp4 \
  -c:v libx264 -b:v 555k -pass 1 -preset slow \
  -an -f null /dev/null

# Pass 2 — actual encode using the analysis from pass 1
ffmpeg -i in.mp4 \
  -c:v libx264 -b:v 555k -pass 2 -preset slow \
  -c:a aac -b:a 128k -movflags +faststart \
  out_25mb.mp4

# Pass 1 writes ffmpeg2pass-0.log; pass 2 reads it.
# Clean up after: rm ffmpeg2pass-*
Discord-friendly preset·bash
# Target ≤ 25 MB for Discord (free tier). Adjust duration/audio rate as needed.
DURATION=$(ffprobe -v error -show_entries format=duration \
  -of default=noprint_wrappers=1:nokey=1 in.mp4)
TARGET_KBPS=$(awk -v d="$DURATION" 'BEGIN { printf "%d", (25*8192/d)-128 }')
echo "Target video bitrate: ${TARGET_KBPS}k"

ffmpeg -y -i in.mp4 -c:v libx264 -b:v ${TARGET_KBPS}k -pass 1 \
  -preset slow -an -f null /dev/null
ffmpeg -i in.mp4 -c:v libx264 -b:v ${TARGET_KBPS}k -pass 2 \
  -preset slow -c:a aac -b:a 128k discord.mp4

External links

Exercise

Take a 5-minute clip. Single-pass: libx264 -b:v 1M. Two-pass at the same bitrate. Note the file sizes (close to target?) and visually inspect a high-motion section in both — does two-pass distribute bits more evenly? Then redo at 500 kbps and see whether two-pass is more dramatic at low bitrates.

Progress

Progress is local-only — sign in to sync across devices.
Spotted a bug or have feedback on this page?Report an Issue

Comments 0

🔔 Reply notifications (sign in)
Sign inPlease sign in to comment.

No comments yet — be the first.