Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 8th, 2012  |  syntax: None  |  size: 1.29 KB  |  hits: 16  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. #!/opt/local/bin/perl
  2.  
  3. use 5.010;
  4. use strict;
  5. use warnings;
  6. #use Encode;
  7. use File::Basename qw(basename);
  8. use File::Find qw(finddepth);
  9.  
  10. my $library = "$ENV{HOME}/Music/Songbird Music";
  11.  
  12. finddepth(\&action, $library);
  13.  
  14. sub action {
  15.         if (/.flac$/ && -f) {
  16.                 my $fn = $_;
  17.                 my $dn = $File::Find::dir;
  18.                 my %alltags = gettags($fn);
  19.  
  20.                 my $albumartist = $alltags{albumartist}->[0];
  21.                 my $album = $alltags{album}->[0];
  22.                 my $newpath = "$library/$albumartist/$album";
  23.                 my $track = $alltags{tracknumber}->[0];
  24.                 my $title = $alltags{title}->[0];
  25.                 my $newname = sprintf('%02s. %s.flac', $track, $title);
  26.                 mvfile($fn, $newname);
  27.         }
  28. }
  29.  
  30. sub gettags {
  31.         my $fn = shift;
  32.         my %alltags;
  33.         open(OUTPUT, "-|", qq{metaflac --no-utf8-convert --export-tags-to=- "$fn"}) || die "can\'t run metaflac: $!";
  34.         local $_;
  35.         while (<OUTPUT>) {
  36.                 chomp;
  37.                 my @tag = split('=');
  38.                 my $tagname = lc($tag[0]);
  39.                 $alltags{$tagname} = [] unless exists $alltags{$tagname};
  40.                 if ($tag[1]) {
  41.                         $tag[1] =~ tr!/\\<>|:;$*"'!!d;
  42.                         push @{$alltags{$tagname}}, $tag[1];
  43.                 } else {
  44.                         push @{$alltags{$tagname}}, 'null';
  45.                 }
  46.         }
  47.         close(OUTPUT) || die "can\'t close metaflac: $!";
  48.         return %alltags;
  49. }
  50.  
  51. sub mvfile {
  52.         my $fn = shift;
  53.         my $newname = shift;
  54.         if ($fn ne $newname) {
  55.                 say "$fn != $newname";
  56.         }
  57. }