#!/usr/bin/perl use strict; use Audio::FLAC::Header; my $t; my @t = qw(year 31557600 month 2627994 week 604800 day 86400 hour 3600 minute 60); die "Usage: $0 /path/to/flac /other/path/to/flac" unless length($ARGV[0]||''); for (@ARGV) { $t+=process_dir($_); } print "\nTotal: " . sane_time($t) . " ($t secs/" . sprintf("%0.3f",($t/(60*60*24*365.25))) . " years)\n"; sub flac_inf { my $f = shift; next unless $f =~/\.flac$/; #warn "flac_inf with $f"; my ($info, $t, $flach); if ($flach = Audio::FLAC::Header->new($f)) { $info = $flach->info(); return $$info{TOTALSAMPLES} / $$info{SAMPLERATE}; } warn "Can't get header for $f"; return 0; } sub process_dir { my $dir = shift; my $t = 0; my $d; $dir =~s#/+#/#og; #warn "process_dir with $dir\n"; if (! -d $dir) { #warn "$dir isn't a directory"; $d = flac_inf($dir); print "$dir: " . sane_time($d) . "\n"; return $d; } opendir(D, $dir); for (sort grep !/^\.\.?$/, readdir(D)) { $t += -d "$dir/$_" ? process_dir("$dir/$_") : flac_inf("$dir/$_"); } closedir(D); print "$dir: " . sane_time($t) . "\n"; return $t; } sub sane_time { my $t = shift; my ($u, $v, @o, $c); my @x=@t; # nasty while ($u = shift @x) { $v = shift @x; $c = int($t/$v); if ($c) { $t-=$c*$v; push @o, plural($c, $u); } } push @o, plural(int($t),'second'); return (join(', ',@o[0 .. $#o - 1])) . ' and ' . $o[-1]; } sub plural { return "@_[0] @_[1]" . (@_[0] != 1 ? 's' : ''); }