A bloated script for time-scheduled audio stream recordings
Posted May 18, 2006 12:15 UTC (Thu) by
scheck (guest, #4447)
Parent article:
The Grumpy Editor's guide to audio stream grabbers
I wrote a small script for time-scheduled stream recordings and bloated it
by extensive use of comments and a small "station database", which holds my favourite German radio stations.
A mere echo 'srec myrockstation 60 "Iron Maiden Live in Lederhosen"' | at 22:00 Sat would take care of your desired one-hour-show on coming Saturday at 10pm while you go out with friends who listen to Madonna... ;-)
BTW: if you adapt the mplayer command and the assignment of extensions, the script might also work with videos...
#!/bin/sh
#
# srec - stream recording
# v0.01 2006-03-21 torsten.scheck@gmx.de
#
# Copyright is hereby assigned to the Public Domain.
#
# This script relies on wget or mplayer, depending on
# the stream format.
# parameter defaults
station=${1:-"NoStation"}
length=$((${2:-60} * 60))
name=${3:-"Unnamed"}
# time period in seconds, after which the dumping process is checked
checktime=10
# choose station from database and build station list
url=''
list=''
add() {
list="$list"$(printf "%-8s %-3s %-25s %s" "$list_station" "$list_ext" \
"$list_info" "$list_name")"\n"
if [ "$station" == "$list_station" ]; then
ext=$list_ext
info=$list_info
url=$list_url
fi
}
############################################################
list_station="dlf"
list_ext="ogg"
list_name="Deutschlandfunk"
list_info="96 kbps, variable, stereo"
list_url="http://dradio-live.ogg.t-bn.de/dlf_high.ogg"
add
list_station="dlfmp3"
list_ext="mp3"
list_name="Deutschlandfunk"
list_info="48 kbps, constant, mono"
list_url="http://dradio-live.mp3.t-bn.de/dlf_live"
add
list_station="dlfwma"
list_ext="wma"
list_name="Deutschlandfunk"
list_info="48 kbps, constant, stereo"
list_url="mms://dradio-live.wm.t-bn.de/live/dlf/dlf"
add
list_station="dlrk"
list_ext="ogg"
list_name="Deutschlandradio Kultur"
list_info="96 kbps, variable, stereo"
list_url="http://dradio-live.ogg.t-bn.de/dkultur_high.ogg"
add
list_station="contra"
list_ext="ra"
list_name="SWR Cont.Ra"
list_info="44 kbps, stereo"
list_url="rtsp://213.254.239.61/farm/*/encoder/swr/contra/livestream.rm"
add
############################################################
filename="${name}_${station}_$(date +%Y%m%d_%H%M).${ext}"
logname="${filename%.*}.log"
# display help when station name is not found or wasn't provided
if [ -z "$url" ]; then
echo "Usage: $0 STATION [LENGTH in minutes] [NAME]
Saves an audio stream of a given internet radio STATION to a file in
the current working directory.
LENGTH defaults to 60 minutes and NAME defaults to 'Unnamed', so the
resulting file name looks like this: ${filename}xxx
The download process is logged to a separate file using the suffix 'log'.
This script is best used with the Unix tool 'at' for time-controlled
recordings. For example, try this to record the 55-minutes-show
'Querkoepfe' on the station 'Deutschlandfunk' next Wednesday at 21:05
including some 'safety minutes' before and after the show:
> echo 'srec dlf 60 Querkoepfe' | at 21:03 Wed
Mind your time zone and get the list of your locale's supported
abbreviations for the time specification with:
> locale -c LC_TIME
Available stations:" >&2
echo -ne "$list" | sort >&2
exit 1
fi
# use 'wget' as default tool and 'mplayer' for special streaming formats
case "$ext" in
ra|wma) util="mplayer";;
*) util="wget"
esac
# quit if needed tool is not installed
type "$util" >/dev/null 2>&1 || { echo "Please install '$util'."; exit 1; }
# all output is redirected to logfile for easier 'at' usage
exec >> "$logname"
exec 2>> "$logname"
until
# start background process to dump stream to file
if [ "$util" == "mplayer" ]; then
mplayer -vo null -vc dummy -dumpstream -dumpfile "$filename" "$url" &
else
wget -nv -O "$filename" "$url" &
fi
# keep process id of bg process
mypid=$!
# in case of SIGINT and SIGTERM: kill bg process before exit
trap 'kill $mypid; exit;' 2 15
# wait until bg process figured out if stream is available
# e.g. "ERROR 504: Server Full" ends the bg process
sleep $checktime
# check if process is still here, i.e. the stream is being downloaded,
# or if the recording length is reached
kill -0 $mypid >/dev/null 2>&1 || [ $length -le 0 ]
do
echo "Server problem, another try: $(date '+%Y%m%d %H%M%S')"
# decrease recording length by checktime; in the case of a transient
# problem the remaining length of the stream will be saved
length=$((length - checktime))
# retry download with another until loop
done
if [ $length -gt 0 ]; then
# wait recording length
sleep $length
# kill bg process and wait for its termination
kill $mypid
wait
else
# the recording length had been reached by trying just to get
# the download started
# an unrecoverable problem prevented the desired download
echo "Aborted."
fi
echo "$filename END: $(date '+%Y%m%d %H%M%S')"
(
Log in to post comments)