Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

mp3uncue

by Aristotle (Chancellor)
on Nov 13, 2002 at 22:34 UTC ( #212713=sourcecode: print w/ replies, xml ) Need Help??

Category: Audio Related Programs
Author/Contact Info /msg Aristotle
Description: Some people archive their CD audio albums as a single MP3 with a CUE sheet supplied. This script uses mpgedit to slice such a monolithic MP3 album into individual MP3 tracks (without reencoding any audio data) and id3tool to tag them with the information from the CUE sheet.
#!/usr/bin/perl -w
use strict;
# mp3uncue: cut a multiple track MP3 into individual tracks using a .C
+UE sheet
#           requires:
#           - mpgedit <http://mpgedit.org>
#           - id3tool <http://freshmeat.net/projects/id3tool/>
# 
# Version 0.1: Nov 12, 2002, A. Pagaltzis
#
# (c)2002 A. Pagaltzis, licensed under the GPL
# see http://www.gnu.org/copyleft/gpl.html

use Env qw(@PATH);
use File::Path;
use File::Basename;
use File::Spec::Functions;
use Getopt::Long;

use constant SEPARATOR => "TRACK";
use constant NAME_FMT => "%02d %s - %s.mp3";
use constant NAME_FIELDS => qw(TRACK PERFORMER TITLE);

my %keyword = (
    PERFORMER => sub { $_[0] },
    TITLE     => sub { $_[0] },
    TRACK     => sub { $_[0] },
    INDEX     => sub {
        my ($m, $s, $f) = split /:/, $_[1], 3;
        "$m:" . sprintf("%06.3f", $s + $f / 75);
    },
    FILE      => sub { $_[0] },
);

my ($infile, $outdir, $album, @exec, $mpgedit, $id3tool);


## parse cmdline options and check existence of tools
(
    GetOptions(
         'infile|i=s', \$infile,
         'outdir|d=s', \$outdir,
    )
    && (@ARGV > 0)
    && (@exec = grep { -f and -x _ } map {
        my $dir = $_;
        opendir my($dh), $dir;
        map catfile($dir, $_), readdir $dh;
    } @PATH)
    && (($mpgedit) = grep basename($_) eq 'mpgedit', @exec)
    && (($id3tool) = grep basename($_) eq 'id3tool', @exec)
) or die << 'USAGE';
usage: mp3uncue [-i infile] [-d outdir] sheet.cue
       --infile -i  defaults to filename from cue sheet
       --outdir -d  defaults to current directory

       You need mpgedit <http://mpgedit.org> and
       id3tool <http://freshmeat.net/projects/id3tool>
       in your PATH to run this utility.
USAGE


## parse cue sheet
my $rx = do { local $" = "|"; qr/(@{[keys %keyword]})\s+(.*)/ };
my @track = ({});
while(<>) {
    chomp;
    (my ($key), $_) = /$rx/ or next;
    push @track, {} if $key eq SEPARATOR;
    $track[-1]->{$key} =
        $keyword{$key}->(/"?((?<!")\S+(?<!")|[^"]+)"?\s*/g);
}
push @track, { INDEX => '' };


## set overall defaults
for(shift @track) {
    $infile ||= $_->{FILE};
    $album  ||= $_->{TITLE};
    $outdir ||= '.';
}

## process
mkpath [$outdir] if not -d $outdir;
for my $i (0 .. $#track-1) {
    for($track[$i]) {
        (my $filename = sprintf NAME_FMT, @$_{NAME_FIELDS()}, ('')x10)
+ =~ tr/ _/_/s;
        my $time = $_->{INDEX} . '-' . $track[$i+1]->{INDEX};
        printf STDERR "%-19s = $filename\n", $time;
        $filename = catfile($outdir, $filename);
        system(
            $mpgedit, '-s',
            -e => $time,
            -f => $infile,
            -o => $filename,
        ) == 0 or die basename($0).": mpgedit failed creating $filenam
+e\n";
        system(
            $id3tool,
            "--set-title="  . ($_->{TITLE} || ""),
            "--set-artist=" . ($_->{PERFORMER} || ""),
            "--set-album="  . ($album || ""),
            "--set-track="  . ($_->{TRACK} || ""),
            $filename,
        );
    }
}

Comment on mp3uncue
Download Code

Back to Code Catacombs

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: sourcecode [id://212713]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (9)
As of 2014-12-25 10:00 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (159 votes), past polls