elfs: (Default)
[personal profile] elfs
Tonight, my local NPR affiliate is playing Selected Shorts, and Leonard Nimoy will be doing the reading. I want to listen to it, but Selected Shorts isn't available in a free archive and I don't want to be up at that hour. The problem was simple: how do I capture the audio in a podable format?

My solution works for Linux geeks.

Well, Mplayer will play many audio streaming formats, including many MS and Quicktime formats, but it dumps to a file. Also, I want it in MP3 format, and Lame will handle that, but how do I get the file from Mplayer to Lame without a humungous wave file in the middle? Well, Linux has named pipes, so I created one of those, and then Mplayer dumps to it, and Lame picks the stream out of it. I put both of those processes in the background in the right order, then wait for the correct interval and kill the Mplayer process. Lame detects the pipe closure and cleans up the MP3 output appropriately.

Obviously, for this to work, you need Mplayer, Lame, and I strongly recommend Delay, a replacement for the traditional sleep command with a lot of nifty features. If you don't have it, you can edit the file and substitute sleep for Delay.

Posting code to LJ is notoriously difficult, but let's see if I can't get it to behave now. If you use this code, you are responsible for its use, no warranties, released under the GNU Public License, all that. You are also responsible for discovering your local NPR affiliate's data stream (which can usually be uncovered by downloading one of the 'listen now' links and examining the source code), and for using it responsibly.

 #!/bin/bash

INTERVAL="2:00"
OUTFILE="output.mp3"
AFFILIATE="YOUR AFFILIATE'S MEDIA ADDRESS GOES HERE"

if [ "X$1" == "X-h" -o "X$1" == "X-u" ]; then
echo "Usage: $0 [OUTFILE] [INTERVAL]"
echo "Default outfile is output.mp3"
echo "Default interval is one hour."
exit
fi

if [ "X$1" != "X" ]; then
OUTFILE=$1
fi

if [ "X$2" != "X" ]; then
INTERVAL=$2
fi

rm -f /tmp/nprrip.$$
mkfifo /tmp/nprrip.$$
lame /tmp/nprrip.$$ $OUTFILE 2> /dev/null &
mplayer -really-quiet -ao pcm:file=/tmp/nprrip.$$ $AFFILIATE &
delay $INTERVAL
kill %2
rm -f /tmp/nprrip.$$

Date: 2006-07-19 05:50 am (UTC)
From: [identity profile] ewhac.livejournal.com
Hmm, mkfifo. Didn't know about that one; thanks.

Date: 2006-07-19 06:11 pm (UTC)
From: [identity profile] elfs.livejournal.com
Useful for all sorts of things! Along with creating pipes for programs that can't write to stdout, it can also be used to concatenate a split file on the fly without having to chew up your diskspace-- useful for previewing a binary download from Usenet:
mkfifo porn.mpg
cat dirtymovie.mpg.??? > porn.mpg
mplayer porn.mpg

You can't seek back in the stream this way, but you can seek forward, so if you've got parts one through five you can get a good idea if you want to burn bandwidth downloading parts six through ninety-nine. It's faster, saves diskspace, and saves wear-and-tear on the drive.

Profile

elfs: (Default)
Elf Sternberg

December 2025

S M T W T F S
 12345 6
78910111213
14151617181920
21222324252627
28293031   

Most Popular Tags

Style Credit

Expand Cut Tags

No cut tags
Page generated Dec. 31st, 2025 05:30 pm
Powered by Dreamwidth Studios