FFmpeg x264 Low Latency Preset and Tuning Guide

This article provides a direct guide on configuring FFmpeg's libx264 encoder to achieve the lowest possible latency for live streaming. You will learn the exact preset, tuning, and advanced command-line parameters required to eliminate encoding delays, making your stream ideal for real-time interactive applications.

The Core Settings: Preset and Tune

To achieve near-zero encoding latency, you must use the correct combination of the -preset and -tune options in your FFmpeg command.

  • -preset ultrafast: The preset determines the trade-off between encoding speed and compression efficiency. The ultrafast preset disables heavy encoding tools, resulting in the fastest possible encoding speed and the lowest CPU-induced delay.
  • -tune zerolatency: This is the most critical option for live streaming. It forces the encoder to output frames immediately without buffering. It disables B-frames (bi-directional predictive frames), reduces the rate-control lookahead to zero, and forces instant frame skipping when necessary.

Below is an optimized FFmpeg command template for low-latency live streaming:

ffmpeg -f dshow -i video="Camera" -c:v libx264 -preset ultrafast -tune zerolatency -g 30 -bf 0 -nal-hrd cbr -b:v 2000k -maxrate 2000k -bufsize 1000k -an -f mpegts udp://127.0.0.1:1234

Critical Parameters Explained

To fine-tune your stream further, understand how these specific parameters impact latency:

1. B-Frames (-bf 0)

B-frames require the encoder to look ahead at future frames to compress the current one, which introduces a delay of several frames. Setting -bf 0 disables B-frames entirely, ensuring frames are encoded and sent sequentially. (Note: -tune zerolatency sets this automatically, but declaring it explicitly guarantees enforcement).

2. GOP Size / Keyframe Interval (-g)

The -g option sets the Group of Pictures (GOP) size, which dictates how often an IDR (intra-coded) keyframe is sent. For low latency, set the GOP size to match your frame rate (e.g., -g 30 for a 30 fps stream) or twice the frame rate. This ensures a keyframe is sent every 1 to 2 seconds, allowing players to connect and decode the stream quickly without waiting.

3. Rate Control and Buffer Size (-nal-hrd cbr, -bufsize)

Strict Constant Bitrate (CBR) prevents bitrate spikes that cause network congestion and playback buffering.

  • -b:v & -maxrate: Set these to the same target bitrate (e.g., 2000k).
  • -bufsize: This controls the receiver's buffer size. For low latency, set the buffer size to half of your target bitrate (e.g., -bufsize 1000k for a 2000k stream). A smaller buffer forces the encoder to adhere strictly to the target bitrate frame-by-frame, preventing latency buildup.
  • -nal-hrd cbr: Signals HRD (Hypothetical Reference Decoder) compatibility, forcing CBR compliance on the stream level.

Network Protocol Selection

Even with optimized x264 settings, your choice of streaming protocol heavily impacts latency. Avoid segment-based protocols like HLS or DASH. Instead, pair your optimized x264 configuration with low-latency push protocols such as SRT (Secure Reliable Transport), WebRTC, or RTSP/RTMP over UDP.