Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic

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]
[erix]: and now you are agnostic?
[james28909]: arunbear, your picking bits of history to fit your own narrative. right now, humans are smarter than in any of those times.
[james28909]: in order for there to be a mutation, there has to be a universe with laws setup that even allow a mutation.
[james28909]: everything past the first two things you piked from my posts are irrelevant to me and my beliefs. they are side effects.
[james28909]: two or three. if it has to do with how humans are handling said situations, then that is after they were able to evolve into those problems. agnostic means only believe with evidence. i took that from a tyson video
[james28909]: and it seems that racism suggest that there is indeed a better race, but that is for evolution to decide

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (12)
As of 2017-12-15 14:55 GMT
Find Nodes?
    Voting Booth?
    What programming language do you hate the most?

    Results (433 votes). Check out past polls.