#!/bin/bash # As far as I know ripping streaming radio is legal. Nevertheless, you use # this script at your own risk. # http://lwn.net/Articles/182954/ # --- check user args --------------------------------------------------------- if [ x"$1" = x"" ]; then echo 'please specify an output file (mp3)' exit 1 else outfile=$1 fi if [ x"$2" = x"" ]; then echo 'please specify a URL' exit 2 else url=$2 fi if [ x"$3" = x"" ]; then echo 'please specify a recording duration (sec)' exit 3 else dur=$[ $3+2 ] fi # mplayer without video MPLAYER="mplayer -quiet -cache 32 -nojoystick -nolirc -prefer-ipv4 -noframedrop -vc null -vo null " # lame, recommended noise shaping, vbr default quality with max256kbps, output every 10min #LAME="lame --vbr-new -V 4 -B 256 --nohist --disptime 600 -h --tc \"ripped: `date +\%m-\%d-\%Y`\"" LAME="lame -h --vbr-new -V 4 --nohist --disptime 600" #LAME="lame -h -b 128 --nohist --disptime 600" ## settings below are optimized for voice encoding #lame -S -a -m m --ty "$YEAR" --vbr-new -V 9 --lowpass 13.4 --athaa-sensitivity 1 \ # --resample 32 $TEMPFILE-silenced.wav $FILE.mp3 >/dev/null & #sox $TEMPFILE.wav -c 1 $TEMPFILE-silenced.wav silence 1 0.2 0.5% -1 0.2 0.5% >/dev/null& checktime=10 # --- create a named pipes ---------------------------------------------------- tmpdir=`mktemp -td pipesXXXXX` || exit 4 wavpipe="$tmpdir/wavpipe" #soxpipe="$tmpdir/soxpipe" mkfifo $wavpipe || exit 5 #mkfifo $soxpipe || exit 6 # --- attach and execute ------------------------------------------------------ # invoke lame sourcing wave pipe and detach echo "$LAME $wavpipe \"$outfile\" 2>&1 &" $LAME $wavpipe "$outfile" 2>&1 & #$LAME $soxpipe "$outfile" 2>&1 & # invoke sox, sourcing the wave pipe and sinking the sox pipe and detach #sox $wavpipe $soxpipe silence 1 1 0.2 0.5% -1 0.2 0.5% >/dev/null & dur=$((dur - checktime)) until # start ripping, sinking to streaming pipe as a detached process so we can # begin countdown to TERM echo "$MPLAYER -ao pcm:fast:file=\"$wavpipe\" -playlist \"$url\" 2>&1" $MPLAYER -ao pcm:fast:file="$wavpipe" -playlist "$url" 2>&1 & mypid=$! # in case of SIGINT and SIGTERM: kill bg process before exit trap 'kill $mypid; exit 7;' 2 15 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 || [ $dur -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 dur=$((dur - checktime)) # retry download with another until loop done if [ $dur -gt 0 ]; then # wait recording length sleep $dur # 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 # remove pipes rm -fr $tmpdir