#!/usr/bin/perl -w use strict; use MP3::Info; use Ogg::Vorbis::Header::PurePerl; use Spreadsheet::WriteExcel; my @FILELIST; opendir( DIRECTORY, $ARGV[0] ) || die "Cannot open directory"; @FILELIST = grep( !/^\.\.?}$/, readdir DIRECTORY ); close(DIRECTORY); my $excelfilename = $ARGV[1]; my $excelbook = Spreadsheet::WriteExcel->new($excelfilename); my $excelsheet = $excelbook->add_worksheet(); my $processedcount = 0; print "Processing"; chdir $ARGV[0]; foreach (@FILELIST) { my $file = $_; my $artist = ""; my $title = ""; my $filetype = ""; my $bitrate = 0; my $length = ""; my $size = 0; my $stat = ""; $stat = ( ( stat($file) )[7] ); $stat /= ( 1024 * 1024 ); if ( $stat >= 10 ) { $size = sprintf( "%4.2f", $stat ); } else { $size = sprintf( "%3.2f", $stat ); } $filetype = uc( substr( $file, -3 ) ); if ( $filetype eq "MP3" ) { my $success = 1; my $info = get_mp3info($file); $bitrate = $info->{BITRATE}; $length = $info->{TIME}; my $tag = get_mp3tag($file) or $success = 0; if ( $success == 1 ) { #ID3 tags have been found $artist = $tag->{ARTIST}; $title = $tag->{TITLE}; } if ( $success == 0 ) { #absence of ID3 tags $_ = $file; #be on the safe side s/\.[Mm][Pp]3$//; #strip the file ending if (m/\s-\s/) { # MP3 is in "Artist - Title.mp3" for +mat my @array = split( / - /, $_ ); my $temptitle = ""; $artist = $array[0]; for ( my $index = 1 ; $index < @array ; $index++ ) { $temptitle = $temptitle . $array[$index]; } $title = $temptitle; } else # MP3 has no artist listed { $title = substr( $file, 0, ( length($file) - 4 ) ); } } } if ( $filetype eq "OGG" ) { my $success = 1; my $ogg = Ogg::Vorbis::Header::PurePerl->load($file); $bitrate = $ogg->info->{BITRATE_NOMINAL}; 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 ) ); } $_ = $file; #be on the safe side s/\.[Oo][Gg][Gg]$//; #strip the file ending if (m/\s-\s/) { # OGG is in "Artist - Title.ogg" forma +t my @array = split( / - /, $_ ); my $temptitle = ""; $artist = $array[0]; for ( my $index = 1 ; $index < @array ; $index++ ) { $temptitle = $temptitle . $array[$index]; } $title = $temptitle; } else { # MP3 has no artist listed $title = substr( $file, 0, ( length($file) - 4 ) ) ; #strip off the file extension } } if ( $filetype eq "MP3" || $filetype eq "OGG" ) { $excelsheet->write( $processedcount, 0, $artist ); $excelsheet->write( $processedcount, 1, $title ); $excelsheet->write( $processedcount, 2, $length ); $excelsheet->write( $processedcount, 3, $bitrate ); $excelsheet->write( $processedcount, 4, $size ); $excelsheet->write( $processedcount, 5, $filetype ); $excelsheet->write( $processedcount, 6, $file ); $processedcount++; if ( ( $processedcount % 100 ) == 0 ) { print "."; } } } $excelbook->close();

In reply to MP3/OGG Data to .xls by All_Star25

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.