scripts: webradio & recradio & playing == version Monday 29 March 2010 =================================================================================== #!/bin/bash ## webradio [ [volume | [R(ecord) [[duration HH:MM] tmpdir]]] | show ] ## uses sox, mplayer, grep, amixer Params=( ## declare 1st array with stations L1radio #0 radio1 #1 radio4 #2 radio5 #3 radio6 #4 klara #5 ndr_K #6 wdr3 #7 wdr5 #8 bbc3 #9 ireland #10 scotland #11 bretonne #12 ) URLs=( ## declare 2nd array with URLs "mms://194.171.15.26/l1radio" # L1radio #0 "http://shoutcast.omroep.nl:8100" # radio1 #1 "-playlist http://shoutcast.omroep.nl:8106/listen.pls" # radio4 #2 "http://shoutcast.omroep.nl:8108" # radio5 #3 "-playlist http://livestreams.omroep.nl/npo/radio6-bb" # radio6 #4 "http://mp3.streampower.be/klara-high.mp3" # klara #5 "http://213.200.97.110:8000/ndr/ndrkultur.mp3" # ndr_K #6 "-playlist http://www.wdr.de/wdrlive/media/wdr3.smil" # wdr3 #7 "-playlist http://www.wdr.de/wdrlive/media/wdr5.smil" # wdr5 #8 "-playlist http://www.bbc.co.uk/radio3/ram/r3g2.ram" # bbc3 #9 "-playlist http://www.liveireland.com/live.pls" # ireland #10 "-playlist http://www.bbc.co.uk/scotland/alba/media/live/radio_ng.ram" #11 "-playlist http://kerne.online.stalig.net/live-ori.ogg.m3u" # bretonne #12 ) ## initializations ## OUTWAV=$HOME/Downloads/RecWeb/ TmpDir=/tmp ## default tempdir MPLAY="/usr/bin/mplayer -cache 384 -quiet -nojoystick -nolirc -vo null " MREC=$MPLAY"-ao pcm:waveheader:file=" REC='' ProcessID='' ParamCount=$# Station=$1 if [ $# -ge 2 ]; then if [ $2 -ge 0 -a $2 -le 100 ]; then ## volume Volume=$2 else ## record Record=$2 Duration=$3 fi fi if [ $# -eq 4 ]; then TmpDir=$4 ## runtime created tempdir if [ ! -d $TmpDir ]; then mkdir -p $TmpDir if [ $? -ne 0 ]; then exit 1 fi fi fi cd $TmpDir ## cd to TmpDir: .wav (audiodump) will be created in TmpDir if [ ! -f /tmp/playing ]; then ## init playing echo '' > /tmp/playing fi ## functions ## function MRecorder() { local URL=$1 local OUT=$2 local L local Start typeset -i L=0 while true; do $MPLAY-endpos 00:00:00 $URL > /tmp/mrecord.$OUT 2>/dev/null NewURL=`cat /tmp/mrecord.$OUT | grep -m 1 'Playing ' | cut -c 9-` echo "$NewURL" > /tmp/mrecord.$OUT NewURL=`head -n 1 /tmp/mrecord.$OUT` L=${#NewURL}-1 NewURL=`echo "$NewURL" | cut -c -$L` Start=`date +%Y-%m-%d_%a_%H.%M` echo "$MREC$Start-$OUT.wav $NewURL" > /tmp/mrecord.$OUT $MREC$Start"-"$OUT.wav $NewURL 1>/dev/null 2>&1 done } function Move2Outwav() ## if TmpDir/ad-Param.wav exists then move it to $OUTWAV { local StartTime if [ ! -d $HOME/Downloads -o ! -d $HOME/Downloads/RecWeb ]; then mkdir -p $HOME/Downloads/RecWeb ## if targetdir doesn't exists, then create it fi StartTime=`ls $TmpDir/*-$1.wav 2>/dev/null | grep -m 1 $1.wav | cut -c 6- | cut -c -20` if [ -n $StartTime ]; then sox $TmpDir/*-$1.wav $OUTWAV$StartTime-$1.wav 2>/dev/null if [ $? -eq 0 ]; then rm $TmpDir/*-$1.wav fi fi } function KillCmdLine() ## kill CmdLine { local CmdL="$1" local ON='' if [ -n "$REC" ]; then if [ -f /tmp/rec_pid_${Params[$CmdNR]} ]; then ProcessID=`cat /tmp/rec_pid_${Params[$CmdNR]}` kill -9 $ProcessID 2>/dev/null rm /tmp/rec_pid_${Params[$CmdNR]} fi if [ -f /tmp/mrecord.${Params[$CmdNR]} ]; then CmdL=`cat /tmp/mrecord.${Params[$CmdNR]}` rm /tmp/mrecord.${Params[$CmdNR]} 2>/dev/null fi fi ON=`ps x | grep "$CmdL" | grep -v grep` ## find if mplayer CmdLine is running while [ -n "$ON" ]; do ProcessID=`echo "$ON" | cut -c -6` ## if ON then isolate ProcessID ; kill ProcessID if [ -n "$ProcessID" ]; then kill -9 $ProcessID 2>/dev/null fi ON=`ps x | grep "$CmdL" | grep -v grep` done } function Playing() { local Running Playing1st=`cat /tmp/playing` ## save which station is playing echo $REC${Params[$CmdNR]} > /tmp/playing case $1 in on) amixer set Front on amixer set Front $Volume% amixer set PCM 100% $CmdLine 1>/dev/null 2>&1 ;; rec) MRecorder "${URLs[$CmdNR]}" "${Params[$CmdNR]}" & rec_pid=$! echo $rec_pid > /tmp/rec_pid_${Params[$CmdNR]} if [ -z "$Duration" ]; then Running=$rec_pid while [ -n "$Running" ]; do sleep 5 Running=`ps h -o pid $rec_pid` done else sleep $Duration fi KillCmdLine "$CmdLine" Move2Outwav "${Params[$CmdNR]}" 2>/dev/null ## after recording, move file.wav ;; off) Running=`ps x | grep "$MPLAY" | grep -v grep` if [ -z "$Running" ]; then ## if nothing running anymore Playing1st='' ## reset /tmp/playing fi if [ -z $REC ]; then amixer set Front on amixer set Front 50% amixer set PCM 100% fi ;; *) echo 'error in calling function Playing' ;; esac echo $Playing1st > /tmp/playing } function ShowUsage() ## (exitcode ('show')): stations { ## fill Show with Params; echo Show; exit $1 Show="[ "${Params[0]} typeset -i I=1 while [ $I -lt ${#Params[@]} ]; do Show="$Show | "${Params[$I]} ## fill Show with stations I=I+1 done if [ "$2" = "show" -o "$2" = "Show" -o "$2" = "SHOW" ]; then echo "$Show ] " else Name=`basename $0` echo " Usage: " echo " $Name Show " echo " $Name $Show ] [VOLUME]" echo -n " $Name $Show ]" echo " [ 'R'(ecord) [ HH:MM ] ] [ TmpDir ]" echo -n "Free on TmpDir $TmpDir for recording:" df $TmpDir -h | grep G | cut -c 33- | cut -c -7 fi exit $1 } ## end functions ## ## MAIN ############################################################################### if [ $# -eq 0 ]; then ## if paramcount<>1 then ShowUsage ShowUsage 2 else ## else check param(1) in array Params typeset -i NR=0 typeset -i CmdNR=-1 while [ $NR -lt ${#Params[@]} ]; do if [ "$1" = "${Params[$NR]}" ]; then CmdNR=$NR ## if found then CmdNR := index Params( param ) fi NR=NR+1 done fi if [ $CmdNR -lt 0 ]; then ## no Param found; ShowUsage param(1) and exit ShowUsage 3 $1 fi ## CmdLine:= URLs( CmdNR ); if paramcount=2 then record; if paramcount=3 then endpos:= param(3) if [ $# -ge 2 -a "$2" = "R" ]; then ## for recording REC='REC ' CmdLine=$MREC${URLs[$CmdNR]} ## add URL from 2nd array else CmdLine=$MPLAY${URLs[$CmdNR]} ## add URL from 2nd array fi if [ $# -ge 3 ]; then ## endtime if recording + 15 secs timeshift Duration=`echo $3 | cut -c -2`"h "`echo $3 | cut -c 4-`"m 15s" fi KillCmdLine "$CmdLine" if [ -z "$ProcessID" ]; then ## CmdLine was not killed ## not already running: exec CmdLine = start recording | playing if [ -n "$REC" -a ! -f /tmp/mrecord.${Params[$CmdNR]} ]; then ## start recording if [ ! "REC ${Params[$CmdNR]}" = "$P" ]; then Playing rec 2>/dev/null fi else ## start playing P=`cat /tmp/playing` R=`cat /tmp/playing | grep REC` if [ -z "$P" -o -n "$R" ]; then ## just 1 station allowed playing Playing on fi fi fi Playing off printf "\a" ## beep exit $? =================================================================================== #!/bin/bash ## recradio [station [duration [start]]] ## uses kalarm, webradio ## inits Station=$1 Duration=$2 Start=$3 OK='' if [ $# -eq 0 ]; then kalarm --caption RecordRadio ## start kalarm to see pending jobs and the result echo recradio `date +%H:%M` ## time now webradio show ## list of possible stations echo -n " Set Station (Required) -> " read Station #echo -n " Set Start HH:MM [`date +%b' '%d` | `date +%m%d%y`] (Default Now) -> " ## at echo -n " Set Start [[[yyyy-]mm-]dd-]hh:mm (Default Now) -> " read Start echo -n " Set Duration HH:MM (Default None) -> " read Duration echo -n $Station' '$Start' '$Duration' OK? (or Ctrl-C) '; read OK ## confirm else ## params from commandline REC=`echo $Station | grep REC` if [ -n "$REC" ]; then Station=`echo $Station | cut -c 5-` fi fi if [ -z "$OK" -a -n "$Station" ]; then ## if confirmed and station known if [ -z "$Start" ]; then ## no starttime: start recording now echo "Don't close this terminal." ~/bin/webradio $Station R $Duration 1>/dev/null 2>&1 ## full path required else ## for unknown reason at doesn't work, but Kalarm does #echo "~/bin/webradio $Station R $Duration" | at $Start 1>/dev/null 2>&1 #echo `atq` ## set job for kalarm kalarm --time "$Start" --exec "webradio $Station R $Duration 1>/dev/null 2>&1" 1>/dev/null 2>&1 if [ $# -eq 0 ]; then ## running interactive in terminal echo -n "Press any key... " read ## confirm exit from terminal fi fi fi exit $? =================================================================================== #!/bin/bash ## playing: print content of /tmp/playing or next Kalarm; uses watch, kalarm function OutPut { Falarm=~/.kde/share/apps/kalarm/calendar.ics if [ ! -f /tmp/playing ]; then ## init playing echo '' > /tmp/playing fi Alarm=`cat /tmp/playing` if [ -z "$Alarm" -a -f $Falarm ]; then A=( $(cat $Falarm | grep 'DTSTART' | cut -c 9-) ) Alarm=${A[0]} typeset -i I=1 while [ $I -lt ${#A[@]} ]; do if [[ "$Alarm" > "${A[$I]}" ]]; then Alarm="${A[$I]}" fi I=I+1 done if [ -n "$Alarm" ]; then D=`echo $Alarm | cut -c 7- | cut -c -2` M=`echo $Alarm | cut -c 5- | cut -c -2` h=`echo $Alarm | cut -c 10- | cut -c -2` m=`echo $Alarm | cut -c 12- | cut -c -2` Alarm=$D'/'$M' '$h':'$m fi fi echo $Alarm } if [ $# -eq 0 ]; then kstart --skiptaskbar --skippager --alldesktops --windowclass kdesktop --window Kdesktop --onbottom Eterm --title playing -x -O --buttonbar no --scrollbar no --no-cursor yes -f '#4197FF' --cursor-color '#0E3A8D' --geometry 15x1+5+20 -e watch -tn 3 'playing X' 2>/dev/null & ## alternative without showing next alarm ##kstart --skiptaskbar --skippager --alldesktops --windowclass kdesktop --window Kdesktop --onbottom Eterm --title playing -x -O --buttonbar no --scrollbar no --no-cursor yes -f '#0000FF' --cursor-color '#0E3A8D' --geometry 15x1+5+20 -e watch -tn 3 cat /tmp/playing 2> /dev/null & else OutPut fi exit 0 ===================================================================================