
Untitled
By: a guest on
May 8th, 2012 | syntax:
None | size: 1.29 KB | hits: 16 | expires: Never
#!/opt/local/bin/perl
use 5.010;
use strict;
use warnings;
#use Encode;
use File::Basename qw(basename);
use File::Find qw(finddepth);
my $library = "$ENV{HOME}/Music/Songbird Music";
finddepth(\&action, $library);
sub action {
if (/.flac$/ && -f) {
my $fn = $_;
my $dn = $File::Find::dir;
my %alltags = gettags($fn);
my $albumartist = $alltags{albumartist}->[0];
my $album = $alltags{album}->[0];
my $newpath = "$library/$albumartist/$album";
my $track = $alltags{tracknumber}->[0];
my $title = $alltags{title}->[0];
my $newname = sprintf('%02s. %s.flac', $track, $title);
mvfile($fn, $newname);
}
}
sub gettags {
my $fn = shift;
my %alltags;
open(OUTPUT, "-|", qq{metaflac --no-utf8-convert --export-tags-to=- "$fn"}) || die "can\'t run metaflac: $!";
local $_;
while (<OUTPUT>) {
chomp;
my @tag = split('=');
my $tagname = lc($tag[0]);
$alltags{$tagname} = [] unless exists $alltags{$tagname};
if ($tag[1]) {
$tag[1] =~ tr!/\\<>|:;$*"'!!d;
push @{$alltags{$tagname}}, $tag[1];
} else {
push @{$alltags{$tagname}}, 'null';
}
}
close(OUTPUT) || die "can\'t close metaflac: $!";
return %alltags;
}
sub mvfile {
my $fn = shift;
my $newname = shift;
if ($fn ne $newname) {
say "$fn != $newname";
}
}