FFmpeg Low Latency RTMP Screen Capture Guide
Streaming your desktop screen with sub-second latency using RTMP and FFmpeg requires precise configuration of capture parameters, encoder settings, and network buffers. This guide provides a direct, highly optimized FFmpeg configuration designed to eliminate transmission delays, allowing you to achieve near-real-time performance for your live desktop streams.
The Optimized FFmpeg Command
To minimize latency under one second, you must capture the screen efficiently, use a fast encoding preset, disable frame reordering, and constrain the encoder’s buffer.
Here is the recommended command for Windows (using
gdigrab):
ffmpeg -f gdigrab -framerate 30 -i desktop -c:v libx264 -preset ultrafast -tune zerolatency -pix_fmt yuv420p -g 30 -bf 0 -b:v 2500k -maxrate 2500k -bufsize 500k -f flv rtmp://your-rtmp-server/live/stream-keyFor Linux (using x11grab), use this version:
ffmpeg -f x11grab -framerate 30 -video_size 1920x1080 -i :0.0 -c:v libx264 -preset ultrafast -tune zerolatency -pix_fmt yuv420p -g 30 -bf 0 -b:v 2500k -maxrate 2500k -bufsize 500k -f flv rtmp://your-rtmp-server/live/stream-keyParameter Breakdown for Ultra-Low Latency
To understand why this command achieves sub-second latency, here is a breakdown of the critical parameters used:
-preset ultrafast: This tells the H.264 encoder to prioritize encoding speed over compression efficiency. It reduces the CPU time required to encode each frame to almost zero.-tune zerolatency: This is the most crucial flag for low latency. It disables frame buffering inside the encoder, forcing FFmpeg to output encoded packets immediately after receiving input frames.-bf 0: Disables B-frames (bi-directional predictive frames). B-frames require the encoder to look ahead at future frames, which introduces an inherent mathematical delay. Setting this to0ensures frames are processed sequentially without delay.-g 30: Sets the Group of Pictures (GOP) size (keyframe interval) to 30. At 30 frames per second, this sends a keyframe every second. Frequent keyframes help the player recover quickly from network jitter and start the stream faster.-b:v 2500k -maxrate 2500k -bufsize 500k: This enforces strict Constant Bitrate (CBR) encoding. By setting the buffer size (-bufsize) to a fraction of the bitrate (e.g., 500k for a 2500k stream), you prevent the encoder from buffering large chunks of data before transmission.-pix_fmt yuv420p: Ensures maximum compatibility with video players and RTMP ingestion servers, preventing decoding delays on the receiver end.
Server and Player Considerations
Optimizing the FFmpeg encoder is only half of the equation; the receiving server and media player must also be optimized for low latency:
- Disable Player Buffering: Standard players like VLC
or default web players often buffer 1 to 5 seconds of video to prevent
stuttering. If you are playing the stream in VLC, use the argument
--network-caching=100to reduce the player’s internal buffer. - Use Low-Latency Players: For web-based playback,
use a library specifically configured for low latency, such as
hls.jswith low-latency settings, or ingest the RTMP stream into a server that transmuxes to WebRTC or low-latency HTTP-FLV.