Using ffmpeg to display a static image if an RTMP input source is missing
21:48 18 Mar 2016

Here is what I would like ffmpeg to output:

  • If I am streaming from my iPhone to my RTMP server, ffmpeg should output the live video from my iPhone.
  • If not, ffmpeg should output a blank red screen.

Here's what I have so far. It sort of works.

ffmpeg \
  -f lavfi \
  -re \
  -i 'color=s=320x240:r=30:c=red' \
  -thread_queue_size 512 \
  -i 'rtmp://localhost/stream/iphone' \
  -c:v libx264 \
  -f flv \
  -filter_complex "\
    [1:v]scale=320:240[stream]; \
    [0:v][stream]overlay=0:0:eof_action=pass[output] \
  "\
  -map '[output]' \
  -tune zerolatency \
  'rtmp://localhost/stream/output'

What happens: it boots up and starts streaming my iPhone's output no problem. When I disconnect, it hangs for a long time, perhaps 20 seconds. Then it starts outputting red, okay. But then if I reconnect my phone, it doesn't resume. It's still red. Two questions:

  • Is there a way to configure the buffering so that it starts outputting red as soon as it stops getting data from the RTMP stream?
  • Is there a way to have it auto-retry, so after the RTMP stream returns, it will switch back?

Full verbose output, if that's helpful. I'm using the latest git version of ffmpeg as of 2016-03-18 on Ubuntu Wily. The RTMP server is nginx-rtmp.

ffmpeg rtmp