restart encoding with ffmpeg when bad source I have an ffmpeg process which saves a stream from wowza server, but when the stream in wowza is interrupted for a second or so sometimes ffmpeg cycles in:

[segment @ 0x671a10] st:0 PTS: 9600015 DTS: 9600015 &lt; 9629949 invalid, clipping
[segment @ 0x671a10] st:0 PTS: 9601935 DTS: 9601935 &lt; 9629950 invalid, clipping
[segment @ 0x671a10] st:0 PTS: 9603855 DTS: 9603855 &lt; 9629951 invalid, clipping
[segment @ 0x671a10] st:0 PTS: 9605775 DTS: 9605775 &lt; 9629952 invalid, clipping
[segment @ 0x671a10] st:0 PTS: 9607695 DTS: 9607695 &lt; 9629953 invalid, clipping
[segment @ 0x671a10] st:0 PTS: 9609615 DTS: 9609615 &lt; 9629954 invalid, clipping
[segment @ 0x671a10] st:0 PTS: 9611535 DTS: 9611535 &lt; 9629955 invalid, clipping
[segment @ 0x671a10] st:0 PTS: 9613455 DTS: 9613455 &lt; 9629956 invalid, clipping
[segment @ 0x671a10] st:0 PTS: 9615375 DTS: 9615375 &lt; 9629957 invalid, clipping
[segment @ 0x671a10] st:0 PTS: 9632160 DTS: 9614160 &lt; 9649231 invalid, clipping
[segment @ 0x671a10] st:0 PTS: 9624960 DTS: 9617760 &lt; 9649232 invalid, clipping
[segment @ 0x671a10] st:0 PTS: 9621360 DTS: 9621360 &lt; 9649233 invalid, clipping


It never survives, even when the source is healthy and missed only a few seconds. It would be great if the ffmpeg can exit or restart (It is enough to exit on error. I can execute it in an endless while.) My command is:

ffmpeg -shortest -re -y -err_detect aggressive -i rtmp://127.0.0.1/movies/my.stream timeout=10 -codec copy -map 0 -bsf:v h264_mp4toannexb,dump_extra -f segment -segment_time 60 /video/my.stream#%09d.ts


I have tested with -err_detect explode/careful/compliant/aggressive and on error in the input stream ffmpeg did not stop.  Sometimes it gets a timeout (because the timeout option I set for the rtmp), but some time it keep printing:

[segment @ 0x671a10] st:0 PTS: 9621360 DTS: 9621360 &lt; 9649233 invalid, clipping


It never survives - no exits, no output, so I have no saved stream until I manually restart the ffmpeg!
Although I am not sure if stderr will really help here because of the way that ffmpeg does its reporting, I can recommend the following pattern.

Use the -progress [url] flag and parse it with the tool of your choice every 100ms or so. Here is something to get you started that (with a little tweaking) might do what you want:

#!/bin/bash

touch /tmp/ffmpeg.log

ffmpeg -shortest -re -y -err_detect aggressive -i \ 
rtmp://127.0.0.1/movies/my.stream timeout=10 -codec copy \
-map 0 -bsf:v h264_mp4toannexb,dump_extra -f segment \
-segment_time 60 -progress /tmp/ffmpeg.log /video/my.stream#%09d.ts &amp;

pid=$!

while true
    do
         # you'll might have to change "invalid" 
         # for whatever is being put into the progress log
         # just look at /tmp/ffmpeg.log to figure out what
         # grep should try to find 
         error=$(grep -n "invalid" /tmp/ffmpeg.log)

         # is the length of the error more than 0 / did grep find it???
         if (( ${#error} &gt; 0 ))
             then
                 kill -9 $pid
                 ffmpeg -shortest -re -y -err_detect aggressive -i \ 
                 rtmp://127.0.0.1/movies/my.stream timeout=10 -codec copy \
                 -map 0 -bsf:v h264_mp4toannexb,dump_extra -f segment \
                 -segment_time 60 -progress /tmp/ffmpeg.log /video/my.stream#%09d.ts &amp;
             pid=$!
         else
             usleep 100
         fi
    done
exit 0