HOW-TO: Create Randomizing Music Playlists automatically for Twonky

[Edited]

I created the script below to create “Random” playlists on the MBL.

By using the script below, you can, create a playlist by Genre of all the music in a particular media library.

You can then play this Playlist file on any DLNA client such as the WDTVs with a minimum of fuss.

Here’s how to use it.

  • You must access the MBL via SSH.
  • I’ve tested this on a library of 15,000 MP3 files.
  • All the M3s must have the Genre stored as an ID3V2 tag.  Ones that don’t will be ignored.
  • I’ve included code to properly “escape” most common characters seen (in English filenames, at least.)
  • It took less than an hour to run
  • Use at your own risk
  • This does NOT make ANY changes to the MBL’s internal files, except for adding a CRON link, so it should be no risk to the O/S.
  • This may NOT survive a firmware upgrade.
  • This was tested on firmware 2.11.09-053

First, log in via SSH and create the file “plistcreate.pl” in one of your share directories.  (/shares/…)

Cut and paste this into that file exactly:

#! /usr/bin/perl
### M3U Playlist Creator -- Creates Playlists to Randomize by Genre
### Written by TonyPh12345
### Feel Free to Modify -- but keep the banner above, and comment your own changes.

$pathToMusic = '/shares/Public/Shared Music/iTunes';
$pathToPlaylists = '/shares/Public/Shared Music/AutoPlaylists';

# Make sure BOTH PATHS ABOVE EXIST!

sub shuffle (\@) { #### Subroutine to do the randomization of the playlist
        my $r=pop;
        $a = $_ + rand @{$r} - $_
        and @$r[$_, $a] = @$r[$a, $_]
        for (0..$#{$r});
}

@flist = `find '$pathToMusic' -name '*.mp3'`;

foreach $line (@flist) {
        chomp $line;
        $oline = $line; # Save original unescaped name
        $line =~ s/ /\\ /g; # Escape Spaces...
        $line =~ s/\'/\\\'/g; # Escape Apostrophes
        $line =~ s/\(/\\\(/g; # Escape Opening Parentheses
        $line =~ s/\)/\\\)/g; # Escape Closing Parentheses
        $line =~ s/\&/\\\&/g; # Escape Ampersands
        $line =~ s/\$/\\\$/g; # Escape Dollar Signs
                                        # Warning: Other characters may need escaping!!!
        $genline = `id3v2 -l $line | grep -i TCON`;
        chomp $genline;
        $genline =~ /.+\): (.+) \(/; # REGEX out the Genre name
        $genreName = $1;
        unless ($genline eq '') { # If the file has no ID3 Genre, ignore it and skip.
                unless ($indexes{$genreName} > 0) {
                        $totindex += 1;
                        $indexes{$genreName} = $totindex;
                        $filename[$indexes{$genreName}][0]=0;
                }
                $filename[$indexes{$genreName}][0]+=1;
                $filename[$indexes{$genreName}][$filename[$indexes{$genreName}][0]] = $oline;
        }
}

foreach $genre (keys(%indexes)) {
        undef @fgroup;
        for ($i = 1 ; $i <= $filename[$indexes{$genre}][0] ; $i++) {
                push (@fgroup, $filename[$indexes{$genre}][$i]);
        }
        shuffle(@fgroup);
        my $plistfile = $pathToPlaylists . '/' . 'Random ' . $genre . '.m3u';
        open (MYPLIST, "> $plistfile") or die $!;
        foreach $rand (@fgroup) {
                print MYPLIST "$rand\n";
        }
        close MYPLIST;
}

 Save the file and make it executable:

chmod a+x /root/plistcreate.pl

 Note the two variables at the beginning of the file:

$pathToMusic = '/shares/Public/Shared Music/iTunes';
$pathToPlaylists = '/shares/Public/Shared Music/AutoPlaylists';

 Edit those variables as needed.

The first one is the path to the music file store. 

The second one is where the Playlist Files will be stored.

BOTH PATHS must be in a Twonky-scanned path.

Both paths should BEGIN with the ‘/’ character, but should NOT have one at the end.

That’s all there is to it!

To get started initially (instead of waiting for the first Cron to run), you can execute the script manually:

cd
./plistcreate.pl

 Only errors will be printed, so if all is going well, you won’t see ANY output while it’s running.

Once done, wait a few minutes for Twonky to discover the files (assuming you haven’t modified the Twonky configuration and disabled that), and you should see these Random (GenreName) files in the Playlist Category.

Ooops – I just learned the hard way that, during a FW upgrade, the root home directory is purged.

So don’t install this there – put it in one of your shares.