Playing Amazon music samples on Linux
12th of December 2006
Let's say you want to shop for music CDs at Amazon (because you want them with nice covers and/or are feeling a bit generous towards the lovely recording industry in this Christmas time), that you use Linux and that you would like to sample the music before you buy. You have a problem, at first.
Real Audio samples from Amazon will not play for you, ever, Helix and RealPlayer10 bomb out on Real Audio 3 (hello, wake up, Real Networks!), mplayer doesn't like it either. This is a confirmed problem. With WMA, on the other hand, there's at least hope. So let's start coding...
The solution in Bash follows, and it even beats native Windows media players, because you don't have to click each sample's link, it plays them sequentially:
# takes ASIN of the CD as first parameter
# CD no. in a compilation as optional second parameter,
# defaults to first CD
function playamazon () {
[ "$2" != "" ] && CD=$2 || CD=1;
for i in $(seq -w 1 20); do
mplayer -playlist http://www.amazon.com$(wget -q -O - \
http://www.amazon.com/gp/music/wma-pop-up/${1}00${CD}0${i} \
| grep URL | cut -d\" -f4) 2>&1 | grep "name:";
done;
}
Run the above function definition in Bash shell or put in your .bashrc. Look up the CD's ASIN identification number and run like this:
$ playamazon B000GGSMD0 name: Cape Cod Girls ^C $ playamazon B000GGSMD0 2 name: Boney name: Good Ship Venus name: Long Time Ago ...
It will finish at the end of the samples for the CD, reaching 20 tracks, or being hit by your Ctrl+C, whichever comes first. Of course this only works if Amazon.com (the US one) actually lists some available WMA (Windows) samples. So far has been working consistently for me... now off for some shopping!
BTW, Amazon, in the unlikely case you're listening, how about making samples available of every CD in your catalogue that you are actually selling a copy of (and even list it as being in stock, in your stock)? That would be very helpful, thanks.
Edit: When you're feeling like turning Amazon into your regular music sample web radio (that's surely what they had in mind when they introduced the music samples, anyway, we're just helping them here), here's another ugly unmaintainable one-line hack system shells can be so proud of (and many a sysadmin thank their job security for):
sed -e 's/^\([^ ]*\)[ ]\?\([^ ]*\)$/echo Album ASIN: \1 \ \&\& playamazon \1 \2/' < amazon_playlist | bash -il
(broken up here for layout reasons)
Assumes you have a file amazon_playlist with one ASIN per line, optionally followed by the CD number parameter. No format or type-checking here, nope. Helpfully echoes the ASIN for when you like the album so much that you actually want to buy it.
Now you can be off and productive again (such as reading Reddit or Slashdot, for instance) and your music sampling & maybe shopping goes on in the background!
Comment
I've seen it in action at the Blag (LUG) meet in Brixton and it's impressive. It even plays CD sets - those that are "two disk buys".
David Svoboda from Carnegie Mellon University sent me his take on this script, which I copy below verbatim (sorry for the lack of markup). If you're going to use this, just copy it all, that will have the right line breaks.
I've tested this and it works beautifully. It's good for less technical users who are trying to sample couple songs (it pops up a totem window for each song that has to be closed by the user manually -- this can be further improved with totem command line options). My original effort was aimed at converting Amazon track sample database into online radio of sorts... it worked well enough for as long as I wanted to listen to it and I'm sure next time I'm shopping for music, I'll use it again.
David Svoboda's Playamazon script:
#!/bin/sh
# A variant of Jan Kokoska's playamazon shell function, a bit cleaner,
# and lets user enter the URL of the Amazon CD (which is prob easier
# than finding an ASIN :)
# Also doesn't use mplayer. Requires zenity, totem, perl, w32-codecs
# (for wma compatibility)
URL=`zenity --title PlayAmazon --entry --text="Enter the Amazon URL of the CD to sample:"`
CD=`echo $URL | perl -n -e 'split "/"; print $_[5];'`
CDNAME=`echo $URL | perl -n -e 'split "/"; print $_[3];'`
VOL=1
echo "Sampling $CDNAME (ASIN=$CD)"
for TRACK in $(seq -w 1 20); do
totem http://www.amazon.com$(wget -q -O - \
http://www.amazon.com/gp/music/wma-pop-up/${CD}00${VOL}0${TRACK} \
| grep URL | cut -d\" -f4) 2>&1 | grep "name:";
done;
I've modified David Svoboda's script above to download multiple samples at once with GNU MIMMS. It's presently set to use 3 connections at once, but I've found that I can get this up to 5. YMMV.
It stores all the samples in the /tmp directory as wmv files. It also creates a plain text playlist file (m3u) in the /tmp directory.
You can find it here:
http://pself.com/playamazon.sh
It works! There's a blind delay for a few seconds first but be patient :)
Yea, I should have mentioned the delay, that's buffering the stream. You can have full detail, but it's pages of junk cluttering up the terminal, this way instead it almost looks user-friendly!
Hey Jan, it's kool can I give the url to your Dig to others ?? I think a lot of linux users will appreciate it.
If you agree send me a email and I'll stick it on the Blag forum.
Hey Vasco, thanks! The Digg url is here:
http
digg.com/linux_unix/Playing_Amazon_music_samples_on_Linux
Looping over multiple albums with the amazon_playlist doesn't work so well, I might debug it later. Playing one album, single cd or more cds, works fine for every one I tried. I bought 3 cds for Xmas through this so that worked out.