Tech, toys, games, and other minor obsessions.

Recording The Patchwork Philharmonic radio show

I made a little project for myself.  My brother is doing a radio show these days, but he’s on the east coast, so I have to catch it online.  Which works out just fine because I figured I’d be able to record it pretty easily so that I could listen to it at my leisure.  We’ll I did a little work and automated the whole recording process and I thought it would make an interesting thing to post.  So here goes…

Tools:
VLC
OS  X or Linux (i’m using #! currently)
Dropbox
curl
twitter
bit.ly

Step 1:

The first thing I needed was an application to record the internet stream.  When I think of media players I tend to think of one of two programs: VLC and mplayer.  I chose VLC, but not for any specific reason, they can probably both do the job.  After a little bit of reasearch I was able to find the command line parameters needed to capture the stream to a file

vlc -I dummy ${stream_url} –sout=file/raw:${file} –run-time=${duration} vlc://quit

Lets break that down:

-I dummy

All this does is keeps vlc from popping up a UI (keeps it in a command line).

${stream_url}

Is just a placeholder (a variable) for the actual stream URL (in my case http://wbor.org:8000/WBOR) the variable part will come in handy later when we script this.

–sout=file/raw:${file}

this is indicating that instead of playing the stream on the comptuers speakers we want to stream it to a file instead.  raw indicates that we are just going to copy the raw stream down with no encoding (you can encode on the fly if you need to) and ${file} is the variable for the path and filename we want to write.

–run-time=${duration}

pretty simple, it’s the amount of time (in seconds) that we want to record the stream for (in my case 5400).

vlc://quit

this is a command that causes vlc to quit when it is done (otherwise it stays active which we dont need).

So now I have a command that I can run to record the show, but I want to automate it.  For this i’m going to use cron which is installed by default on most OS X versions and most linux distributions by default.  I’m also going to write a script to customize the execution of the script a little bit, for this I’m writing a bash script.  Again most OS X and linux installations have bash installed by default.  Here are the two scripts I wrote

JRS.sh

#!/bin/bash
###########
# JRS.sh – Joes Radio Show
# author: mjb
# last modified 2009/09/30
#
# this script records a radio program
# then copies it to a dropbox folder
# for sharing purposes
###########

#### variables {
twit_user=”"
twit_pass=”"
message=”The Patchwork Philharmonic for $(date +”%Y-%m-%d”)”
tmp_path=”/tmp”
stream_url=”http://wbor.org:8000/WBOR”
path=”/Users/mbabler/Dropbox/Public/JoesRadio”
filename=”$(date +”%Y-%m-%d”)-JRS.mp3″
tmp_file=”${tmp_path}/${filename}”
file=”${path}/${filename}”
duration=”5400″
base_url=”http://dl.getdropbox.com/u/15310/JoesRadio”
post_url=”${base_url}/${filename}”
twitter_url=”http://twitter.com/statuses/update.xml”
up_status=1
#### } end variables

echo “***$(date)”
echo “$(date +”%T”): Starting Capture”
### run vlc and capture the stream
/Applications/VLC.app/Contents/MacOS/VLC -I dummy ${stream_url} –sout=file/raw:${tmp_file} –run-time=${duration} vlc://quit

### as long as vlc quit normally then…
if [ $? -eq 0 ]; then
echo “$(date +”%T”): Capture Complete”
### copy to dropbox folder
mv ${tmp_file} ${file}
echo “$(date +”%T”): File Copied to Dropbox”

### check if file has been uploaded yet
echo “$(date +”%T”): We’re going to check every 5 minutes to see if the file has been uploaded”
while [ $up_status -ne 0 ]; do
sleep 300
echo “$(date +”%T”): Checking if file has been uploaded to Dropbox…”
### this is not an ideal way to check it, a consistant dropbox api would be better
### this also wont work for not public files
curl -sfI ${post_url} > /dev/null 2>&1
up_status=$(echo $?)
done
echo “$(date +”%T”): File has been uploaded to Dropbox!”

### post to twitter about it
echo “$(date +”%T”): Telling the world”
### call bit.ly.sh to shorten the URL
short_url=$(sh ~/bin/bit.ly.sh $post_url)

message=”${message} ${short_url}”
### post the message
curl -u ${twit_user}:${twit_pass} -d status=”$message” http://twitter.com/statuses/update.xml
echo “$(date +”%T”): Twitter notified”
else
echo “$(date +”%T”): Capture failed”
fi
echo “***Done”

bit.ly.sh

#!/bin/bash

##############
# bit.ly.sh
# author: mjb
# last modified 2009/10/02
#
# this script takes a url as an argument
# and then connects to bit.ly and
# outputs the shortened URL
##############

### variables
apiKey=”"
login=”"
version=”2.0.1″
format=xml
base_url=”http://api.bit.ly/shorten”
longURL=”$1″
url=”${base_url}?version=${version}&longUrl=${longURL}&format=${format}&login=${login}&apiKey=${apiKey}”

### make the request
response=$(curl -s $url 2>&1)

### strip all the junk and get the URL
x=${response##*}
bit_url=${x%%*}

### output the new short URL
echo ${bit_url}

The first script, JRS.sh, is the parent script.  The second script, bit.ly.sh, is called by the parent.  JRS.sh creates the filename, using the current date and starts the recording.  After the recording is finished it moves the file from the /tmp dir and puts it in my Dropbox.  Once the file has been copied to the dropbox it starts getting synced to the dropbox server.  The script will loop every 5 minutes to check and see if the upload has completed yet.  After the sync to the dropbox server is complete the script creates a bit.ly link to the file by calling the bit.ly.sh script.  Once we have the shortened URL we create a twitter message and post to twitter telling people where they can download the mp3.

Then we need to automate running the script by putting an entry in cron

crontab -e

will put you into edit mode of your cron.  You then need to add something like the following

#min #hour #dayofmonth #month #weekday #command
30   11    *           *      2        sh /Users/mbabler/bin/JRS.sh > /tmp/JRS.log 2>&1

that cron entry means that this command will run at 11:30a every Tuesday and will redirect the output to the file /tmp/JRS.log

So now all the pieces are together we have a scheduled recording that will automatically make the files available via dropbox (and will automatically sync to people who its shared with) and post a link on twitter!

I should figureout how to podcast it…

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>