#!/usr/bin/perl use strict; use File::Find; my $debug = 3; my $music_base_dir = '/mnt/data/music/'; my $tmp_dir = '/tmp/mkmp3cd/'; my $output_iso = '/tmp/mkmp3cd.iso'; main(); sub main { my $file = shift @ARGV; init_tmp_dir(); open(IN,$file) or die "Couldn't open $file, $!"; debug(1,"Reading song list from $file"); my ($line); while($line = ) { # Strip out comments and extra whitespace chomp $line; $line =~ s/(^#|[^\\]#).*//; $line =~ s/^\s*(.*?)\s*/$1/; next if $line eq ''; debug(3,"Input line: '$line'"); add_thingie($line); } # Now everything is set up inthe tmp_dir, lets make an iso! `mkisofs -J -r -o $output_iso $tmp_dir`; } sub init_tmp_dir { `rm -rf $tmp_dir`; `mkdir $tmp_dir`; } sub debug { my ($level, $msg) = @_; if ($debug >= $level) { print "DEBUG$level: $msg\n"; } } sub add_thingie { my ($thing) = @_; my $loc = $music_base_dir . $thing; debug(3,"Trying to figure out what '$loc' is."); if(-d $loc) { debug(1,"$thing is a directory, adding all contents"); add_dir($thing); } elsif(-f $loc) { debug(1,"$thing is a file, adding it alone"); add_song($thing); } else { debug(1,"I don't know what '$thing' is. Sorry."); } } sub add_dir { my ($dir) = @_; my $loc = $music_base_dir . $dir; my @songs; # recurse through dir, getting list of songs find(sub { push @songs, $File::Find::name }, $loc); @songs = map { s/$music_base_dir//;$_ } @songs; add_song($_) foreach @songs; } sub add_song { my ($song) = @_; my ($artist, $album, $track, $title, $type); my $loc = $music_base_dir . $song; debug(2,"Adding song '$loc'"); # Lets see what we know about this song... if($song =~ /(.*?)\/(.*?)\/.*? - .*? - (\d\d) - (.*)\.(mp3|ogg)/) { # Perfectly formatted! ($artist, $album, $track, $title, $type) = ($1,$2,$3,$4,$5); debug(3,"Found perfect song! $artist/$album/$track - $title ($type)"); copy($loc,$artist,$album,$track,$title,$type); } elsif(-d $loc) { debug(2,"This is a directory... skipping"); } else { debug(2,"Don't know what to do with $song... aborting add"); return; } } sub copy { my ($loc,$artist,$album,$track,$title,$type) = @_; # Make sure we have this directory, and its parents `mkdir -p "$tmp_dir/$artist/$album"`; if($type eq 'ogg') { qx{ ogg123 -d wav -f - "$loc" | lame --vbr-new -b 192 - "$tmp_dir/$artist/$album/$track - $title.mp3" } } elsif ($type eq 'mp3') { print qq{cp "$loc" "$tmp_dir/$artist/$album/$track - $title.$type"\n}; `cp "$loc" "$tmp_dir/$artist/$album/$track - $title.$type"`; } else { debug(1,"Unknown song type, $type, for file $loc"); } }