CRF gives you a quality target but not a size target. Two-pass encoding does the opposite: you specify the average bitrate you want, and the encoder uses the first pass to analyze the content so the second pass can allocate bits optimally. It is the right tool when you need a specific file size or must fit a delivery spec.

The command

ffmpeg -i input.mp4 -c:v libx264 -b:v 2M -pass 1 -an -f null /dev/null && ffmpeg -i input.mp4 -c:v libx264 -b:v 2M -pass 2 -c:a aac -b:a 128k output.mp4

What it does

Pass 1 decodes the input, analyzes every frame, and writes a log file (default name ffmpeg2pass-0.log) describing the complexity of each segment. Pass 2 reads that log and encodes with the target bitrate, allocating more bits to complex segments and fewer to simple ones. The result is a file whose average bitrate is close to the target and whose quality is constant across the timeline.

When to use it

For delivery specs that require a specific size: broadcast, streaming at a target bitrate, fitting on a fixed-capacity medium. Two-pass is roughly twice as slow as single-pass, so use it only when you need the size guarantee.

Video and audio requirements

Any decodable source. The -b:v 2M is the target bitrate; pick it to match your delivery. For constant-quality with a size ceiling, use constrained CRF: -crf 23 -maxrate 2M -bufsize 4M, which targets a quality but caps the bitrate per chunk.

Things to avoid

  • Running pass 1 and pass 2 in different directories. The log file is written in the current directory, and pass 2 reads it from the current directory. If the working directory changes, pass 2 errors.
  • Forgetting -an on pass 1. Pass 1 should not produce audio output; the -f null - discards the video too. Including audio wastes CPU and risks an output file with silent audio.
  • Choosing a target bitrate that is too low. The encoder will compress aggressively and produce visible artifacts. 1.5 to 2 Mbps for 1080p H.264 is the practical minimum for acceptable delivery quality.
  • Assuming two-pass is always better than CRF. For a single target, CRF is faster and equivalent in quality. Two-pass is for when you must hit a specific bitrate.