Low Latency RTSP Streaming with FFmpeg
Streaming live video over RTSP (Real-Time Streaming Protocol) with minimal delay is crucial for applications like security surveillance, live broadcasting, and interactive video feeds. This guide provides a straightforward, step-by-step approach to configuring FFmpeg for low-latency RTSP streaming, highlighting the essential command-line arguments and optimization techniques needed to reduce lag to sub-second levels.
To achieve ultra-low latency, you must optimize both the encoding process and the transport settings. FFmpeg allows you to bypass default buffering behaviors and configure the video encoder specifically for real-time delivery.
The Low-Latency FFmpeg Command
Below is a highly optimized FFmpeg command template designed to capture a video input (such as a local camera) and stream it to an RTSP server with minimal delay:
ffmpeg -f dshow -i video="Integrated Camera" -vcodec libx264 -preset ultrafast -tune zerolatency -fflags nobuffer -flags low_delay -g 30 -an -f rtsp -rtsp_transport tcp rtsp://localhost:8554/live(Note: Replace -f dshow -i video="Integrated Camera"
with your specific input source, such as /dev/video0 on
Linux or an existing media file).
Key Parameter Breakdown
To understand how this command minimizes latency, here is an explanation of the critical parameters used:
-preset ultrafast: Instructs the H.264 encoder to use the fastest possible encoding algorithms. While this slightly reduces compression efficiency, it drastically cuts down encoding time.-tune zerolatency: A crucial setting for H.264 (libx264) that disables frame reordering (B-frames). B-frames require future frames to be encoded first, which introduces inherent delay. Removing them ensures frames are sent as soon as they are processed.-fflags nobuffer: Prevents FFmpeg from buffering input and output packets. This forces immediate transmission of the stream.-flags low_delay: Forces the muxer to write packets immediately to the network socket, bypassing internal queues that would otherwise smooth out network spikes at the cost of latency.-g 30: Sets the Group of Pictures (GOP) size to 30. A shorter GOP ensures that keyframes (I-frames) are sent frequently, allowing clients to decode and display the stream almost instantly upon connecting.-rtsp_transport tcp: Forces the use of TCP for the RTSP stream. While UDP can sometimes be faster, packet loss over UDP can cause severe visual artifacts and decoding delays. TCP ensures reliable, sequential packet delivery, which is often more stable for low-latency setups in controlled networks. If you have a highly reliable network, you can switch this toudp.
Reducing Latency on the Receiver Side
A low-latency stream is only as good as the player rendering it. Many
media players (like VLC) buffer several seconds of video by default to
prevent stuttering. To view your RTSP stream with minimal latency, use
ffplay with buffering disabled:
ffplay -fflags nobuffer -flags low_delay -rtsp_transport tcp rtsp://localhost:8554/liveBy combining these sender-side FFmpeg configurations with a zero-buffer receiver, you can successfully achieve sub-second latency over RTSP.