Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/env perl
- use warnings;
- use strict;
- use File::Find;
- use File::Path;
- use File::Copy;
- use Cwd qw( abs_path );
- use Parallel::ForkManager;
- my $path = abs_path(shift);
- my $target = abs_path(shift);
- my @ttafile_list;
- my $pm = new Parallel::ForkManager(8);
- print "Handling '$path'\n";
- find({wanted => \&process_entry,
- no_chdir => 1,
- preprocess => sub { return sort @_ ; } },$path);
- sub cuefile_tagger {
- my $cue_file = shift;
- open( my $cue_fh , "<" , $cue_file ) or die ( "Cannot open CUE file: $cue_file $!" );
- my $part = 0;
- my $meta = {};
- my %static_meta;
- my @list;
- if($cue_file =~ /([0-9xX]{4})\.([0-9xX]{2})\.([0-9xX]{2})/)
- {
- print "Found date in filename: $1-$2-$3";
- $static_meta{"date"} = $1;
- }
- while ( <$cue_fh> )
- {
- if( /REM GENRE (.*)/) {
- $static_meta{"genre"} = $1;
- print "Genre: ".$static_meta{"genre"}."\n";
- } elsif (/REM DATE ([0-9x]{4})/) {
- $static_meta{"date"} = $1;
- print "Year: ".$static_meta{"date"}."\n";
- } elsif (/REM DISCID (.*)/) {
- $static_meta{"discid"} = $1;
- print "DiscID: ".$static_meta{"discid"}."\n";
- } elsif (/REM COMMENT "(.*)"/) {
- $static_meta{"comment"} = $1;
- print "Comment: ".$static_meta{"comment"}."\n";
- } elsif (/PERFORMER "(.*)"/) {
- if($part == 0) {
- $static_meta{"albumartist"} = $1;
- print "AlbumArtist: ".$static_meta{"albumartist"}."\n";
- }
- else {
- $meta->{"artist"} = $1;
- print "Artist: ".$meta->{"artist"}."\n";
- }
- } elsif (/TITLE "(.*)"/) {
- if($part == 0) {
- $static_meta{"album"} = $1;
- print "Album: ".$static_meta{"album"}."\n";
- }
- else {
- $meta->{"title"} = $1;
- print "Title: ".$meta->{"title"}."\n";
- }
- } elsif (/FILE "(.*)" (.*)/) {
- print "File ($2): $1\n";
- } elsif (/TRACK (.*) (.*)/) {
- print "Track $1 ($2)\n";
- if($part == 0) {
- while(my($k, $v) = each %static_meta) {
- $meta->{$k} = $v;
- }
- } else {
- push @list, $meta;
- $meta = {};
- while(my($k, $v) = each %static_meta) {
- $meta->{$k} = $v;
- }
- }
- $meta->{"tracknumber"} = $1;
- $meta->{"artist"} = $meta->{"albumartist"};
- $part ++;
- } elsif (/INDEX ([0-9]*) ([0-9]{2}):([0-9]{2}):([0-9]{2})/) {
- print "Index $1 $2-$3-$4\n";
- }
- }
- push @list, $meta;
- close ($cue_fh);
- print " ~~~ ... ~~~ \n";
- foreach my $info (@list)
- {
- my $flac_file = shift;
- print "\n";
- print "FLAC: $flac_file\n\n";
- open( my $fh, ">", "/tmp/$$-taginfo.tmp") or die("Could not create tags file");
- while(my($k, $v) = each $info) {
- print $fh "$k=$v\n";
- print "$k=$v\n";
- }
- close $fh;
- system("metaflac", "--remove-all-tags", "--import-tags-from=/tmp/$$-taginfo.tmp","$flac_file");
- unlink "/tmp/$$-taginfo.tmp";
- }
- print " ~~~ ... ~~~ \n";
- }
- sub process_entry {
- if(-f)
- {
- if(/.*\.tta/)
- {
- push @ttafile_list, $File::Find::name;
- }
- }
- }
- foreach my $tta (@ttafile_list) {
- $pm->start and next;
- my $cue = $tta;
- $cue =~ s/\.tta/.cue/;
- -e $tta && print "TTA File: $tta\n";
- -e $cue && print "CUE File: $cue\n";
- mkpath "$target/$cue.d";
- # prepare the splitting
- my @shnsplit = ("shnsplit", "-d", "$target/$cue.d", "-f", "$cue", "-o", "flac flac -V --best -o %f -", "$tta", "-t", "%n %p - %t");
- print "Splitting files: ";
- print "EXEC: '".join("' '",@shnsplit)."'\n";
- system(@shnsplit) == 0 || die "ShnSplit failed!\n";
- print "Tagging files:\n";
- my @flacfiles;
- opendir my $dh, "$target/$cue.d/";
- while(readdir $dh)
- {
- if(/.*\.flac$/)
- {
- print "flac: $_\n";
- push @flacfiles, "$target/$cue.d/$_";
- }
- }
- closedir $dh;
- cuefile_tagger($cue,@flacfiles);
- $pm->finish;
- }
- $pm->wait_all_children;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement