Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Create a playlist for

by gav^ (Curate)
on Dec 31, 2001 at 06:50 UTC ( #135318=sourcecode: print w/ replies, xml ) Need Help??

Category: Audio Related Programs
Author/Contact Info Gavin Estey, gavin@estey.com
Description: I was fed up of not having a playlist in each directory of mp3s, mainly as I'm lazy and just want to click on the .m3u file :) I also had a stack of playlists that had full paths which broke things when I moved stuff around.

This script with create a playlist in each directory that contains mp3s, nothing else exciting.

use strict;
use warnings;
use File::Basename;

my $del_m3u = 1;

my $dir = $ARGV[0] || die "You need to provide a directory";

handle_dir($dir);

exit;

sub handle_dir {
    
    my $dir     = shift;
    my @songs     = ();
    
    print "Dir: $dir\n";
    
    opendir DIR, $dir or die "Unable to open dir -- $!";

    foreach my $file (sort readdir DIR) {
        if (-d "$dir/$file") {
            next if $file =~ /^\.+$/;
            handle_dir("$dir/$file")
        } elsif ($file =~ /\.[Mm]3[Uu]$/ && $del_m3u) {
            unlink "$dir/$file" or die "Unable to delete $file -- $!";
        } elsif ($file =~ /\.[Mm][Pp]3$/) {
            push @songs, $file;
        }
    }

    closedir DIR;

    my $playlist = $dir . "/" . basename($dir) . ".m3u";
    
    if (@songs > 0) {        
        open OUT, ">$playlist" or die "Unable to write to $playlist --
+ $!";    
        print OUT $_, "\n" foreach @songs;    
        close OUT;
    }
    
}

Comment on Create a playlist for
Download Code
Re: Create a playlist for
by gav^ (Curate) on Dec 31, 2001 at 09:14 UTC
    Doh, sorry for the double post. Not sure what happened.

    I just had a thought, what about making the playlists for a directory also contain all the mp3s? So that if you had c:/mp3s/cd/1 and c:/mp3s/cd/2 you'd end up with a m3u in c:/mp3s/cd that had both dirs in it's playlist.

    Just add a: return @songs; and change handle_dir("$dir/$file"); to unshift @songs, handle_dir("$dir/$file");

Re: Create a playlist for
by NetBSD (Initiate) on Jun 03, 2005 at 02:09 UTC
    awsome dude, ive been looking for something like this for months, and i couldnt figure out how to code it, thanks for coding it.

Back to Code Catacombs

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: sourcecode [id://135318]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (3)
As of 2014-07-29 02:08 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (211 votes), past polls