http://qs1969.pair.com?node_id=711244

I've already posted a node to CUFP about an MP3 shuffler. This is solving a different problem. The other week I've succeeded in getting audio working on one of my Linux boxes - one with a reasonable sound card. The issue is that my music collection is on a different machine.

Having rejected NFS and Samba shares as an option for getting at the mp3 files, I explore the various options for playing audio, and discover that Kaffeine can take a URL. Excellent! I tweak the server's Apache config, and can now serve music files to my Linux box, when I point a browser at the mp3. Pasting the URL into Kaffeine wasn't completely successful as it couldn't cope with URL escaping, so I needed to manually replace all the %20 with spaces.

Next, I'm thinking that what would be really nice is to prime Kaffeine with a playlist of random tracks from my music collection. Having dug around the file system, I couldn't find where the playlist is stored, but eventually, I realised this was the wrong approach, and discovered the "Save" and "Import" functions. What do they save playlists as? Answer: XML! Bingo! I can now write some Perl to automate the whole process: discover the albums and artists on my server, and make some random selections. Save the URLs, unescaped as an XML file and Bob's your old man's brother.

#!/usr/bin/perl use strict; use warnings; use WWW::Mechanize; use XML::Writer; use IO::File; use URI::Escape; use Getopt::Long; my $server = 'http://orinoco/musicfiles/'; my $ntracks = 10; my $outfile = 'rand.kaffeine'; GetOptions( 'server=s' => \$server, 'tracks=i' => \$ntracks, 'output=s' => \$outfile, ); my $mech = WWW::Mechanize->new; $mech->get($server); my @artists = $mech->find_all_links(text_regex => qr(/$)); my @pick; for (1..$ntracks) { my $rart = int(rand(@artists)); $mech->follow_link( text => $artists[$rart]->text ); my @albums = $mech->find_all_links(text_regex => qr(/$)); my $ralb = int(rand(@albums)); $mech->follow_link( text => $albums[$ralb]->text ); my @tracks = $mech->find_all_links(text_regex => qr(^\d\d)); my $rtk = int(rand(@tracks)); push @pick, $tracks[$rtk]; $mech->back; $mech->back; } my $out = IO::File->new(">$outfile"); print $out "<!DOCTYPE XMLPlaylist>\n"; # Horrible hack: XML::Writer is + too strict my $xml = XML::Writer->new( OUTPUT => $out ); #$xml->doctype('XMLPlaylist', undef, undef); $xml->startTag( 'playlist', client => 'kaffeine'); for my $track (@pick) { $xml->emptyTag( 'entry', url=>uri_unescape($track->url_abs)); } $xml->endTag('playlist'); $xml->end;

Enjoy

--
wetware hacker
(Qualified NLP Trainer and Hypnotherapist)

Replies are listed 'Best First'.
OT Comment on: Random tune picker for Kaffeine
by hawtin (Prior) on Sep 14, 2008 at 17:37 UTC

    Just out of interest why did you reject Samba/ NFS? On my home network I have a "music server" that runs a DAAP server (compatible with iTunes, SoundBridge devices and other clients), it is called FireFly and I have found it great for many clients over the last couple of years. However there are also some times when I need to be able to see the files in a read-only, MS Windows compatible form and for those I also have a Samba share.

    Like you I also produce playlists using Perl, for example picking all my mp3s that were hits in the 1930s, or number 1s in Australia in the 1970s (I have a stupidly large collection of music charts). In these cases I always find that I need to output playlists in many forms, as m3u, XML, lists of files and CSV listings for example.

    You might consider extending your example to use some other source to score each track then randomly selecting N from the top M in some specific order and finally outputting your results in multiple forms.

      The reasons for rejecting Samba and NFS were mainly around how much time and effort I am willing to invest in getting something working. There's a router doing NAT between the two boxes (don't ask), and I've not managed to get an NFS mount to happen through this connection.

      I've had lots of success using Samba from my server, when using it with my WinXP laptop. That's been the primary way of playing music so far. Using smbclient, I've been able to get FTP style access to copy individual files, but I wasn't aware that you could mount external smb volumes on a Linux system. If this is possible, please let me know how to do it, and what the fstab entry looks like.

      I'll look into Firefly, that sounds cool.

      I know I could make my app do more than pick 10 random tracks - this is just a start.

      --
      wetware hacker
      (Qualified NLP Trainer and Hypnotherapist)

        I wasn't aware that you could mount external smb volumes on a Linux system.

        Well it's possible since kernel 2.2 at the very least :) Looks like mount -t cifs -o username=name,password=password //server/share /mount/point Same thing in fstab :

        //server/share /mount/point cifs username=user,password=pass 0 0