1
0
Fork 0
forked from len0rd/rockbox

Added progress display and summary. Implemented --strip.

git-svn-id: svn://svn.rockbox.org/rockbox/trunk@5573 a1c6a512-1295-4272-9138-f99709370657
This commit is contained in:
Björn Stenberg 2005-01-17 11:18:44 +00:00
parent 5bc7e9a952
commit 025f5c0084

View file

@ -10,9 +10,11 @@
use MP3::Info; use MP3::Info;
my $db; my $db = "rockbox.id3db";
my $dir; my $dir = ".";
my $strip; my $strip;
my $verbose;
my $help;
while($ARGV[0]) { while($ARGV[0]) {
if($ARGV[0] eq "--db") { if($ARGV[0] eq "--db") {
@ -30,6 +32,14 @@ while($ARGV[0]) {
shift @ARGV; shift @ARGV;
shift @ARGV; shift @ARGV;
} }
elsif($ARGV[0] eq "--verbose") {
$verbose = 1;
shift @ARGV;
}
elsif($ARGV[0] eq "--help" or ($ARGV[0] eq "-h")) {
$help = 1;
shift @ARGV;
}
else { else {
shift @ARGV; shift @ARGV;
} }
@ -42,9 +52,9 @@ my %filename;
my $dbver = 1; my $dbver = 1;
if(! -d $dir) { if(! -d $dir or $help) {
print "songdb [--db <file>] --path <dir> [--strip <path>]\n"; print "'$dir' is not a directory\n" if (! -d $dir);
print "given argument is not a directory!\n"; print "songdb [--db <file>] [--path <dir>] [--strip <path>] [--verbose] [--help]\n";
exit; exit;
} }
@ -100,6 +110,8 @@ my $maxsongperalbum;
sub dodir { sub dodir {
my ($dir)=@_; my ($dir)=@_;
print "$dir\n";
# getdir() returns all entries in the given dir # getdir() returns all entries in the given dir
my @a = getdir($dir); my @a = getdir($dir);
@ -120,15 +132,26 @@ sub dodir {
# TRACKNUM # TRACKNUM
# YEAR # YEAR
# don't index songs without tags
if (not defined $$id3{'ARTIST'} and
not defined $$id3{'ALBUM'} and
not defined $$id3{'TITLE'})
{
next;
}
#printf "Artist: %s\n", $id3->{'ARTIST'}; #printf "Artist: %s\n", $id3->{'ARTIST'};
$entries{"$dir/$f"}= $id3; my $path = "$dir/$f";
$filename{$id3}="$dir/$f"; if ($strip ne "" and $path =~ /^$strip(.*)/) {
$path = $1;
}
$entries{$path}= $id3;
$artists{$id3->{'ARTIST'}}++ if($id3->{'ARTIST'}); $artists{$id3->{'ARTIST'}}++ if($id3->{'ARTIST'});
$genres{$id3->{'GENRE'}}++ if($id3->{'GENRE'}); $genres{$id3->{'GENRE'}}++ if($id3->{'GENRE'});
$years{$id3->{'YEAR'}}++ if($id3->{'YEAR'}); $years{$id3->{'YEAR'}}++ if($id3->{'YEAR'});
$id3->{'FILE'}="$dir/$f"; # store file name # fallback names
$$id3{'ARTIST'} = "<no artist tag>" if ($$id3{'ARTIST'} eq ""); $$id3{'ARTIST'} = "<no artist tag>" if ($$id3{'ARTIST'} eq "");
$$id3{'ALBUM'} = "<no album tag>" if ($$id3{'ALBUM'} eq ""); $$id3{'ALBUM'} = "<no album tag>" if ($$id3{'ALBUM'} eq "");
$$id3{'TITLE'} = "<no title tag>" if ($$id3{'TITLE'} eq ""); $$id3{'TITLE'} = "<no title tag>" if ($$id3{'TITLE'} eq "");
@ -136,10 +159,11 @@ sub dodir {
# prepend Artist name to handle duplicate album names from other # prepend Artist name to handle duplicate album names from other
# artists # artists
my $albumid = $id3->{'ALBUM'}."___".$id3->{'ARTIST'}; my $albumid = $id3->{'ALBUM'}."___".$id3->{'ARTIST'};
if($id3->{'ALBUM'}) { if($albumid ne "<no album tag>___<no artist tag>") {
my $num = ++$albums{$albumid}; my $num = ++$albums{$albumid};
if($num > $maxsongperalbum) { if($num > $maxsongperalbum) {
$maxsongperalbum = $num; $maxsongperalbum = $num;
$longestalbum = $albumid;
} }
$album2songs{$albumid}{$$id3{TITLE}} = $id3; $album2songs{$albumid}{$$id3{TITLE}} = $id3;
$artist2albums{$$id3{ARTIST}}{$$id3{ALBUM}} = $id3; $artist2albums{$$id3{ARTIST}}{$$id3{ALBUM}} = $id3;
@ -157,23 +181,25 @@ sub dodir {
dodir($dir); dodir($dir);
print "\n";
print "File name table\n"; print "File name table\n" if ($verbose);
my $fc; my $fc;
for(sort keys %entries) { for(sort keys %entries) {
printf(" %s\n", $_); printf(" %s\n", $_) if ($verbose);
$fc += length($_)+1; $fc += length($_)+1;
} }
my $maxsonglen; my $maxsonglen = 0;
my $sc; my $sc;
print "\nSong title table\n"; print "\nSong title table\n" if ($verbose);
#for(sort {$entries{$a}->{'TITLE'} cmp $entries{$b}->{'TITLE'}} %entries) {
for(sort {$entries{$a}->{'TITLE'} cmp $entries{$b}->{'TITLE'}} keys %entries) { for(sort {$entries{$a}->{'TITLE'} cmp $entries{$b}->{'TITLE'}} keys %entries) {
printf(" %s\n", $entries{$_}->{'TITLE'} ); printf(" %s\n", $entries{$_}->{'TITLE'} ) if ($verbose);
my $l = length($entries{$_}->{'TITLE'}); my $l = length($entries{$_}->{'TITLE'});
if($l > $maxsonglen) { if($l > $maxsonglen) {
$maxsonglen = $l; $maxsonglen = $l;
$longestsong = $entries{$_}->{'TITLE'};
} }
} }
$maxsonglen++; # include zero termination byte $maxsonglen++; # include zero termination byte
@ -181,16 +207,17 @@ while($maxsonglen&3) {
$maxsonglen++; $maxsonglen++;
} }
my $maxartistlen; my $maxartistlen = 0;
print "\nArtist table\n"; print "\nArtist table\n" if ($verbose);
my $i=0; my $i=0;
my %artistcount; my %artistcount;
for(sort keys %artists) { for(sort keys %artists) {
printf(" %s\n", $_); printf(" %s\n", $_) if ($verbose);
$artistcount{$_}=$i++; $artistcount{$_}=$i++;
my $l = length($_); my $l = length($_);
if($l > $maxartistlen) { if($l > $maxartistlen) {
$maxartistlen = $l; $maxartistlen = $l;
$longestartist = $_;
} }
$l = scalar keys %{$artist2albums{$_}}; $l = scalar keys %{$artist2albums{$_}};
@ -203,27 +230,30 @@ while($maxartistlen&3) {
$maxartistlen++; $maxartistlen++;
} }
print "\nGenre table\n"; if ($verbose) {
for(sort keys %genres) { print "\nGenre table\n";
printf(" %s\n", $_); for(sort keys %genres) {
printf(" %s\n", $_);
}
print "\nYear table\n";
for(sort keys %years) {
printf(" %s\n", $_);
}
} }
print "\nYear table\n"; print "\nAlbum table\n" if ($verbose);
for(sort keys %years) { my $maxalbumlen = 0;
printf(" %s\n", $_);
}
print "\nAlbum table\n";
my $maxalbumlen;
my %albumcount; my %albumcount;
$i=0; $i=0;
for(sort keys %albums) { for(sort keys %albums) {
my @moo=split(/___/, $_); my @moo=split(/___/, $_);
printf(" %s\n", $moo[0]); printf(" %s\n", $moo[0]) if ($verbose);
$albumcount{$_} = $i++; $albumcount{$_} = $i++;
my $l = length($moo[0]); my $l = length($moo[0]);
if($l > $maxalbumlen) { if($l > $maxalbumlen) {
$maxalbumlen = $l; $maxalbumlen = $l;
$longestalbumname = $moo[0];
} }
} }
$maxalbumlen++; # include zero termination byte $maxalbumlen++; # include zero termination byte
@ -245,17 +275,25 @@ sub dumpint {
($num&0xff)); ($num&0xff));
} }
if($db) { if (!scalar keys %entries) {
print STDERR "\nCreating db $db\n"; print "No songs found. Did you specify the right --path ?\n";
print "Use the --help parameter to see all options.\n";
exit;
}
if ($db) {
my $songentrysize = $maxsonglen + 12; my $songentrysize = $maxsonglen + 12;
my $albumentrysize = $maxalbumlen + 4 + $maxsongperalbum*4; my $albumentrysize = $maxalbumlen + 4 + $maxsongperalbum*4;
my $artistentrysize = $maxartistlen + $maxalbumsperartist*4; my $artistentrysize = $maxartistlen + $maxalbumsperartist*4;
print STDERR "Max song length: $maxsonglen\n"; printf STDERR "Number of artists : %d\n", scalar keys %artists;
print STDERR "Max album length: $maxalbumlen\n"; printf STDERR "Number of albums : %d\n", scalar keys %albums;
print STDERR "Max artist length: $maxartistlen\n"; printf STDERR "Number of songs : %d\n", scalar keys %entries;
print STDERR "Database version: $dbver\n"; print STDERR "Max artist length : $maxartistlen ($longestartist)\n";
print STDERR "Max album length : $maxalbumlen ($longestalbumname)\n";
print STDERR "Max song length : $maxsonglen ($longestsong)\n";
print STDERR "Max songs per album: $maxsongperalbum ($longestalbum)\n";
print STDERR "Database version: $dbver\n" if ($verbose);
open(DB, ">$db") || die "couldn't make $db"; open(DB, ">$db") || die "couldn't make $db";
printf DB "RDB%c", $dbver; printf DB "RDB%c", $dbver;