How to Show Fallback Image When FFmpeg Stream Disconnects
Maintaining a continuous live stream when the primary input source disconnects is crucial for a professional broadcasting setup. This article explains how to configure FFmpeg to automatically inject a static slate or fallback image when a live video source drops, ensuring your stream remains active and your viewers are not left with a blank screen or a dropped connection.
To achieve a seamless fallback transition in FFmpeg, the most reliable method is to loop a static background image and overlay the live stream on top of it. When the live stream disconnects, the overlay filter passes the background image through to the output while FFmpeg attempts to reconnect to the source.
The FFmpeg Command
Use the following command structure to set up the fallback mechanism:
ffmpeg -loop 1 -f image2 -i slate.png \
-reconnect 1 -reconnect_at_eof 1 -reconnect_streamed 1 -reconnect_delay_max 5 \
-i rtmp://example.com/live/stream \
-filter_complex "[0:v][1:v] overlay=eof_action=repeat [outv]" \
-map "[outv]" -map 1:a? -c:v libx264 -c:a aac -f flv rtmp://example.com/live/destinationHow It Works
-loop 1 -f image2 -i slate.png: This inputs your fallback image (slate.png) and loops it infinitely. It acts as the bottom layer of your video.- Reconnect Flags: The flags
-reconnect 1 -reconnect_at_eof 1 -reconnect_streamed 1 -reconnect_delay_max 5tell FFmpeg to actively attempt reconnection if the live stream input drops, rather than shutting down the entire process. -i rtmp://example.com/live/stream: This is your primary live video source.overlay=eof_action=repeat: Theoverlayfilter places the live stream (input 1) over the fallback image (input 0). Settingeof_action=repeatensures that if the live stream reaches End-Of-File (disconnects), FFmpeg continues to output the underlying looped image while waiting for the input stream to recover.-map 1:a?: The question mark tells FFmpeg that the audio stream is optional. If the live stream disconnects, the audio stream will temporarily disappear without crashing the FFmpeg process.
This setup ensures that your destination RTMP server receives a continuous video stream (the slate image) even when the source input experiences packet loss or a complete disconnect.