Webradio and audio stream to Podcast converter

Periodically record a web stream and index the files via RSS!

I really enjoy listening to the radio, but unfortunately my favorite programs are broadcast at a time where I can't really listen to them. My solution for the problem is again! a shell script. It's meant to be run as a cronjob and uses vlc to record an online stream into a file periodically. It can easily be extended to grab the program information from a website.

The big feature, however, is that it also generates an RSS file which conforms to the Podcast specification. This way you can subscribe to your personal radio feed in iTunes and have it automatically synced with your mobile devices.

The code

(customizeable strings are written like ##THIS##)

#!/bin/bash
# Dependencies: bc eye3d vlc lame awk bash grep lynx
#
#
TMP=/tmp/_streamrec.tmp
function usage() {
echo "Usage: $0 station basename seconds
 $0 update
"
exit 1
}
function grep_tag() {
cat $1 |grep "^<$2:" |sed -e "s/^<$2://" -e 's/>$//' || echo "TAG ERROR in $2"
}
function update_feed() {
cat >feed.rss <<EOF
<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:itunes="http://www.itunes.com/dtds/podcast-1.0.dtd" version="2.0">
<channel>
<title>Radio Recorder</title>
<link>http://##YOUR URL##</link>
<language>en-us</language>
<copyright>&#x2117; &amp; &#xA9; ##2011 YOUR NAME##</copyright>
<itunes:subtitle></itunes:subtitle>
<itunes:author></itunes:author>
<itunes:summary>A webstream to Podcast converter</itunes:summary>
<description>A webstream to Podcast converter</description>
<itunes:owner>
<itunes:name>##YOUR NAME##</itunes:name>
<itunes:email>##YOUR EMAIL##</itunes:email>
</itunes:owner>
<itunes:image href="##YOUR IMAGE##" />
<itunes:category text="Radio" />
EOF
for FILE in `ls -1 *.mp3`; do
eyeD3 --no-color --set-encoding=utf8 -v $FILE >$TMP
TITLE=`grep_tag $TMP "Title.songname.content description .TIT2."`
SUBTITLE="Recorded with streamrec"
AUTHOR=`grep_tag $TMP "Lead performer.s..Soloist.s. .TPE1."`
SUMMARY=`grep_tag $TMP "Comments .COMM."`
PUBDATE=`grep_tag $TMP "Album.Movie.Show title .TALB."`
KEYWORDS=""
DURATION=`cat $TMP |grep Time |awk '{print $2}'`
IMAGE="IMAGE URL" ;;
esac
rm -f $TMP
echo "
<item>
<title>$TITLE</title>
<itunes:author>$AUTHOR</itunes:author>
<itunes:subtitle>$SUMMARY</itunes:subtitle>
<itunes:summary>$SUMMARY</itunes:summary>
<itunes:image href=\"$IMAGE\" />
<enclosure url=\"http://##THE LOCATION OF THE RECORDED FILES##/$FILE\" length=\"$BYTES\" type=\"audio/x-mp3\" />
<pubDate>$PUBDATE</pubDate>
<itunes:duration>$DURATION</itunes:duration>
<itunes:keywords>$KEYWORDS</itunes:keywords>
</item>
" >>feed.rss
done
echo "
</channel>
</rss>
" >>feed.rss
}
case $1 in
##STATION 1##) URL="##URL OF THE STREAM##"
SUMMARY=`echo ##TITLE##|recode ..UTF-8`
LYRICS=`lynx --dump ##DESCRIPTION URL## |recode ..UTF-8`
;;
 update) update_feed ; exit ;; 
 *) usage ;;
esac
if [ $# -ne 3 ]; then usage; fi
export LC_ALL="C.UTF-8"
TIMESTAMP=`date +"%y-%m-%d"`
PUBDATE=`date -u +"%a, %d %b %Y %T %Z" |sed 's/UTC/GMT/'`
FILENAME=##ABSOLUTE PATH TO RECORDINGS##/$1_$2_$TIMESTAMP.mp3
LOGFILE=##ABSOLUTE PATH TO LOGFILES##/streamrec.log
DURATION=`echo $3*60|bc`
# http://wiki.debian.org/MultimediaCodecs
DISPLAY= vlc -q "$URL" \
--sout "#transcode{acodec=mp3,ab=256,channels=2}:standard{mux=raw,dst=$FILENAME,access=file}" \
--intf=dummy < /dev/null >> $LOGFILE 2>&1 &
PID=$!
sleep $DURATION
kill $PID
wait $PID
# Set ID3 Tags
eyeD3 --no-color --set-encoding=utf8 \
-a "$1" \
-A "$PUBDATE" \
-t "[$1] $2, $TIMESTAMP" \
-F '~' --comment="eng~Details~$SUMMARY, recording from $PUBDATE\n$LYRICS" \
-G "Broadcast" \
-Y `date +"%Y"` \
$FILENAME >> $LOGFILE 2>&1 #--lyrics="eng~More Details~$LYRICS"
update_feed

Disclaimer

Any of the trademarks, service marks, collective marks, design rights or similar rights that are mentioned, are the property of their respective owners.