Background
I have a garden IP cam id like to stream live to youtube, so i decided to use ffmpeg to achieve this.
The problem
Whenever my IP cam restarts or loses connection; ffmpeg will get stuck on the same frame and not resume once the IP cam is back online.
My solution
I have the ffmpeg output logged to a file, then have a script fetch the last line of the log every few seconds and compares frame numbers. If the frame numbers match, it kills ffmpeg process and starts another ffmpeg process.
My question
Is there a better way more efficient way?
logchecker.sh
#/bin/bash
while true
do
frameA=$(tail /home/daniel/output.txt -n 1 | sed -nr 's/.*frame=(.*)fps.*/\1/p')
echo "$frameA"
sleep 3
frameB=$(tail /home/daniel/output.txt -n 1 | sed -nr 's/.*frame=(.*)fps.*/\1/p')
echo "$frameB"
if [ "$frameA" = "$frameB" ]
then
echo "Camera has hung"
pkill ffmpeg
echo "killed ffmpeg..."
echo "Waiting 30 secs"
sleep 30
bash /home/daniel/ffmpeg.sh &
echo "started ffpmeg.."
echo "Waiting 30 secs"
sleep 30
else
echo "proceed"
fi
sleep 2
done
ffmpeg.sh
#!bin/bash
sleep 30
ffmpeg -f lavfi -i anullsrc -rtsp_transport udp -i rtsp://user:password@url:5544/live0.264 -bufsize 5000k -c:v copy -c:a mp3 -b:a 1 -f flv rtmp://a.rtmp.youtube.com/live2/xxxx-xxxx-xxxx-xxxx 2> /home/daniel/output.txt
stat -L -c %y /proc/$(pgrep -x ffmpeg)/fd/4 | cut -d ':' -f 3 | cut -d ' ' -f 1
you can also have sub-second resolution. – Frontal