source: flacrip/trunk/flactrack @ 12

Last change on this file since 12 was 12, checked in by peter, 11 years ago
  • flactrack aborts if its call to get_musicbrainz_info() returns undef
  • added a select_release() function to MusicBrainz.pm that has the user interactively select the release; causes get_musicbrainz_info() to return undef if the user decides to cancel
  • removed a bunch of old, commented out code from MusicBrainz.pm
  • Property svn:executable set to *
File size: 3.8 KB
RevLine 
[1]1#!/usr/bin/perl -w
2use strict;
3
4# extract one or more tracks from a FLAC file using its embedded cuesheet
5# save those tracks as wav or mp3 files
6# can also run them through a sox filter
7# TODO: separate sox filter for each track!
8
[11]9use FindBin;
10use lib "$FindBin::RealBin/lib";
11
[4]12use Getopt::Long qw{:config no_ignore_case};
[10]13use File::Spec::Functions qw{catfile splitpath};
[4]14use File::Path;
15use Audio::FLAC::Header;
[3]16use MusicBrainz;
[1]17
18GetOptions(
19    'D=s' => \my %TRACKS,
20    't=s' => \my $TYPE,
21    'x=s' => \my $SOX_FILTER,
[4]22    'all|a' => \my $ALL,
23    'dir|d=s' => \my $DIRECTORY,
[1]24);
25
26my $FLAC_FILE = shift or die "Need a flac file to decode";
27
28# default to mp3
29$TYPE ||= 'mp3';
30
[4]31# default the directory to be named like the flac file
32($DIRECTORY ||= $FLAC_FILE) =~ s/\.flac$//;
33
34my $flac = Audio::FLAC::Header->new($FLAC_FILE) or die "Can't read FLAC header from $FLAC_FILE\n";
35
[1]36# for getting track metadata from MusicBrainz
[3]37my $info;
[4]38if ($ALL || $TYPE eq 'mp3') {
[1]39    my $discid = $flac->tags('MBZ_DISCID') or warn "No MBZ_DISCID tag in $FLAC_FILE\n" if $flac;
40    #TODO: calculate TOC and DISCID from cuesheet if there is no MBZ_DISCID tag present
41
[3]42    $info = get_musicbrainz_info($discid);
[12]43    exit unless $info;
[10]44
45    # if we have metadata, change the directory name to ARTIST.DATE.ALBUM, so it will sort nicely
46    my $base_dir = (splitpath($DIRECTORY))[1];
47    my $album_dir = join('.', map { to_filename($_) } @{$info}{qw{ALBUMARTISTSORT ORIGINALDATE ALBUM}});
48    # need to append "disc#" if this is a multidisc album
49    if ($info->{DISCTOTAL} > 1) {
50        $album_dir .= '.disc_' . $info->{DISCNUMBER};
51    }
52    $DIRECTORY = catfile($base_dir, $album_dir);
53    #die $DIRECTORY;
[1]54}
55
[4]56if ($ALL) {
57    die "Use of --all requires a --directory\n" unless $DIRECTORY;
[5]58    die "No track info found on MusicBrainz for $FLAC_FILE\n" unless $info;
[4]59    use YAML;
60    my $cuesheet = $flac->cuesheet;
61    my $count = scalar grep { /TRACK \d\d/ } @{ $flac->cuesheet };
62    print "Found $count tracks\n";
63    #TODO: default to just 01, 02, etc. if there is no $info
64    %TRACKS = map {
65        $_ => catfile($DIRECTORY, sprintf('%02d.%s', $_, to_filename($info->{TRACKS}[$_]{TITLE})))
66    } (1 .. $count);
[5]67    #print Dump(\%TRACKS);
68    for my $tracknum (sort { $a <=> $b } keys %TRACKS) {
69        printf "%2d: %s\n", $tracknum, $TRACKS{$tracknum};
70    }
[4]71    mkpath($DIRECTORY);
72}
73
[5]74#TODO: all the option of sorting by tracknum or not
75#while (my ($tracknum, $title) = each %TRACKS) {
76for my $tracknum (sort { $a <=> $b } keys %TRACKS) {
[1]77    if ($tracknum !~ /^\d+$/) {
78        warn "Don't know what to do with track number '$tracknum'";
79        next;
80    }
81    my $start = $tracknum . '.1';
82    my $end = $tracknum + 1 . '.1';
83    my $cmd = qq{flac -d --cue $start-$end $FLAC_FILE -o - };
84
85    if ($SOX_FILTER) {
86        $cmd .= qq{| sox -t wav - -t wav - $SOX_FILTER };
87    }
88
[5]89    my $title = quotemeta($TRACKS{$tracknum});
[1]90    if ($TYPE eq 'mp3') {
91        # bitrate of 192
92        $cmd .= qq{| lame -b 192};
93        # if there is track info, add it as ID3 tags
[3]94        if ($info) {
95            my $track = $info->{TRACKS}[$tracknum];
[1]96            $cmd .= sprintf q{ --tt %s --ta %s --tl %s --tn %d},
[3]97                quote($track->{TITLE}),
98                quote($track->{ARTIST}),
99                quote($info->{ALBUM}),
[1]100                $tracknum;
101        }
102        $cmd .= qq{ - $title.mp3};
103    } elsif ($TYPE eq 'wav') {
104        $cmd .= qq{> $title.wav};
105    } else {
106        die "Unknown type: $TYPE\n";
107    }
108    #die $cmd;
109    system $cmd;
[5]110    die "\nFLAC decoding canceled\n" if ($? & 127);
[1]111
112    print "\n" if $SOX_FILTER;
113}
114
115sub quote {
116    my ($string) = @_;
117    $string =~ s/"/\\"/g;
118    return qq{"$string"};
119}
[4]120
121sub to_filename {
122    my @strings = @_;
123    #TODO: deal with non-ascii characters (unidecode) e.g. ü --> ue
124    return map {
[10]125        s/&/ and /g;
[4]126        s/[^a-z0-9-_ ]+//gi;
127        s/ +/_/g;
128        lc;
129    } @strings;
130}
Note: See TracBrowser for help on using the repository browser.