How to Use FFmpeg -refs Option for Reference Frames

This article explains how to use the -refs option in FFmpeg to specify the number of reference frames during video encoding. You will learn the correct command syntax, see practical examples, and understand how adjusting the number of reference frames affects your video’s compression efficiency, encoding speed, and playback compatibility.

Understanding the -refs Option

In video compression, reference frames are previously decoded frames that the encoder uses to predict the motion and detail in subsequent frames (P-frames and B-frames). By increasing the number of reference frames, the encoder has more options to find matching blocks, which generally improves compression efficiency and reduces file size at the cost of higher CPU usage and playback complexity.

The -refs option in FFmpeg allows you to set the maximum number of reference frames. It is typically used with video encoders like libx264 (H.264) and libx265 (H.265/HEVC).

Command Syntax and Examples

To specify the number of reference frames, place the -refs option after you define your video codec.

Basic Example (H.264)

To encode a video using H.264 with exactly 5 reference frames, use the following command:

ffmpeg -i input.mp4 -c:v libx264 -refs 5 output.mp4

High-Compression Example (H.265)

For H.265 encoding with 8 reference frames for maximum compression efficiency:

ffmpeg -i input.mp4 -c:v libx265 -refs 8 output.mp4

Key Considerations When Choosing Reference Frames