#!/usr/bin/perl use strict; ###############################################3 # # THIS SCRIPT MAY REMOVE FILES FROM YOUR SYSTEM, MAKE A BACKUP FIRST. USE AT YOUR OWN RISK # # Simple digital music m3u-producing perl/shell script # # REQUIRES: Perl, GNU find, GNU xargs (most Linux systems have these by default) # # I use this to help organize my flac collection and make m3u files. # (Free lossless audio codec: http://flac.sf.net/) # It should work for ogg, mp3 or any other files you can wrangle into a m3u # # You may feel free to use it for personal use only under the terms of the GNU GPL # a copy of which can be found at http://gnu.org/copyleft/gpl.html # # SUGGESTED USAGE: ------------------------------------------------------------------------------- # # Place this script in the directory below your music files. Here's how I lay mine out: # # All # Blues # Classical # Compilations # Guitar # Jazz # Rock # # Example path: tunes/All/Zappa, Frank/Weasels Ripped My Flesh/the_mothers_of_invention-09_oh_no.flac # # Only "All" has the actual files under it, and the others contain relative links to the # other files under All based on the genre of the group. I find this to alllow the most # flexibility in making playlists. You should do the same if you want to use this script # in unmodified form. # # Set the parameters below and run 'perl mkm3us.pl' in the directory just below your 'All' # folder to set up all your m3u files automatically. # # Any deviations from this setup will require modifications, but hopefully it's not too # hard to understand. # # Copyright 2005 Matthew Steven http://www.matts.org/ # ###############################################3 # Config. 0 means no and 1 means yes # Tell us everything you can about what you are doing, script my $verbose=1; # Remove all existing m3u's before starting my $cleanrun=1; # Make all m3us from the files in the directories below my $makeall=1; # List of folders containing your music and links to your music folders. Do not include folder 'All' here. my @genres=('Blues','Classical','Compilations','Guitar','Jazz','Rock'); # The name of the folder containing actual music files. my $allfolder='All'; # The extention your files all have. They need to be the same, or you need to get hacking my $ext = 'flac'; # The rest you can change only if you really know what you're doing... ################################################3 my $startdir=`pwd`; chomp $startdir; chdir "$startdir/$allfolder"; print "Getting started in ".`pwd`; if($cleanrun){ print "Flushing any existing m3u's under $startdir in 10 seconds.... \nCTRL-C to interrupt if you're not sure..."; `sleep 10s`; `find $startdir -name '*.m3u' -print0|xargs -0 rm -f `; } if(! -f "$startdir/$allfolder/All-All.m3u"){ print "No all-music file found, building it...\n"; `find . -name "*.$ext" | perl -pe "s:^\.\/::g" > $startdir/$allfolder/All-All.m3u`; } if($makeall){ print "Making m3u files based on the contents of $startdir/$allfolder/All-All.m3u\n"; open(F,"<$startdir/$allfolder/All-All.m3u") or die $!; while(){ chomp; /^(.*)\/([^\/]+$ext)$/; my $albumname = $1; my $song = $2; my $on=$albumname; $albumname =~ s/[^\-_0-9a-zA-Z]/_/g; `echo "$on/$song" >> "$startdir/$allfolder/$albumname.m3u"`; print "$song added to $albumname\n" if $verbose; foreach my $g (@genres){ if(-e "../$g/$on" && ! -f "../$g/$albumname.m3u"){ `cd ../$g && ln -s ../$allfolder/$albumname.m3u && cd ../$allfolder`; print "Linked $albumname to $g\n" if $verbose; } } } close(F); } print "Making overall m3u files for each subfolder...\n"; push @genres, $allfolder; foreach my $g (@genres){ `rm -f $startdir/$g/All-$g.m3u $startdir/All-$g.m3u`; `cd $startdir/$g && cat *.m3u > All-$g.m3u`; open(F,"<$startdir/$g/All-$g.m3u") or die $!; while(){ chomp; `echo "$g/$_" >> $startdir/All-$g.m3u`; } close(F); } print "All done."; exit;