This bash script records a 60mins archived Stadsradio den haag's broadcast stream. Copy it to a file, give it an appropriate name and make it executable.

The script uses the zenity program for its dialog boxes; this should come installed by default on most modern Linux distros.

If you want to record another stream, look at the STREAM variable.

If you want to record other time slots, add them below the true 02 line [e.g. false 03 \ adds the 3am slot]

By default, the recording is saved in WAV format as tonkaYYYYmmddHHMM.wav [e.g. tonka201104120200.wav] on your desktop; if you want to change any of this, look at the STARTDIR and OUTFILE variables.

#!/bin/bash

###
#
# Copyright © 2008 J. Lester Novros II <redbaron@xs4all.nl>
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# The full text of the GNU General Public License can be found at
# <http://www.gnu.org/copyleft/gpl.html>
#
# ===============================================================
#
# tonkatap-cli v1.0
#
# records 60mins Tonka stream from given date and time in directory of choice
#
# requires zenity and mplayer packages
#
# recommends libnotify-bin package for program end notification
#
###

test -x /usr/bin/mplayer || exit 0
test -x /usr/bin/zenity  || exit 0

PROGRAM=/usr/bin/zenity

function setDateTimeStamp()
{
# stream URL required date/time format 201004080000
#                                      y   m d h m
   DATE=`$PROGRAM --calendar \
                  --text="tonkatap date picker" \
                  --date-format="%Y%m%d"`
   if [[ $? == -1 || $? == 1 || $? == 255 ]]; then
      clear
      exit
   fi
   
   TIME=`$PROGRAM --list --radiolist \
                  --height="225" \
                  --title="tonkatap time picker" \
                  --column "record" --column "time" \
                  false 00 \
                  false 01 \
                  true  02`
   if [[ $? == -1 || $? == 1 || $? == 255 ]]; then
      clear
      exit
   fi
   TIME=${TIME}00
}

function setStartDir()
{
   cd ~/Desktop

   STARTDIR=`$PROGRAM --file-selection \
                      --directory \
                      --title="Save tonkatap folder"`
   if [[ $? == -1 || $? == 1 || $? == 255 ]]; then
      STARTDIR=~/Desktop
   fi

   cd $STARTDIR
}

function startRecording()
{
   STREAM=mms://live-wm.hdpnet.nl/users/stadsomr/windows/mp3/dhfm920/$DATE$TIME.mp3
   OUTFILE=tonka$DATE$TIME

# mplayer slave communicates thru named pipe [date/time added to get unique name to facilitate simultaneous recordings]
   mkfifo /tmp/mplayer$DATE$TIME.pipe

# 'nocache' needed to prevent 'Cache not filling!' error msgs
# 'quiet' needed to prevent status msg from polluting output needed in |updateProgress|
# stderr redirection needed to catch 404 error in |parseOutput|
   mplayer $STREAM \
           -ao pcm:fast:waveheader:file=$OUTFILE.wav \
           -input file=/tmp/mplayer$DATE$TIME.pipe \
           -nocache \
           -quiet \
           -slave \
           -vc dummy -vo null 2>&1 | parseOutput
}

function parseOutput()
{
   while [ ! "$LINE" == "Starting playback..." ]; do
      read LINE
      if [ "$LINE" == "Failed, exiting." ]; then
         $PROGRAM --error --text="server says: '404 Not Found'"
         cleanup
         exit
      fi
   done

   updateProgress
}

function updateProgress()
{
   local PERCENTDONE=0
# 100% is never reached - is this an mplayer bug?
   until [ $PERCENTDONE == 99 ]; do
   # tell mplayer slave to output percent done as 'ANS_PERCENT_POSITION=nn'
      echo "get_percent_pos" > /tmp/mplayer$DATE$TIME.pipe
      read LINE
   # trim mplayer slave output to number
      PERCENTDONE=${LINE##ANS_PERCENT_POSITION=}
   # echo slave output for zenity progress
      echo $PERCENTDONE
      sleep 10
   done | $PROGRAM --progress \
                   --auto-close \
                   --text="" \
                   --title="Tonka stream download progress" \
                   --width="500"

# this is dependent on libnotify-bin being installed
   if [[ -x /usr/bin/notify-send ]]; then
      notify-send -i info "$OUTFILE.wav recording succesfully completed"
   fi

   cleanup
}

function cleanup()
{
   rm -rf /tmp/mplayer$DATE$TIME.pipe
}

   # not used atm
function convert2mp3()
{
   lame -m s $OUTFILE.wav -o "$OUTFILE.mp3" 2>&1 > /dev/null
}

   # trap ctrl-C and friends so we can clean up after ourselves
trap "cleanup" INT TERM EXIT

setDateTimeStamp
setStartDir
startRecording

trap - INT TERM EXIT