My quest for stability with an IPTV provider
Hi all, I have been working on trying to get IPTV stable enough to use with TVHeadend. The issue I have with many providers is when the stream goes bad, my recordings become unusable. The last few days I have made a bit of a breakthrough and it seems pretty stable. I thought I would share my work with you all to pay it forward and see if it can be improved. One thing I'm not happy with is it doesn't necessarily clean up the processes particularly when a mux scan happens. Credit goes to [this thread](https://tvheadend.org/boards/4/topics/29249) which kept feeding me ideas to try. Anyways here's what I did:
/usr/local/bin/ffmpegin.sh
#!/bin/bash
while [ 1 ]; do /opt/ffmpeg-5.1.1-amd64-static/ffmpeg -nostats -reconnect 1 -reconnect_on_network_error 1 -reconnect_on_http_error 1 -reconnect_streamed 1 -reconnect_delay_max 30 -fflags nobuffer -err_detect ignore_err -i $1 -c copy -map 0 -f mpegts pipe:1 1> /home/hts/dump.$2.stream done
/usr/local/bin/ffmpegout.sh
#!/bin/bash
/opt/ffmpeg-5.1.1-amd64-static/ffmpeg -nostats -follow 1 -err_detect ignore_err -i /home/hts/dump.$1.stream -c:a aac -c:v mpeg2video -b:v 12M -map 0 -f mpegts pipe:1
/usr/local/bin/linker.sh
#!/bin/bash
pid=$$ echo "main pid is $pid" | logger -t ffmpeg-linker
trap 'echo "Caught trap, ffmpegin.sh pid is $PID" | logger -t ffmpeg-linker && rm -f /home/hts/dump.$pid.stream && trap - SIGTERM && kill -- -$$ | logger -t ffmpeg-linker' EXIT SIGINT SIGTERM
rm -f /home/hts/dump.$pid.stream
mkfifo /home/hts/dump.$pid.stream
/usr/local/bin/ffmpegin.sh $1 $pid & PID=$! echo "ffmpegin.sh pid is $PID" | logger -t ffmpeg-linker
/usr/local/bin/ffmpegout.sh $pid
rm -f /home/hts/dump.$pid.stream
And of course, you need some kind of filter for your m3u file to add the pipe command to the front of each line: pipe:///usr/local/bin/linker.sh
This approach needs a bit of grunt in your machine too, I've got an old 2.5Ghz Xeon and each recording uses about 2 cores.
Any ideas on how to improve this?