Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?

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,
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";



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 =~ /^\.+$/;
        } 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;

Replies are listed 'Best First'.
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.
Log In?

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

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (8)
As of 2019-02-19 10:28 GMT
Find Nodes?
    Voting Booth?
    I use postfix dereferencing ...

    Results (104 votes). Check out past polls.

    • (Sep 10, 2018 at 22:53 UTC) Welcome new users!