[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.
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)
the command i'm running
$command = "ffmpeg -i {$rtmpUrl} -c:v copy -c:a copy -t 3600 {$path} >/dev/null 2>/dev/null &";
$res = shell_exec($command)
i tried adding -timeout 0 option before and after the input like this
$command = "ffmpeg -timeout 0 -i {$rtmpUrl} -c:v copy -c:a copy -t 3600 {$path} >/dev/null 2>/dev/null &";
and
$command = "ffmpeg -i {$rtmpUrl} -c:v copy -c:a copy -timeout 0 -t 3600 {$path} >/dev/null 2>/dev/null &";
but no improvement.
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).
Note $rtmpUrl and $path have been checked and everything works fine as long as the script is stopped before the livestream ends.
Any suggestions are highly appreciated
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
$command = "timout 0 ffmpeg -i {$rtmpUrl} -c:v copy -c:a copy -t 3600 {$path} >/dev/null 2>/dev/null &";
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 -re flag fixed it for us.
Now script stops when no more new frames come in
$command = "ffmpeg -re -i {$rtmpUrl} -c:v copy -c:a copy -t 3600 {$path} >/dev/null 2>/dev/null &";
"-re" flag didnt help me, but i found other flag "-rw_timeout 5000000" (in microseconds), so command
ffmpeg -rw_timeout 5000000 -i rtmp://localhost:1935/video/$1 -c:v copy -c:a copy /var/www/rec/$1.mp4 2>> /var/www/log/ffmpeg.log
exits after 5 seconds of idle and i got good file.
This command executed from external sh file, which called from /etc/nginx/sites-available/mysite exec_push command, so nginx-rtmp dont kill ffmpeg.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With