Advertisement
sakuya-inu-izayoi

convert-tta-collection.pl

Oct 4th, 2015
390
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 3.90 KB | None | 0 0
  1. #!/bin/env perl
  2.  
  3. use warnings;
  4. use strict;
  5.  
  6. use File::Find;
  7. use File::Path;
  8. use File::Copy;
  9. use Cwd qw( abs_path );
  10.  
  11. use Parallel::ForkManager;
  12.  
  13. my $path = abs_path(shift);
  14. my $target = abs_path(shift);
  15.  
  16. my @ttafile_list;
  17.  
  18. my $pm = new Parallel::ForkManager(8);
  19.  
  20. print "Handling '$path'\n";
  21.  
  22. find({wanted => \&process_entry,
  23.       no_chdir => 1,
  24.       preprocess => sub { return sort @_ ; } },$path);
  25.  
  26. sub cuefile_tagger {
  27.   my $cue_file = shift;
  28.  
  29.  
  30. open( my $cue_fh , "<" , $cue_file ) or die ( "Cannot open CUE file: $cue_file $!" );
  31.  
  32. my $part = 0;
  33. my $meta = {};
  34. my %static_meta;
  35. my @list;
  36.  
  37. if($cue_file =~ /([0-9xX]{4})\.([0-9xX]{2})\.([0-9xX]{2})/)
  38. {
  39.   print "Found date in filename: $1-$2-$3";
  40.   $static_meta{"date"} = $1;
  41. }
  42.  
  43. while ( <$cue_fh> )
  44. {
  45.   if(      /REM GENRE (.*)/) {
  46.     $static_meta{"genre"}   = $1;
  47.     print "Genre: ".$static_meta{"genre"}."\n";
  48.   } elsif (/REM DATE ([0-9x]{4})/) {
  49.     $static_meta{"date"}    = $1;
  50.     print "Year: ".$static_meta{"date"}."\n";
  51.   } elsif (/REM DISCID (.*)/) {
  52.     $static_meta{"discid"}  = $1;
  53.     print "DiscID: ".$static_meta{"discid"}."\n";
  54.   } elsif (/REM COMMENT "(.*)"/) {
  55.     $static_meta{"comment"} = $1;
  56.     print "Comment: ".$static_meta{"comment"}."\n";
  57.   } elsif (/PERFORMER "(.*)"/) {
  58.     if($part == 0) {
  59.       $static_meta{"albumartist"} = $1;
  60.       print "AlbumArtist: ".$static_meta{"albumartist"}."\n";
  61.     }
  62.     else {
  63.       $meta->{"artist"} = $1;
  64.       print "Artist: ".$meta->{"artist"}."\n";
  65.     }
  66.   } elsif (/TITLE "(.*)"/) {
  67.     if($part == 0) {
  68.       $static_meta{"album"} = $1;
  69.       print "Album: ".$static_meta{"album"}."\n";
  70.     }
  71.     else {
  72.       $meta->{"title"} = $1;
  73.       print "Title: ".$meta->{"title"}."\n";
  74.     }
  75.   } elsif (/FILE "(.*)" (.*)/) {
  76.     print "File ($2): $1\n";
  77.   } elsif (/TRACK (.*) (.*)/) {
  78.     print "Track $1 ($2)\n";
  79.     if($part == 0) {
  80.       while(my($k, $v) = each %static_meta) {
  81.         $meta->{$k} = $v;
  82.       }
  83.     } else {
  84.       push @list, $meta;
  85.       $meta = {};
  86.       while(my($k, $v) = each %static_meta) {
  87.         $meta->{$k} = $v;
  88.       }
  89.     }
  90.     $meta->{"tracknumber"} = $1;
  91.     $meta->{"artist"} = $meta->{"albumartist"};
  92.     $part ++;
  93.   } elsif (/INDEX ([0-9]*) ([0-9]{2}):([0-9]{2}):([0-9]{2})/) {
  94.     print "Index $1 $2-$3-$4\n";
  95.   }
  96. }
  97. push @list, $meta;
  98. close ($cue_fh);
  99.  
  100. print " ~~~ ... ~~~ \n";
  101.  
  102. foreach my $info (@list)
  103. {
  104.   my $flac_file = shift;
  105.   print "\n";
  106.   print "FLAC: $flac_file\n\n";
  107.  
  108.   open( my $fh, ">", "/tmp/$$-taginfo.tmp") or die("Could not create tags file");
  109.   while(my($k, $v) = each $info) {
  110.     print $fh "$k=$v\n";
  111.     print "$k=$v\n";
  112.   }
  113.   close $fh;
  114.    
  115.   system("metaflac", "--remove-all-tags", "--import-tags-from=/tmp/$$-taginfo.tmp","$flac_file");
  116.    
  117.   unlink "/tmp/$$-taginfo.tmp";
  118. }
  119.  
  120. print " ~~~ ... ~~~ \n";
  121. }
  122.  
  123. sub process_entry {
  124.   if(-f)
  125.   {
  126.     if(/.*\.tta/)
  127.     {
  128.       push @ttafile_list, $File::Find::name;
  129.     }
  130.   }
  131. }
  132.  
  133. foreach my $tta (@ttafile_list) {
  134.       $pm->start and next;
  135.  
  136.       my $cue = $tta;
  137.       $cue =~ s/\.tta/.cue/;
  138.      
  139.       -e $tta && print "TTA File: $tta\n";
  140.       -e $cue && print "CUE File: $cue\n";
  141.  
  142.       mkpath "$target/$cue.d";
  143.       # prepare the splitting
  144.       my @shnsplit = ("shnsplit", "-d", "$target/$cue.d", "-f", "$cue", "-o", "flac flac -V --best -o %f -", "$tta", "-t", "%n %p - %t");
  145.  
  146.       print "Splitting files: ";
  147.       print "EXEC: '".join("' '",@shnsplit)."'\n";
  148.       system(@shnsplit) == 0 || die "ShnSplit failed!\n";
  149.      
  150.       print "Tagging files:\n";
  151.  
  152.       my @flacfiles;
  153.  
  154.       opendir my $dh, "$target/$cue.d/";
  155.       while(readdir $dh)
  156.       {
  157.         if(/.*\.flac$/)
  158.         {
  159.           print "flac: $_\n";
  160.           push @flacfiles, "$target/$cue.d/$_";
  161.         }
  162.       }
  163.       closedir $dh;
  164.  
  165.       cuefile_tagger($cue,@flacfiles);
  166.  
  167.       $pm->finish;
  168. }
  169.  
  170. $pm->wait_all_children;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement