ffmpeg monitor and restart stream when it hung or stall
You are using ffmpeg to stream to youtube but after a while, it hung or stuck. You want to restart the streaming command when this happens.
First run the ffmpeg command on a terminal in a while loop (this example is for fish terminal)
1
while true
2
ffmpeg -re -i https://roundrobin3.videostreamingwowza.com/visdeurbel2/visdeurbel2.stream/playlist.m3u8 -c:v copy -c:a aac -ar 44100 -ab 128k -ac 2 -strict -2 -flags +global_header -bsf:a aac_adtstoasc -bufsize 3000k -f flv "rtmp://a.rtmp.youtube.com/live2/abcd-efgh-ijkl" > youtube.log
3
sleep 5
4
end
Copied!
This will run the stream and output it to a log.txt
run this script on separate terminal to monitor and restart ffmpeg process
1
#!/bin/sh
2
​
3
#sleep 10
4
while true
5
do
6
frameA=$(grep "videostreamingwowza" youtube.log | tail -n1 | sed -n 's/.*\(roundrobin3.videostreamingwowza.com.*\.ts.*\)/\1/p')
7
echo "A: $frameA"
8
sleep 30
9
frameB=$(grep "videostreamingwowza" youtube.log | tail -n1 | sed -n 's/.*\(roundrobin3.videostreamingwowza.com.*\.ts.*\)/\1/p')
10
echo "B: $frameB"
11
​
12
if [ "$frameA" = "$frameB" ]
13
then
14
echo "Stream has hung"
15
printf "%s - Stream has hung\n" "$(date)" >> stream.log
16
pkill ffmpeg
17
echo "killed ffmpeg..."
18
fi
19
​
20
sleep 2
21
done
Copied!
Basically it take some line from the log, and do it again after 30 seconds.
If the line stays the same, it assume that the stream stuck and kill the ffmpeg process
Last modified 1mo ago
Copy link