How to Use FFmpeg Tune Zerolatency
This article explains how to use the -tune zerolatency
option in FFmpeg to achieve real-time video streaming with minimal
delay. It covers the exact command-line syntax and details the specific
encoder parameters modified by this setting to eliminate frame
buffering.
Using -tune zerolatency in FFmpeg
The -tune zerolatency option is a preset modifier used
primarily with the H.264 (libx264) and H.265
(libx265) encoders. It optimizes the encoding pipeline for
live interactive use cases—such as video conferencing, remote desktop
streaming, or live gameplay broadcasting—where delay between capture and
playback must be minimized.
To apply this option in your FFmpeg command, insert
-tune zerolatency after specifying your video encoder:
ffmpeg -i input.mp4 -c:v libx264 -preset fast -tune zerolatency -f mpegts udp://127.0.0.1:1234Parameters Modified by zerolatency
When you enable -tune zerolatency, FFmpeg passes
specific internal configuration flags to the underlying encoder. These
changes disable features that require buffering future frames, allowing
the encoder to compress and output frames immediately.
The primary encoder parameters modified by this setting include:
1. B-Frames (bframes = 0)
By default, video encoders use B-frames (bi-directional predictive
frames), which require the encoder to analyze future frames to calculate
compression. This introduces a multi-frame processing delay. The
zerolatency tune sets the number of consecutive B-frames to
0, ensuring only I-frames (keyframes) and P-frames
(predicted frames) are used.
2. Rate Control Lookahead
(rc-lookahead = 0)
Normally, encoders analyze a buffer of upcoming frames (often 40 to
60 frames) to distribute bitrate efficiently and improve visual quality.
This buffer causes a latency equal to the buffer size. Under
zerolatency, rc-lookahead is disabled and set
to 0, forcing the encoder to make rate control decisions
instantly for each frame.
3. Macroblock Tree Rate
Control (mbtree = 0)
Macroblock Tree (MB-tree) rate control tracks temporal propagation of
data across frames to optimize compression quality. Because MB-tree
relies heavily on the lookahead buffer, it is disabled when using the
zerolatency option.
4. Sliced Threading
(sliced-threads = 1 / enabled)
Standard frame-based threading encodes multiple frames in parallel,
which increases overall throughput but introduces latency equal to the
number of threads. The zerolatency option switches the
encoder to sliced threading. This method parallelizes the encoding of a
single frame across multiple CPU threads by dividing it into horizontal
slices, ensuring that each individual frame is processed and output as
fast as possible.
5. Weighted
Prediction for P-Frames (weightp = 0)
Weighted prediction improves compression during fades and lighting
changes by analyzing reference frames. Because this analysis requires
looking ahead at upcoming frames, weightp is disabled to
prevent latency.
By modifying these parameters, -tune zerolatency
guarantees that the encoder outputs a packet of data as soon as a frame
is input, achieving immediate delivery at the cost of slightly lower
compression efficiency.