#!/usr/bin/perl -w # Perl MP3 Playlist Generator # (c) 2003 J@hacked.it www.jannis.to # # Originally downloaded from http://www.jannis.to/LuFu.pl # March 25, 2004 by Kevin P. Inscoe (kevin@inscoe.org) # Modified for http use and fixed some bugs. # # Notes on the m3u file format can be found at: # http://hanna.pyxidis.org/tech/m3u.html & # http://forums.winamp.com/showthread.php?s=&threadid=65772 # use MP3::Info; $|=1; if ($#ARGV < 1) { print "Usage: LuFu.pl path file [getinfo] [Base-URI]\n"; print " -path: the directory to scan e.g. /home/j/mnt\n"; print " -file: the m3u file to write to e.g. /home/j/all.m3u\n"; print " -getinfo: whether or not to scan extra information from each\n"; print " mp3 and write it to the playlist (0=no, 1=yes)\n"; print " -URI: Base URI for remote access (will be used instead of path names)"; exit } print "Searching directories in ".$ARGV[0]; open(PL, ">".$ARGV[1]) || die "\nFatal: Can't open ".$ARGV[1].": $!\n"; print PL "#EXTM3U\n"; # Init some varaibles my $i = 0; my @queue = (); my @indexed = (); my $q = 0; my $URI = "0"; if ($#ARGV>2) { $URI = $ARGV[3]; print "\nBase URI=$URI\n\n";} # First one to check push (@queue, $ARGV[0]); # Find all directories while ($#queue >=0) { my $c = shift @queue; opendir(D, $c); push(@indexed, $c); while ($_ = readdir(D)) { next unless -d "$c/$_" && $_ ne '.' && $_ ne '..'; push(@queue, "$c/$_"); if ($#queue > $q) { $q = $#queue;} } print "."; closedir(D); } print "\nDone, I will now look for mp3s:\n"; # Find all mp3s foreach $dir (@indexed) { print "-> adding from $dir"; opendir(D, $dir); while ($_ = readdir(D)) { next unless $_ =~ /\.mp3$/i && -f "$dir/$_"; # # If a URI use that and replace base directory otherwise just write $dir # my $out = ""; if ( $URI eq "0" ) { $out = "$dir/$_"; } else { my $basedir = $ARGV[0]; my $len = length($basedir); my $tagdir = substr($dir,$len); print "tagdir=$tagdir\n"; $out = "$URI$tagdir/$_"; } # Scan extra info? if ($#ARGV>1 && $ARGV[2]==1) { my $info = new MP3::Info "$dir/$_"; if (defined($info) && defined($info->secs) && defined($info->title)) { my $o = $info->secs; $o =~ s/([^\.]*)\.[^\.]*/$1/; my $tmp = sprintf "#EXTINF:%s,%s,%s,\n%s", $o, $info->title, $out, $out; $out = $tmp; } } print PL "$out\n"; print "."; $i++; } closedir(D); print "\n"; } print "\nDone. Indexed $i MP3s out of ".($#indexed+1)." directories ($q).\n"; close(PL);