How to Do Two-Pass Encoding in FFmpeg

Two-pass encoding is a method used in video compression to achieve the highest possible quality for a specific target file size or average bitrate. This article provides a straightforward, step-by-step guide on how to perform a true two-pass bitrate control encoding using FFmpeg, detailing the exact commands and parameters needed for both the analysis and encoding phases.

Why Use Two-Pass Encoding?

In a single-pass encoding workflow, the encoder must make decisions on the fly without knowing what scenes lie ahead. Two-pass encoding solves this by splitting the process into two stages:

  1. Pass 1: FFmpeg analyzes the entire video to assess scene complexity (identifying fast motion, static scenes, and detail levels) and writes this data to a log file.
  2. Pass 2: FFmpeg reads the log file and allocates the available bitrate dynamically—using more bits for complex scenes and saving bits on simple scenes—resulting in optimal visual quality.

Step-by-Step Two-Pass Commands

To perform two-pass encoding (using the standard libx264 video codec as an example), you must run two separate FFmpeg commands in sequence.

Step 1: The First Pass (Analysis)

Run the following command to analyze the video and generate the log file.

For Linux and macOS:

ffmpeg -y -i input.mp4 -c:v libx264 -b:v 2000k -pass 1 -an -f null /dev/null

For Windows (Command Prompt):

ffmpeg -y -i input.mp4 -c:v libx264 -b:v 2000k -pass 1 -an -f null NUL

Parameter Breakdown:

  • -y: Overwrites existing output files automatically.
  • -i input.mp4: Specifies the input video file.
  • -c:v libx264: Selects the H.264 video encoder.
  • -b:v 2000k: Sets the target average video bitrate (e.g., 2000 kbps).
  • -pass 1: Tells FFmpeg to perform the first pass and write the analysis to a log file (default name is ffmpeg2pass-0.log).
  • -an: Disables audio processing. Audio is ignored in the first pass to speed up the analysis.
  • -f null /dev/null (or NUL): Discards the video output. No actual video file is written during this pass, only the log file.

Step 2: The Second Pass (Encoding)

Run the second command to read the generated log file, compress the video, process the audio, and output the final file.

ffmpeg -i input.mp4 -c:v libx264 -b:v 2000k -pass 2 -c:a aac -b:a 128k output.mp4

Parameter Breakdown:

  • -pass 2: Tells FFmpeg to read the log file generated in Step 1 and apply the bitrate allocation.
  • -c:a aac: Selects the AAC audio encoder.
  • -b:a 128k: Sets the target audio bitrate to 128 kbps.
  • output.mp4: The final, compressed output file.

Essential Rules for Two-Pass Encoding

For two-pass encoding to work correctly, you must adhere to these rules:

  • Keep Settings Identical: Video-specific parameters such as resolution (-s), frame rate (-r), aspect ratio, and video bitrate (-b:v) must be identical in both Pass 1 and Pass 2 commands. Changing these between passes will corrupt the log file data and cause the encoding to fail or produce poor results.
  • Log Files: FFmpeg automatically generates files named ffmpeg2pass-0.log (and potentially ffmpeg2pass-0.log.mbtree) in your working directory. Once the second pass is complete, you can safely delete these files.