Set RTMP Timeout and Protocol in FFmpeg
Configuring network timeouts and transport options for RTMP streams in FFmpeg prevents processes from hanging during network drops and ensures compatibility with firewalls. This guide outlines how to adjust read/write timeouts, apply connection timeout limits, and specify the underlying transport protocol for RTMP in FFmpeg.
Setting the Connection and I/O Timeout
FFmpeg uses microsecond-based flags for managing network timeouts.
The primary parameter for RTMP read and write timeouts is
-rw_timeout.
To set a timeout for reading from or writing to an RTMP endpoint,
place -rw_timeout before the input (-i) or
before the output URL:
ffmpeg -rw_timeout 5000000 -i rtmp://example.com/live/stream -c copy output.mp4In this example, 5000000 microseconds equals 5 seconds.
If no data is received or acknowledged within 5 seconds, FFmpeg
terminates the connection instead of waiting indefinitely.
For output streams:
ffmpeg -re -i input.mp4 -c copy -f flv -rw_timeout 5000000 rtmp://example.com/live/streamURL-Based Timeout Parameters
You can also pass timeout parameters directly inside the RTMP URL
query string depending on whether your build uses native FFmpeg RTMP or
librtmp:
ffmpeg -i "rtmp://example.com/live/stream timeout=10" -c copy output.mp4Here, timeout=10 sets a 10-second timeout for the
initial socket connection.
Configuring the RTMP Transport Protocol
By default, standard RTMP operates directly over TCP on port 1935. To change how the stream is transported across the network, you modify the URL protocol scheme or specify tunneling protocols.
1. Standard TCP (Default)
Uses port 1935 over raw TCP:
rtmp://example.com/live/stream2. RTMPT (HTTP Tunneling)
Encapsulates RTMP packets inside HTTP requests, typically over port 80, bypassing firewalls that block standard RTMP:
rtmpt://example.com:80/live/stream3. RTMPS (RTMP over TLS/SSL)
Encrypts the RTMP stream using TLS over port 443:
rtmps://example.com:443/live/streamCombined Example
The following command pushes a local video file to a secure RTMP (RTMPS) server with a 5-second network timeout configured:
ffmpeg -re -i input.mp4 -c:v libx264 -c:a aac -f flv -rw_timeout 5000000 rtmps://live.example.com:443/app/stream_key