How to Stream Video Over SRT Using FFmpeg
Secure Reliable Transport (SRT) is a modern video streaming protocol designed for low-latency, secure transmission over unpredictable networks like the public internet. This article provides a practical, step-by-step guide on how to configure and stream live video over SRT using the versatile command-line tool FFmpeg. You will learn the basic FFmpeg syntax for SRT streaming, how to configure caller and listener modes, and how to optimize your stream settings for maximum stability.
Prerequisites
To stream over SRT, you must ensure that your version of FFmpeg is compiled with SRT support. You can verify this by running the following command in your terminal:
ffmpeg -protocols | grep srtIf srt appears in the output under both input and output
protocols, your FFmpeg build is ready. If not, you will need to download
or compile a version of FFmpeg that includes the
--enable-libsrt configuration flag.
Understanding SRT Modes: Caller vs. Listener
Unlike traditional protocols, SRT requires one peer to act as a listener (waiting for an incoming connection) and the other to act as a caller (initiating the connection). It does not matter which side is sending the video; either the sender or the receiver can be the caller or the listener.
- Caller Mode: The default mode where FFmpeg initiates the connection to a specific IP address and port.
- Listener Mode: FFmpeg opens a port and waits for an incoming connection from a remote caller.
How to Stream using FFmpeg (Caller Mode)
In most live streaming scenarios, your FFmpeg encoder will act as the caller and stream to a remote server or ingest point (such as OBS, vMix, or a media server) acting as the listener.
Here is the standard command to encode a local video file and stream it over SRT:
ffmpeg -re -i input.mp4 -c:v libx264 -preset veryfast -b:v 3000k -maxrate 3000k -bufsize 6000k -pix_fmt yuv420p -g 50 -c:a aac -b:a 128k -f mpegts "srt://receiver-ip-address:9000?mode=caller"Command breakdown: * -re: Reads the
input file in real-time (essential when streaming files instead of live
feeds). * -i input.mp4: Specifies the input source. *
-c:v libx264 -preset veryfast: Encodes the video to H.264
using the “veryfast” preset to minimize CPU usage. *
-b:v 3000k -maxrate 3000k -bufsize 6000k: Sets a constant
bitrate (CBR) of 3 Mbps, which is ideal for streaming stability. *
-g 50: Sets the keyframe interval (GOP size). For a 25fps
video, 50 creates a keyframe every 2 seconds. *
-c:a aac -b:a 128k: Encodes the audio to AAC at 128 kbps. *
-f mpegts: Containers the stream in an MPEG-TS format,
which is required for SRT transmission. *
"srt://receiver-ip-address:9000?mode=caller": The
destination SRT URL. Replace receiver-ip-address with the
IP of your receiving device and 9000 with your target
port.
How to Stream using FFmpeg (Listener Mode)
If your receiving application is configured as a caller, you must set FFmpeg to run as a listener.
To stream a video file while listening for a incoming receiver connection, use the following command:
ffmpeg -re -i input.mp4 -c:v libx264 -preset veryfast -c:a aac -f mpegts "srt://0.0.0.0:9000?mode=listener"In this setup, 0.0.0.0 tells FFmpeg to bind to all
available network interfaces on port 9000 and wait for the
receiver to connect before it begins transmitting the stream.
Advanced SRT Parameters
FFmpeg allows you to pass specific SRT parameters directly inside the
connection string to optimize performance over unstable networks.
Multiple parameters can be appended using the &
separator.
1. Adjusting Latency
Latency is the buffer time (in microseconds) used to recover lost packets. The default is 120ms (120000 microseconds). For long-distance or unstable connections, you should increase this value (e.g., to 250ms):
"srt://receiver-ip-address:9000?mode=caller&latency=250000"2. Enabling Encryption
SRT supports AES encryption to secure your stream. To enable it, you
must define the passphrase and the key length (pbkeylen can
be 16, 24, or 32 bytes, corresponding to AES-128, AES-192, and
AES-256):
"srt://receiver-ip-address:9000?mode=caller&passphrase=YourSecretPassword&pbkeylen=16"3. Defining Stream ID
Some streaming servers and CDNs require a unique identifier (StreamID) to route your stream properly:
"srt://receiver-ip-address:9000?mode=caller&streamid=live/stream1"