2021-03-01 01:40:41 +00:00
#!/bin/bash -e
2021-02-28 17:04:41 +00:00
2022-03-08 07:56:59 +00:00
target_url = " ${ 1 :- icecast : //source : source @127.0.0.1 : 61120 /main } "
ffmpeg_pids = ( )
2021-02-28 17:04:41 +00:00
call_ffmpeg( ) {
command ffmpeg -hide_banner " $@ "
}
daemon_ffmpeg( ) {
call_ffmpeg " $@ " &
2022-03-08 07:56:59 +00:00
ffmpeg_pids += ( $! )
2021-02-28 17:04:41 +00:00
}
shutdown_ffmpeg( ) {
2022-03-08 07:56:59 +00:00
if is_ffmpeg_running; then
2021-03-07 21:21:57 +00:00
kill " $ffmpeg_pid " || true
2022-03-08 07:56:59 +00:00
for t in $( seq 0 10) ; do
if ! kill -0 " $ffmpeg_pid " ; then
2021-10-12 18:32:57 +00:00
break
fi
sleep 1
done
2022-03-08 07:56:59 +00:00
if kill -0 " $ffmpeg_pid " ; then
2021-10-12 18:32:57 +00:00
kill -9 " $ffmpeg_pid " || true
fi
2021-02-28 17:04:41 +00:00
fi
ffmpeg_pid =
}
is_ffmpeg_running( ) {
[ -n " $ffmpeg_pid " ] && kill -0 " $ffmpeg_pid "
}
on_exit( ) {
shutdown_ffmpeg
}
trap on_exit EXIT
2021-03-01 00:51:10 +00:00
offline = 0
2022-03-08 07:56:59 +00:00
while true; do
2021-02-28 17:04:41 +00:00
found_audio_source = ""
2022-03-08 07:56:59 +00:00
while read -r line; do
declare -a " found_source=( $( sed -e 's/"/\\"/g' -e "s/'/\"/g" -e 's/[][`~!@#$%^&*():;<>.,?/\|{}=+-]/\\&/g' <<< " $line " ) ) "
found_source[ 0] = $( sed -e 's/\\\([`~!@#$%^&*():;<>.,?/\|{}=+-]\)/\1/g' <<< " ${ found_source [0] } " )
found_source[ 1] = $( sed -e 's/\\\([`~!@#$%^&*():;<>.,?/\|{}=+-]\)/\1/g' <<< " ${ found_source [1] } " )
2021-02-28 17:04:41 +00:00
case " ${ found_source [0] } " in
2022-04-01 06:09:51 +00:00
*\( ID*\ Main\ Audio\) )
2021-02-28 17:04:41 +00:00
found_audio_source = " ${ found_source [0] } "
; ;
esac
2022-04-01 06:47:59 +00:00
done < <( call_ffmpeg -loglevel info -extra_ips 192.168.188.21,192.168.188.76 -find_sources true -f libndi_newtek -i "dummy" 2>& 1 | grep -Po "'(.+)'\s+'(.+)" | tee)
2021-02-28 17:04:41 +00:00
2022-03-08 07:56:59 +00:00
if [ -z " $found_audio_source " ] ; then
offline = $(( offline + 1 ))
2021-03-01 00:51:10 +00:00
else
offline = 0
fi
2022-03-08 07:56:59 +00:00
if ! is_ffmpeg_running && [ -n " $found_audio_source " ] ; then
2021-02-28 17:04:41 +00:00
echo " starting ffmpeg with audio source: $found_audio_source " >& 2
2022-03-08 07:56:59 +00:00
call_ffmpeg -loglevel warning \
-analyzeduration 1 -f libndi_newtek -extra_ips 192.168.188.21 -i " $found_audio_source " \
-map a -c:a pcm_s16le -ar 48000 -ac 2 -f s16le - |
2022-03-09 11:17:08 +00:00
fakesilence --samplerate 48000 --channels 2 --silence-threshold 125ms |
2022-03-13 05:53:21 +00:00
daemon_ffmpeg -loglevel warning \
2022-03-08 07:56:59 +00:00
-ar 48000 -channels 2 -f s16le -i - \
2022-03-13 05:53:21 +00:00
-map a -c:a flac -f ogg -content_type application/ogg " ${ target_url } "
2022-03-08 07:56:59 +00:00
2021-02-28 17:04:41 +00:00
# HACK - can't use the standard mpegts here, but liquidsoap will happily accept anything ffmpeg can parse (by default)… so let's just use nut here even though it feels super duper wrong
2022-03-08 07:56:59 +00:00
elif is_ffmpeg_running && [ -z " $found_audio_source " ] && [ " $offline " -gt 0 ] ; then
2021-02-28 17:04:41 +00:00
echo "shutting down ffmpeg since no source has been found" >& 2
shutdown_ffmpeg # it won't shut down by itself unfortunately
fi
2022-03-08 07:56:59 +00:00
2021-03-07 21:21:57 +00:00
sleep 1
2021-02-28 17:04:41 +00:00
done