#!/usr/bin/perl ## SanDisk's sansa e200 expects very ## very windows like playlists. ## Difficult to fake on unix derivates. ## This script writes SanDisk sansa e200 ## compatible playlists from your ## and into your .mp3-consisting ## directory tree. ## ## Loosely based on the 'WinAMP M3U ## Sorted Playlist Generator version .0.1' ## by ## ## works fine for me. Lots of TODOS, ## a few FIXMEs ... ## ## version 0.1 ## 2008/Sept/05 use strict; use vars qw(@files %filehash); use File::Find; use MP3::Info; my $usage = "/[path_to]/mkpls.pl \n"; ##set start directory my ($base) = @ARGV; die $usage unless ($base); ##check whether there are further directories ##works for me, might be different for you sub checkdir{ my $return=0; my ($path) = $_[0]; $path=~s/###/ /g; opendir(IMD, $path) || die("Cannot open directory $path"); my @thefiles= readdir(IMD); closedir(IMD); foreach my $file (@thefiles){ unless ($file eq "." || $file eq ".."){ $file= $path."/".$file; if(-d $file ){ $file=~s/ /###/g; $return=1; if(checkdir($file)==0){ callpls($file);} else{}; }; }; } return $return; } ##is called if a directory ist the last leaf sub callpls{ my $path=$_[0]; $path=~s/###/ /g; my $pls_file = (split("/", $path))[-1]; $pls_file=~s/ /_/g; chdir $path; makelist(".", "$path/$pls_file\.m3u"); } ##add audiofile to the list sub addFile { $filehash{$File::Find::name} = $_; return unless -f; return unless /\.mp3$/; push @files, $File::Find::name; } ##write the actual list sub makelist{ my ($source_path, $output_filename) = @_; if ($source_path=~/\\/){ $source_path=~s/\\//g; }; return "Directory not found:: $source_path" unless (-e "$source_path"); unlink $output_filename if (-e $output_filename); print "\n"; @files = (); %filehash = (); find (\&addFile, "$source_path"); my @files = sort {uc($a) cmp uc($b)} @files; open FILE, ">$output_filename" or return "could not open $output_filename for writing: $!"; binmode(FILE, ":utf8"); print FILE "#EXTM3U\r\n"; my $counter = 1; my $max = scalar (@files); foreach my $file (@files) { my $tag = get_mp3tag($file); my $info = get_mp3info($file); my $pair; if (($tag->{ARTIST}) && ($tag->{TITLE})) { $pair = $tag->{ARTIST} . ' - ' . $tag->{TITLE}; } else { $pair = $filehash{$file}; } print FILE "#EXTINF:" . int ($info->{SECS}) . "," . $pair . "\r\n"; $file =~ s/\//\\/g; $file =~ s/\.\\//g; print FILE "$file\r\n\r\n"; print "$counter of $max: $pair\r\n"; $counter++; } close FILE; } ##call the function checkdir($base);