title: FFMPEG RTMP STREAM RECORDING TIMEOUT
body: <p>[SOLVED] -- solution in FINAL EDIT
  i am recording an rtmp livestream with ffmpeg, and looking to add a flag that will automatically stop processing the moment new data stops arriving.</p>
  <p>If i start and stop the script (stop by killing the process on demand), everything is fine, recording is saved and can be played.
  However, when the stream is stopped from the source without a manual call to STOP, the script will still run for a bit and the resulting file will be corrupted (tested with manual stop call - works , and with stopping the stream before the recording , simulating browser/tab close or disconnect - fails)</p>
  <p>the command i'm running</p>
  <pre><code>$command = &quot;ffmpeg -i {$rtmpUrl} -c:v copy -c:a copy -t 3600 {$path} &gt;/dev/null  2&gt;/dev/null &amp;&quot;;
  $res = shell_exec($command)
  </code></pre>
  <p>i tried adding -timeout 0 option before and after the input like this</p>
  <pre><code>$command = &quot;ffmpeg -timeout 0 -i {$rtmpUrl} -c:v copy -c:a copy -t 3600 {$path} &gt;/dev/null  2&gt;/dev/null &amp;&quot;; 
  </code></pre>
  <p>and</p>
  <pre><code>$command = &quot;ffmpeg -i {$rtmpUrl} -c:v copy -c:a copy -timeout 0 -t 3600 {$path} &gt;/dev/null  2&gt;/dev/null &amp;&quot;;
  </code></pre>
  <p>but no improvement.</p>
  <p>What am i missing here? is there any way to automatically stop the script when new data stops ariving from the livestream (meaning that stream is stopped and recording should stop aswell).</p>
  <p>Note $rtmpUrl and $path have been checked and everything works fine as long as the script is stopped before the livestream ends.</p>
  <p>Any suggestions are highly appreciated</p>
  <p>LATER EDIT: realised timeout was set in the wrong place, added it first but result was still the same, so still looking for any suggestions</p>
  <pre><code>$command = &quot;timout 0 ffmpeg -i {$rtmpUrl} -c:v copy -c:a copy -t 3600 {$path} &gt;/dev/null  2&gt;/dev/null &amp;&quot;;
  </code></pre>
  <p>FINAL EDIT
  in case someone finds this thread looking for a solution in a similar case,
  solved, timeout was not what i was looking for, instead using the <code>-re</code> flag fixed it for us.
  Now script stops when no more new frames come in</p>
  <pre><code>$command = &quot;ffmpeg -re -i {$rtmpUrl} -c:v copy -c:a copy -t 3600 {$path} &gt;/dev/null  2&gt;/dev/null &amp;&quot;;
  </code></pre>