#!/usr/bin/perl ################################################################################ # Author: Blaxter (blaxter@gmail.com) # # Date: 2006-02-19 # # Needed: # # apt-get install libmp3-info-perl libterm-progressbar-perl # # # # GNU GPL license # # # ################################################################################ use MP3::Info; use Term::ProgressBar; my $width=60; my $step; my $B=128; # Bitrate max of the mp3's my @dirsToOpen; # Dirs must be checked my @mp3s; # mp3's of the actual dir my $info; # for read the mp3 with MP3::Info my $dirActual; # obviously the actual dir my @dirs; # Directories which have mp3 with bitrate > $B my %hashB; my $added = 0; my $roundMode = 0; ################################################################################ sub PWD { local $tmp = `pwd`; chop $tmp; $tmp; } sub nfiles { local $actual = &PWD, $n = 0; @files = `ls -R -1 "$actual"`; foreach (@files) { $n++ if(/\.mp3$/io); } $n; } sub imprime { print "\t$_\n" foreach(@_); } sub muerete { die "uso: $0 [-r|--round] Directorio_Base\n"; } sub takeDir { push @dirsToOpen, $ARGV[@_]; &muerete if(!-d $ARGV[@_]); } sub takeMode { if($ARGV[0] == "-r" || $ARGV[0] == "--round") { $roundMode = 1; } else { &muerete; } } sub traduce { local($b) = @_; return ' < 112' if($b < 112); return '112' if($b < ((128-112)/2 + 112)); return '128' if($b < ((160-128)/2 + 128)); return '160' if($b < ((192-160)/2 + 160)); return '192' if($b < ((256-192)/2 + 192)); return '256' if($b <= 256); return '> 256'; } ################################################################################ if(scalar(@ARGV) < 1) { &muerete; } else { &takeMode if(scalar(@ARGV) == 2); &takeDir(scalar(@ARGV)-1); } print "\t*Analizando los mp3's del directorio ".&PWD."\n"; my $total_n = &nfiles; my $next_update = 0; my $n = 0; my $progress = Term::ProgressBar->new( { name => 'Analisis', count => $total_n, ETA => linear }) ; $progress->minor(0); $progress->max_update_rate(1); while(scalar(@dirsToOpen) > 0) { $dirActual = shift(@dirsToOpen); opendir(DIR, $dirActual) || die "You cannot read '$dirActual'! OMG!"; foreach(readdir(DIR)) { $next_update = $progress->update($n)if $n >= $next_update; if($_ eq "." || $_ eq "..") { next; # actual dir and before one, NOT thx } if(-d "$dirActual/$_") { push @dirsToOpen, "$dirActual/$_"; } elsif(/\.mp3$/io) { $n++; $info = get_mp3info("$dirActual/$_"); if($added != 1 && $info->{BITRATE} > $B) { push @dirs, $dirActual; $added = 1; } if(!defined $hashB{$info->{BITRATE}}) { $hashB{$info->{BITRATE}} = 1; } else { $hashB{$info->{BITRATE}}++; } } } close(DIR); $added = 0; } $progress->update($total_n); if(scalar(@dirs) > 0) { print "Dirs with mp3's with bitrate > $B\n"; &imprime(@dirs); } print "\n\n"; $step = $n/$width; if ($roundMode == 1) { %hashR = ( ' < 112' => 0, '112' => 0, '128' => 0, '160' => 0, '192' => 0, '256' => 0, '> 256' => 0 ); foreach my $key (keys %hashB) { $hashR{&traduce($key)} += $hashB{$key}; } %hashB = %hashR; } foreach $key (sort (keys %hashB)) { printf "Con bitrate $key: $hashB{$key} (%.1f \%)\t", $hashB{$key}/$n*100.0 if ($hashB{$key} > 0); for($i=0;$i<($hashB{$key}/$step);$i++) { if(($hashB{$key} - $step*$i)<= $step/2){ print "-";}else{print "=";} } print ">\n" if ($hashB{$key} > 0); } print "\t\t-Un total de $n mp3s analizados\n"; exit 0;