Thursday, January 12, 2012

Script to process single image/cue file (cuesplit.pl)


Script to process single image/cue file. Currently supports WAV, FLAC and APE. Known issue where extended characters in TITLE cause a problem (i.e. Mañana - had to manually replace ñ with n in the cue file).

<<<--- cuesplit.pl --->>>

#! /usr/bin/perl
use strict;
use File::Path;
use File::Glob qw(:globally :nocase);

sub Convert_Format {
    my $Image_File = $_[0];
    my $EXT = ($Image_File =~ m/([^.]+)$/)[0];
    # Try FLAC format
    $Image_File =~ s/$EXT/flac/;
    if (-e $Image_File) {
    return $Image_File;
    }
    # Try APE format
    $Image_File =~ s/flac/ape/;
    if (-e $Image_File) {
    return $Image_File;
    }
    # Try WAV format
    $Image_File =~ s/ape/wav/;
    if (-e $Image_File) {
    return $Image_File;
    }
    return $_[0];
}

my $DestPath;
my $Artist;
my $Album;
my $Line;
my $CUE;
my @CUES=<*.cue>;
foreach $CUE (@CUES) {
    open FILE, "<", $CUE or die $1;
    my @Lines = <FILE>;
    close FILE;
    foreach $Line (@Lines) {
        chomp $Line;
#           print $Line, "\n";
        if ($Line =~ /^PERFORMER/) {
            chomp $Line;
            $Line =~ s/^PERFORMER\s+\"//;
            $Line =~ s/(.*)\".*$/$1/;
            $Artist = $Line;
            $Artist =~ tr/\:\?/\-/s;
#               print $Artist, "\n";
        }
        if ($Line =~ /^TITLE/) {
            chomp $Line;
            $Line =~ s/^TITLE\s+\"//;
            $Line =~ s/(.*)\".*$/$1/;
            $Album = $Line;
            $Album =~ tr/\:\?/\-/s;
#               print $Album, "\n";
        }
    }
    $DestPath = $Artist."/".$Album;
    mkpath $DestPath;
    foreach $Line (@Lines) {
        if ($Line =~ /^FILE/) {
            chomp $Line;
            $Line =~ s/^FILE\s+\"//;
            $Line =~ s/(.*)\".*$/$1/;
            $Line = Convert_Format $Line;
            unless (-e $Line) {
                print "Cue needs: ", $Line, "\n";
                last;
            }
            print $Line, "\n";
            `shntool split "$Line" -f "$CUE" -m '?-:-' -t '%n. %t' -d "$DestPath" -o "flac ext=flac flac -s -o %f -"`
        }
    }
}

No comments:

Post a Comment