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.mp4High-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.mp4Key Considerations When Choosing Reference Frames
- Default Values: If you do not specify the
-refsoption, FFmpeg’s encoders will use their default presets. Forlibx264, the default is typically 3 reference frames (depending on the preset). - Playback Compatibility: Older hardware players,
mobile devices, and smart TVs have strict hardware limits. Setting
-refstoo high (e.g., above 4 or 5 for 1080p video) may violate specific H.264 profiles (like High Profile @ Level 4.1) and cause playback to stutter or fail on legacy devices. - Diminishing Returns: Increasing the reference frame count beyond 5 or 6 rarely yields significant file size savings, but it will significantly increase the time and processing power required to encode the video.