Description: |
This script takes a directory name as the first argument, parses through it, outputting the artist, title, length, bitrate, filetype, and filename of each MP3 or OGG file in the directory as columns in an Excel spreadsheet (the filename of which is determined by the second argument). As a method of keeping track of progress, it adds a dot to "Processing" after every 100 entries.
Known issues:
Files without tags do not work so well.
Some OGG/MP3 data may not be entered in some cases, including certain types of VBR encoding ( this is a limitation of the modules used ).
Uses MP3::Info, Ogg::Vorbis::Header::PurePerl, and Spreadsheet::WriteExcel;. Tested with ActivePerl build 810. |
#!/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();
|