Ch. 15 · Low-Latency and Real-Time Streaming

한국어日本語
9 min

Live Encoding Settings: zerolatency and Keyframe Interval

The settings that stop the encoder from waiting on the future, and why the keyframe interval should match the segment length.

Two things already came up in Chapters 3 and 10. B-frames reference future frames, which adds latency (Inter prediction), and the keyframe interval has to line up with segment boundaries (GOP structure). This lesson covers those two along with the other settings you actually touch in live encoding.

x264's -tune zerolatency

In x264 (libx264), -tune zerolatency is a bundle that changes several options at once so the encoder emits each frame as soon as it receives it. According to the x264 help, it is known to include turning off B-frames, turning off lookahead, and using slice-based threading instead of frame-based threading. The exact contents can differ between versions, so check ffmpeg -h encoder=libx264 and the x264 documentation.

What creates latencyWhy it adds latencyWhat zerolatency does (in short)
B-framesFuture frames must be encoded before the frames can be put in orderDoesn't use B-frames
Lookahead (rc-lookahead)Analyzes upcoming frames in advance to allocate bitsReduces or turns off lookahead
Frame threadingSeveral frames are processed in parallel, so output gets delayedParallelizes within a single frame using slice threading

The cost: the same quality takes more bits. B-frames and lookahead in particular are a major pillar of compression efficiency.

The preset affects latency too

The slower the preset, the more computation each frame gets, and if the encoder can't keep up with real time, frames simply fall behind. In live work, "can you sustain real-time speed" comes before "latency". If encoding speed falls short of the input frame rate, no tuning helps, so pick a preset that reaches speed=1x or higher on your hardware (What a preset really is). Hardware encoders are an option for taking that load off (Chapter 13).

Keyframe interval: frames versus seconds

In live work the keyframe interval is usually set in seconds and then converted to a frame count. Interval (seconds) × frame rate = the -g value. For example, a 2-second interval is -g 60 at 30fps and -g 120 at 60fps. The segment length must be an integer multiple of this interval so that each segment starts with a keyframe.

Example x264 encode for live streaming (30fps, keyframe fixed at 2 seconds, input is a placeholder)
ffmpeg -i INPUT \
  -c:v libx264 -preset veryfast -tune zerolatency \
  -b:v 3000k -maxrate 3000k -bufsize 6000k \
  -g 60 -keyint_min 60 -sc_threshold 0 \
  -c:a aac -b:a 128k \
  -f flv rtmp://SERVER/APP/STREAM_KEY
  • -g 60 -keyint_min 60 — fixes the keyframe interval at 60 frames (2 seconds at 30fps).
  • -sc_threshold 0 — turns off scene-change detection, which would otherwise insert extra keyframes. Leave it on and the keyframe spacing becomes uneven, which can make segment lengths wobble.
  • -maxrate and -bufsize — limit how far the instantaneous bitrate can spike (VBV). A safety measure to keep you from exceeding the line's capacity.
  • -f flv rtmp://… — RTMP streaming is sent in an FLV container. Follow your service's instructions for the server address and stream key.
Why you run close to CBR

A live line has a fixed capacity, so if the bitrate swings around it will momentarily overflow and the stream stalls. That is why it is common to set -b:v equal to -maxrate and use -bufsize to set the allowed swing, running close to CBR. Remember that the goal differs from CRF for file encoding (CQP, CRF, and ABR): CRF holds quality constant, while for live the priority is a constant bitrate. Note that the larger you make -bufsize, the more momentary variation it absorbs, but the latency and the size of the swings grow.

Field note: line up encoder → packager → player as one set

If the encoder's keyframe interval, the packager's segment (part) length, and the player's target latency don't match each other, your low-latency settings won't do their job. Don't change just one place; write the numbers from all three in the same table and adjust them together. The keyframe interval and bitrate cap a platform requires for ingest differ by service, so always check that platform's official guidance.

zerolatencyzerolatency

A bundle of tuning options in x264 and similar encoders that turns off B-frames, lookahead, and so on to cut the encoder's own latency.

LookaheadLookahead

An encoder feature that analyzes upcoming frames in advance to allocate bits. It improves compression efficiency but adds latency.

VBVVideo buffering verifier

A decoder buffer model used to limit the instantaneous bitrate. ffmpeg's -maxrate/-bufsize correspond to it.

Scene cutScene cut

An encoder feature that inserts an extra keyframe when the scene changes. Turn it off if you need a fixed GOP.

Check yourself

What -g value fixes the keyframe interval at 2 seconds for 30fps live video?

Check yourself

Which pairing correctly states the benefit and cost of -tune zerolatency?