Configure RTMP Retry Intervals in FFmpeg

This guide explains how to manage connection drops and configure retry intervals when streaming RTMP with FFmpeg. While FFmpeg does not offer a native, automatic retry mechanism for RTMP publishing outputs, you can combine socket-level timeout parameters with external shell loops to achieve reliable reconnection intervals. This article covers the essential timeout flags, how to structure a robust retry loop, and alternative protocol configurations.

Understanding FFmpeg RTMP Connection Limits

When publishing an RTMP stream, FFmpeg acts as a client pushing data to a server. If the connection drops, FFmpeg terminates the process with an error by default. Unlike its HTTP input protocols—which feature native flags like -reconnect and -reconnect_delay_max—the native RTMP muxer lacks a built-in auto-reconnect switch for output streaming.

To create a resilient streaming pipeline, you must handle socket detection inside FFmpeg and manage reconnection intervals via a process supervisor or script.

Step 1: Configure Socket Detection Timeouts

Before setting a retry interval, ensure FFmpeg detects a broken connection quickly instead of hanging indefinitely. You can pass timeout options directly to the underlying TCP layer of the RTMP URL.

  • -rw_timeout: Sets the read/write timeout in microseconds. If no data is transferred within this window, FFmpeg exits immediately, triggering your retry mechanism.

Example syntax:

ffmpeg -re -i input.mp4 -c:v libx264 -c:a aac -f flv -rw_timeout 5000000 "rtmp://live.example.com/app/stream_key"

In this example, -rw_timeout 5000000 sets the timeout to 5 seconds (5,000,000 microseconds). If the RTMP socket stops acknowledging writes for 5 seconds, FFmpeg exits.

Step 2: Configure the Reconnection Loop and Interval

To enforce a specific retry interval, wrap the FFmpeg command in a continuous loop. If the process crashes or disconnects, the loop waits for a designated interval before attempting to reconnect.

Linux / macOS (Bash Script)

The following script continuously attempts to push a live source (such as a capture card or virtual device) and waits 5 seconds between reconnection attempts:

#!/bin/bash

RTMP_URL="rtmp://live.example.com/app/stream_key"
RETRY_INTERVAL=5

while true; do
    echo "[$(date)] Starting FFmpeg RTMP stream..."
    
    ffmpeg -re -i input.mp4 \
        -c:v libx264 -preset veryfast -b:v 3000k -maxrate 3000k -bufsize 6000k \
        -c:a aac -b:a 128k \
        -f flv -rw_timeout 5000000 "$RTMP_URL"
    
    echo "[$(date)] Stream disconnected. Retrying in $RETRY_INTERVAL seconds..."
    sleep $RETRY_INTERVAL
done

Windows (Batch Script)

On Windows, use timeout to define the reconnection interval:

@echo off
set RTMP_URL=rtmp://live.example.com/app/stream_key
set RETRY_INTERVAL=5

:loop
echo [%date% %time%] Starting FFmpeg RTMP stream...

ffmpeg -re -i input.mp4 ^
    -c:v libx264 -preset veryfast -b:v 3000k -maxrate 3000k -bufsize 6000k ^
    -c:a aac -b:a 128k ^
    -f flv -rw_timeout 5000000 "%RTMP_URL%"

echo [%date% %time%] Stream disconnected. Retrying in %RETRY_INTERVAL% seconds...
timeout /t %RETRY_INTERVAL% /nobreak >nul
goto loop

Handling Input RTMP Pull Reconnection

If you are consuming an RTMP stream as an input rather than publishing it, you can handle timeouts similarly using -rw_timeout before the input flag:

ffmpeg -rw_timeout 5000000 -i "rtmp://live.example.com/app/source_stream" -c copy output.mp4

For HTTP-based live sources (such as HTTP-FLV or HLS), FFmpeg does provide internal retry options:

  • -reconnect 1: Reconnects automatically if the TCP connection drops.
  • -reconnect_at_eof 1: Reconnects if the stream unexpectedly reaches end-of-file.
  • -reconnect_streamed 1: Reconnects even for non-seekable live streams.
  • -reconnect_delay_max <seconds>: Defines the maximum backoff interval between attempts.

Example for HTTP-wrapped live inputs:

ffmpeg -reconnect 1 -reconnect_at_eof 1 -reconnect_streamed 1 -reconnect_delay_max 5 -i "http://live.example.com/live/stream.flv" -c copy output.mp4