Category: Audio Related Programs
Author/Contact Info All_Star25
Description: This piece of code takes in a directory as an argument, goes through the directory (but does not include subdirectories), and creates a HTML playlist, with a hyperlink for each entry.
This hyperlink could be customized to be a link to a download, or in the case that I have used and tested, a link to a CGI script that queues a song in a playlist (useful for Shoutcast radio). The desired customization can be easily achieved by altering the $directory, $baselink, and $filename variables declared near the beginning of the script.
#!/usr/bin/perl -w
use strict;
use MP3::Info;
use Ogg::Vorbis::Header::PurePerl;

my $directory  = "insert_directory_here";
my $baselink   = "http://insert_website_here/cgi-bin/queuesong.pl?song
+=";
my $filename   = "playlist.html";
my $targetfile = $directory . $filename;

# takes in a parameter which is the name of the directory
opendir( FILEDIR, $ARGV[0] ) || die "Cannot open directory";
my @FILES = grep( !/^\.\.}$/, readdir FILEDIR );
open( HTMLFILE, ">$targetfile" );
print HTMLFILE "Perl Generated Playlist<BR>\n\r";
close(HTMLFILE);

open( HTMLFILE2, ">>$targetfile" );
my $filecount = 0;
chdir $ARGV[0];
foreach my $file (@FILES) {
    $_ = $file;
    my $success = 0;
    my $length  = "00:00";

    # mp3/ogg reading stuff goes here
    if (/[Mm][Pp]3$/) {
        my $info = get_mp3info($file);
        $length = $info->{TIME};
        $success++;
    }
    elsif (/[Oo][Gg][Gg]$/) {
        my $ogg = Ogg::Vorbis::Header::PurePerl->load($file)
          or die "Couldn't load $file";

        if ( $ogg->info->{LENGTH} >= 6000 ) {
            $length = sprintf( "%3d:%2d",
                ( $ogg->info->{LENGTH} / 60 ),
                ( $ogg->info->{LENGTH} % 60 ) );
        }
        elsif ( $ogg->info->{LENGTH} >= 600 ) {
            $length = sprintf( "%2d:%2d",
                ( $ogg->info->{LENGTH} / 60 ),
                ( $ogg->info->{LENGTH} % 60 ) );
        }
        else {
            $length = sprintf( "%1d:%2d",
                ( $ogg->info->{LENGTH} / 60 ),
                ( $ogg->info->{LENGTH} % 60 ) );
        }

        $success++;
    }

    if ( $success > 0 ) {
        $filecount++;
        my $link = "<a href=\"$baselink$_\">";
        s/\.[Mm][Pp]3$//;    #strip the file endings
        s/\.[Oo][Gg][Gg]$//;
        print HTMLFILE2 "$filecount. $link$_</a> ($length)<BR>\n\r";
    }

}
close(HTMLFILE2);