Configure libx264 Reference Frames in FFmpeg
This article explains how to configure the reference frames count in
the FFmpeg libx264 encoder. You will learn the specific
command-line parameters to use, how reference frames impact both
compression efficiency and playback compatibility, and practical
examples to apply to your video encoding workflows.
To configure the number of reference frames in FFmpeg using the
libx264 encoder, you use the -refs option.
Reference frames (or “ref frames”) are the frames that the encoder looks
back to when using inter-frame compression to predict changes in the
video.
The -refs Command
Syntax
The basic syntax for setting reference frames is:
ffmpeg -i input.mp4 -c:v libx264 -refs 4 output.mp4In this command: * -c:v libx264 selects the H.264
encoder. * -refs 4 limits the maximum number of reference
frames to 4.
How Reference Frames Affect Your Video
Adjusting the reference frame count involves a trade-off between file size, encoding speed, and device compatibility:
- Compression Efficiency: Increasing the number of reference frames allows the encoder to find more similarities across different parts of the video, which usually reduces the final file size without losing quality.
- Encoding Speed: A higher number of reference frames requires the encoder to search through more frames, which increases encoding time.
- Playback Compatibility: Most hardware decoders (such as those in older smartphones, smart TVs, or streaming sticks) have strict limits on the number of reference frames they can decode. Setting this value too high may cause playback stutter or failure.
Recommended Values and Standards
For most web distribution and device compatibility, a reference frame count between 3 and 5 is ideal.
If you do not manually set the -refs parameter, FFmpeg’s
libx264 encoder will automatically determine the number of
reference frames based on the preset you choose (e.g.,
preset fast uses fewer reference frames, while
preset veryslow uses more).
Example: Compatibility-Focused Encoding (H.264 High Profile Level 4.1)
If you are targeting standard playback hardware (like Apple TV or older iPads), you should limit your reference frames to 4 for 1080p video:
ffmpeg -i input.mp4 -c:v libx264 -profile:v high -level 4.1 -refs 4 output.mp4Example: Low-Latency or Low-Power Encoding
For live streaming or scenarios where decoding needs to be as lightweight as possible, you can set the reference frames to 1:
ffmpeg -i input.mp4 -c:v libx264 -refs 1 output.mp4