Monday, November 6, 2006

Perl script to autogenerate playlists from mp3s

I wrote this perl script to autogenerate playlist files (.m3u) for each CD we have. Some servers don't play the songs in CD order, but they will with a playlist.

# assumes mp3 directory structure of g:/mp3/artist/album/tracks.mp3
# generates playlist of format: g:/m3u/artist-album.m3u

$mp3dir="g:/mp3";
$m3udir="g:/m3u";

opendir(ARTIST, $mp3dir);
@dir = grep !/^\.\.?$/, readdir ARTIST;
foreach $artist (@dir) {
print "\nartist: $artist\n";
opendir(ALBUM,"g:/mp3/$artist");
@album = grep !/^\.\.?$/, readdir ALBUM;

foreach $album (@album) {
print " album: $album\n";

opendir(TRACK,"g:/mp3/$artist/$album");
@track=grep /\.mp3/,readdir TRACK;
$playlist="";
foreach $track (@track) {
print " track: $track\n";
$playlist.="g:\\mp3\\$artist\\$album\\$track\n";
}
closedir TRACK;

# create playlist
$m3u_name="$m3udir/$artist-$album.m3u";
$m3u_name=~s/\\ /_/g;
print " m3u: $m3u_name\n\n";
PrivoxyWindowOpen(A,">$m3u_name");
print A $playlist;
close(A);
}
closedir ALBUM;
}

No comments:

Post a Comment