#!/bin/sh
#
# recorder -- wrapper to pull remote audio stream and convert contents
# to mp3
#
# Usage: recorder best_of_our_knowledge 120 \
# mms://wmbcast.kqed.speedera.net/wmbcast.kqed/wmbcast_kqed_jan112002_1500_45704
#
# Feel free to copy, modify, etc.
#
# Love,
# danny [ http://dannyman.toldme.com/ ]

# Path and arguments to lame (mp3 encoder)
lame="/usr/local/bin/lame -S"
# Path and arguments to mplayer (stream decoder)
mplayer="/usr/local/bin/mplayer -quiet"
# Where to put the output files
odir="/usr/local/www/data"

showname=$1
duration=$2
url=$3

fifo="${showname}_fifo"
ofile="$odir/${showname}-`date +%Y%m%d-%H%M`.mp3"

mkfifo $fifo
$lame $fifo $ofile &
$mplayer -ao pcm -aofile $fifo $url &

sleep 5
pids=`ps auxww | grep $fifo | awk '{print $2}'`

sleep `echo ${duration}*60 | bc`

kill $pids
rm $fifo
