TIme Shifting Radio without killing your iPod Battery


Listening to your favorite programs on your own time is great, but if your programs are longer than 15 minutes, your battery charge doesn't last very long. Here's how to get around that.

I like to listen to NPR programs (oops, there goes half of my audience) like "Fresh Air", "This American Life" and "A Prairie Home Companion." Unfortunately, I am not always available to listen to these programs at the time they are broadcast. For years I have wanted a way to time shift them, the way I time shift TV programs. Finally, technology is available to do it. When I started recording the programs off the internet radio streams, I discovered another problem. My 3rd generation iPod has only(!) 20 minutes or so of skip protection. This means that the whole track can't stay in memory, so the hard disk has to run quite a lot, leading to a much reduced battery charge. This article will tell how I get around that. (Sorry, the code presented here only works for Mac). This stuff is only necessary for hard disk based iPods. Shuffles and nanos have no problem with long tracks, since everything is in flash memory already.

There are many products which can capture internet radio streams. After trying a few, I settled on Audio Hijack. It does a good job of capturing the streams I want. But I am left with the problem of slicing them up. Now, there are stream capture apps that slice up captured streams, but those are oriented to grabbing music, not the kind of continuous programs I am interested in.

So, back to good old AppleScript. iTunes can be told to set the start and stop times of a track, and then to convert that track, creating a new track that is only from the start to the stop time:
tell application "iTunes"
set start of theTrack to theStart
set finish of theTrack to theFinish
set newTrack to item 1 of (convert theTrack)
end tell

OK, that's a good start, but hardly useful. It would require setting new start and stop times, and a lot of manual work. Let's set up a repeat loop to slice a whole track up into 15 minute segments.
tell application "iTunes"
set trackDuration to the duration of theTrack
set theStart to 0
set theFinish to 15 * minutes
repeat while theFinish < trackDuration
set start of theTrack to theStart
set finish of theTrack to theFinish
with timeout of 1200 seconds
set newTrack to item 1 of (convert theTrack)
end timeout
set theStart to theFinish
set theFinish to theFinish + 15 * minutes
if theFinish > trackDuration then
set theFinish to trackDuration
end if
end repeat
set start of theTrack to theStart
set finish of theTrack to theFinish
with timeout of 1200 seconds
set newTrack to item 1 of (convert theTrack)
end timeout
end tell

Now we're cooking. The timeout statements make sure that the script doesn't timeout while the conversion is going on (AppleScript has a default timeout which is shorter than most conversions). But we don't have a way to specify the track. Well, how about converting the current selection in iTunes? And as long as we're doing that, how about we make it possible to select a group of tracks to convert all at once?
tell application "iTunes"
set selectedTracks to selection
repeat with theTrack in selectedTracks
set trackDuration to the duration of theTrack
set theStart to 0
set theFinish to 15 * minutes
repeat while theFinish < trackDuration
set start of theTrack to theStart
set finish of theTrack to theFinish
with timeout of 1200 seconds
set newTrack to item 1 of (convert theTrack)
end timeout
set theStart to theFinish
set theFinish to theFinish + 15 * minutes
if theFinish > trackDuration then
set theFinish to trackDuration
end if
end repeat
set start of theTrack to theStart
set finish of theTrack to theFinish
with timeout of 1200 seconds
set newTrack to item 1 of (convert theTrack)
end timeout
end repeat
end tell

Well, that cuts down a lot of the work. But now we're stuck renaming all of the new tracks, and we will probably want to put them into a playlist. As the Perl camel book says, laziness is one of the hallmarks of a good programmer. So back to the code.
tell application "iTunes"
set selectedTracks to selection
repeat with theTrack in selectedTracks
set trackName to name of theTrack
set theDatePos to offset of "20" in trackName
set showName to text 1 thru (theDatePos - 2) of trackName
set playListName to "`" & showName & "'"
set theList to playlist playListName
set trackDuration to the duration of theTrack
set theStart to 0
set theFinish to 15 * minutes
set theIndex to 1
repeat while theFinish < trackDuration
set start of theTrack to theStart
set finish of theTrack to theFinish
with timeout of 1200 seconds
set newTrack to item 1 of (convert theTrack)
end timeout
set newName to the name of theTrack & " " & theIndex
set name of newTrack to newName
add (get location of newTrack) to theList
set theStart to theFinish
set theFinish to theFinish + 15 * minutes
if theFinish > trackDuration then
set theFinish to trackDuration
end if
set theIndex to theIndex + 1
end repeat
set start of theTrack to theStart
set finish of theTrack to theFinish
with timeout of 1200 seconds
set newTrack to item 1 of (convert theTrack)
end timeout
set newName to the name of theTrack & " " & theIndex
set name of newTrack to newName
add (get location of newTrack) to theList
end repeat
end tell

So, what's going on here? Audio Hijack leaves me with tracks that are named like this: "Fresh Air 20051124 1259". So I have created playlists like this: `Fresh Air'. The backtick (`) sorts to the top of an ASXII sorted list, putting these playlists at the top of my iPod list. The first part of the repeat gets the playlist name for the given track. Each sliced track is renamed with an appended index, and then added to that playlist.

One tricky point. Contrary to what the iTunes APpleScript dictionary says, convert does not return a track. it returns the track in a list. Thus, to get the track, you need to ask for the first item of the list.

Well, that does it. Just copy that last script into a new script editor doc, and save it into your Scripts folder. Make the appropriate playlist(s) in iTunes, Select the tracks you want to slice, and choose your script. You will get sliced up shows, named and put into playlists.

One final thing. It occurred to me while I was preparing this entry that perhaps a track converted to an audiobook wouldn't have the battery problem. I can't find anything that ways one way of the other. If you have information about that, put it into the comments.

Posted: Thu - November 24, 2005 at 11:58 AM          


©