Answers.Body: <p>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.</p>
  
  <p>Use the <code>-progress [url]</code> 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:</p>
  
  <pre><code>#!/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
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-08-27T13:13:34.157Z
Answers.LastActivityDate: 2014-08-28T10:07:11.283Z
Answers.LastEditDate: 2014-08-28T10:07:11.283Z
Answers.UserDisplayName: denjello
Body: <p>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:</p>
  
  <pre><code>[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
  </code></pre>
  
  <p>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:</p>
  
  <pre><code>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
  </code></pre>
  
  <p>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:</p>
  
  <pre><code>[segment @ 0x671a10] st:0 PTS: 9621360 DTS: 9621360 &lt; 9649233 invalid, clipping
  </code></pre>
  
  <p>It never survives - no exits, no output, so I have no saved stream until I manually restart the ffmpeg!</p>
  
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2014-03-07T01:38:10.207Z
Comments.Text: What do you mean by "and never survives"?  Does the video fail to produce anything or does it simply stop encoding?  It isn't exactly clear what the error you are getting is from your description.
Comments.UserDisplayName: AJ Henderson
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2014-03-07T09:49:54.207Z
Comments.Text: The ffmpeg never stops and the there is no video produced. It just keep printing such lines....
Comments.UserDisplayName: neoX
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2014-03-07T19:03:24.030Z
Comments.Text: You should include the first ~50 lines and the last ~50 lines of your `ffmpeg` console output if possible.
Comments.UserDisplayName: llogan
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2014-03-07T22:34:15.853Z
Comments.Text: could you pipe stderr to another process that kills and restarts ffmpeg if it detects the "invalid, clipping" comment?
Comments.UserDisplayName: stib
Tags: <ffmpeg>
Title: restart encoding with ffmpeg when bad source
UserDisplayName: neoX
ThreadText: # restart encoding with ffmpeg when bad source
  
  (Asked by: username_0 on 2014-03-07)
  
  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 < 9629949 invalid, clipping
  [segment @ 0x671a10] st:0 PTS: 9601935 DTS: 9601935 < 9629950 invalid, clipping
  [segment @ 0x671a10] st:0 PTS: 9603855 DTS: 9603855 < 9629951 invalid, clipping
  [segment @ 0x671a10] st:0 PTS: 9605775 DTS: 9605775 < 9629952 invalid, clipping
  [segment @ 0x671a10] st:0 PTS: 9607695 DTS: 9607695 < 9629953 invalid, clipping
  [segment @ 0x671a10] st:0 PTS: 9609615 DTS: 9609615 < 9629954 invalid, clipping
  [segment @ 0x671a10] st:0 PTS: 9611535 DTS: 9611535 < 9629955 invalid, clipping
  [segment @ 0x671a10] st:0 PTS: 9613455 DTS: 9613455 < 9629956 invalid, clipping
  [segment @ 0x671a10] st:0 PTS: 9615375 DTS: 9615375 < 9629957 invalid, clipping
  [segment @ 0x671a10] st:0 PTS: 9632160 DTS: 9614160 < 9649231 invalid, clipping
  [segment @ 0x671a10] st:0 PTS: 9624960 DTS: 9617760 < 9649232 invalid, clipping
  [segment @ 0x671a10] st:0 PTS: 9621360 DTS: 9621360 < 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 < 9649233 invalid, clipping
  
  ```
  
  It never survives - no exits, no output, so I have no saved stream until I manually restart the ffmpeg!
  
  **Tags:** ffmpeg
  
  > **Comments:**
  > **username_1**: What do you mean by "and never survives"?  Does the video fail to produce anything or does it simply stop encoding?  It isn't exactly clear what the error you are getting is from your description.  
  > **username_0**: The ffmpeg never stops and the there is no video produced. It just keep printing such lines....  
  > **username_2**: You should include the first ~50 lines and the last ~50 lines of your `ffmpeg` console output if possible.  
  > **username_3**: could you pipe stderr to another process that kills and restarts ffmpeg if it detects the "invalid, clipping" comment?  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_4 on 2014-08-27. Score: 7
  
  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 &
  
  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} > 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 &
               pid=$!
           else
               usleep 100
           fi
      done
  exit 0
  
  ```
  
  
  ---
  
  