#!/usr/bin/perl -w # # id3list v0.1 # by Christian Birchinger # released under the BSD license my $dir='mp3incoming'; my @numvalues = sort qw(seconds len size sizemb mp3version frequency mpeglayer bitrate mode); my @alphavalues = sort qw(title artist album year track comment vbr copyrighted stereo); my @allvalues = sort (@numvalues,@alphavalues); my @sortorder = qw(artist title); use MP3::Info; use strict; opendir(DIR, $dir) || die("couldn't open dir $dir: $!"); my @files = sort grep { /\.mp3$/i } readdir(DIR); my ($mp3file,$totalsize,$totalsec); my %mp3; foreach $mp3file (@files) { # Get id3v2 and id3v1 info from file my $fullpath = "${dir}/${mp3file}"; my $mp3info = get_mp3info($fullpath); my $id3tags = get_mp3tag($fullpath); my $id3v2tags = get_mp3tag($fullpath, 2, 1); my $size = (stat($fullpath))[7]; # Use id3v2 tags if present and fallback to id3v1 tags if not if ( $id3v2tags->{TIT2} ) { $mp3{$mp3file}->{title} = $id3v2tags->{TIT2} } else { $mp3{$mp3file}->{title} = $id3tags->{TITLE} } if ( $id3v2tags->{TPE1} ) { $mp3{$mp3file}->{artist} = $id3v2tags->{TPE1} } else { $mp3{$mp3file}->{artist} = $id3tags->{ARTIST} } if ( $id3v2tags->{TALB} ) { $mp3{$mp3file}->{album} = $id3v2tags->{TALB} } else { $mp3{$mp3file}->{album} = $id3tags->{ALBUM} } if ( $id3v2tags->{TYER} ) { $mp3{$mp3file}->{year} = $id3v2tags->{TYER} } else { $mp3{$mp3file}->{year} = $id3tags->{YEAR} } if ( $id3v2tags->{TRCK} ) { $mp3{$mp3file}->{track} = $id3v2tags->{TRCK} } else { $mp3{$mp3file}->{track} = $id3tags->{TRACKNUM} } if ( $id3v2tags->{COMM} ) { $mp3{$mp3file}->{comment} = $id3v2tags->{COMM} } else { $mp3{$mp3file}->{comment} = $id3tags->{COMMENT} } if ( $id3v2tags->{TCON} ) { ($mp3{$mp3file}->{genre} = $id3v2tags->{TCON}) =~ s/^\(\d+\)// } else { ($mp3{$mp3file}->{genre} = $id3tags->{GENRE}) =~ s/^\(\d+\)// } # read generic mp3file info $mp3{$mp3file}->{bitrate} = $mp3info->{BITRATE}; $mp3{$mp3file}->{vbr} = $mp3info->{VBR} ? 'VBR' : ''; $mp3{$mp3file}->{mode} = $mp3info->{MODE}; $mp3{$mp3file}->{copyrighted} = $mp3info->{COPYRIGHT} ? 'Copyrighted' : 'Not copyrighted'; $mp3{$mp3file}->{frequency} = $mp3info->{FREQUENCY}; $mp3{$mp3file}->{len} = $mp3info->{TIME}; $mp3{$mp3file}->{stereo} = $mp3info->{STEREO} ? 'Stereo' : 'Mono'; $mp3{$mp3file}->{mpeglayer} = $mp3info->{LAYER}; $mp3{$mp3file}->{seconds} = $mp3info->{SECS}; $mp3{$mp3file}->{mp3version} = $mp3info->{VERSION}; $mp3{$mp3file}->{size} = $size; $mp3{$mp3file}->{sizemb} = sprintf("%.2f MB", $size/1024/1024); $totalsize += $size; $totalsec += $mp3info->{SECS}; } my $totalmb = sprintf("%.2f MB", $totalsize/1024/1024); my $totalmin = sprintf("%02u", $totalsec/60); foreach $mp3file (getsortedlist()) { print "$mp3{$mp3file}->{artist} - $mp3{$mp3file}->{title} - $mp3{$mp3file}->{len} - $mp3{$mp3file}->{sizemb} - $mp3{$mp3file}->{bitrate} kbit $mp3{$mp3file}->{vbr}\n"; } print "Total Size: $totalmb - Total Playtime: $totalmin minutes\n"; # --- [ Sort function / returns an array with the sorted keysequence from the %mp3 hash ]----------------------- sub getsortedlist { my @result = keys(%mp3); my ($sortby, $sortvalue, $sortrule, $compop); # The most ugly hack on the planet. Tell me if you have a better solution. foreach $sortvalue (@sortorder) { if ($sortvalue eq 'len') { $sortvalue = 'seconds' }; if ($sortvalue eq 'sizemb') { $sortvalue = 'size' }; if (grep {/^$sortvalue$/} @numvalues) { $compop = '<=>' } else { $compop = 'cmp' }; if ($sortrule) { $sortrule = $sortrule . "|| uc(\$mp3{\$a}->{$sortvalue}) $compop uc(\$mp3{\$b}->{$sortvalue})"; } else { $sortrule = "uc(\$mp3{\$a}->{$sortvalue}) $compop uc(\$mp3{\$b}->{$sortvalue})"; } } @result = sort { eval $sortrule; } @result; return @result; } # --------------------------------------------------------------------------------------------------------------