Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

MP3/OGG Directory Parse & Playlist Generation with Hyperlinks

by All_Star25 (Monk)
on Dec 04, 2004 at 00:53 UTC ( [id://412320]=sourcecode: print w/replies, xml ) Need Help??
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);

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others surveying the Monastery: (3)
As of 2024-04-24 01:35 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found