source: flacrip/trunk/flactrack @ 32

Last change on this file since 32 was 30, checked in by peter, 10 years ago

Added a tags/notags switch to flactrack to control whether to tag the MP3s.

  • Property svn:executable set to *
File size: 4.6 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;
[15]17use Text::Unidecode;
[1]18
[30]19# default to using tags
20my $TAGS = 1;
21
[1]22GetOptions(
23    'D=s' => \my %TRACKS,
24    't=s' => \my $TYPE,
25    'x=s' => \my $SOX_FILTER,
[4]26    'all|a' => \my $ALL,
27    'dir|d=s' => \my $DIRECTORY,
[15]28    'ascii-tags' => \my $ASCII_TAGS,
[30]29    'tags!' => \$TAGS,
[1]30);
31
32my $FLAC_FILE = shift or die "Need a flac file to decode";
33
34# default to mp3
35$TYPE ||= 'mp3';
36
[4]37# default the directory to be named like the flac file
38($DIRECTORY ||= $FLAC_FILE) =~ s/\.flac$//;
39
40my $flac = Audio::FLAC::Header->new($FLAC_FILE) or die "Can't read FLAC header from $FLAC_FILE\n";
41
[1]42# for getting track metadata from MusicBrainz
[3]43my $info;
[30]44if ($ALL || ($TYPE eq 'mp3' && $TAGS)) {
[13]45    (my $properties_file = $FLAC_FILE) =~ s/\.flac$/.properties/;
46    if (-e $properties_file) {
47        require Config::Properties;
[1]48
[15]49        # the properties are in UTF-8; mark them as such so unidecode works correctly later
50        my $properties = Config::Properties->new(file => $properties_file, encoding => 'utf8');
[13]51        $info = $properties->getProperties;
52    } else {
53
[17]54        my $discid = $flac->tags('MUSICBRAINZ_DISCID') or warn "No MUSICBRAINZ_DISCID tag in $FLAC_FILE\n" if $flac;
55        #TODO: calculate TOC and DISCID from cuesheet if there is no MUSICBRAINZ_DISCID tag present
[13]56
57        $info = get_musicbrainz_info($discid);
58    }
[12]59    exit unless $info;
[10]60
61    # if we have metadata, change the directory name to ARTIST.DATE.ALBUM, so it will sort nicely
62    my $base_dir = (splitpath($DIRECTORY))[1];
63    my $album_dir = join('.', map { to_filename($_) } @{$info}{qw{ALBUMARTISTSORT ORIGINALDATE ALBUM}});
64    # need to append "disc#" if this is a multidisc album
65    if ($info->{DISCTOTAL} > 1) {
66        $album_dir .= '.disc_' . $info->{DISCNUMBER};
67    }
68    $DIRECTORY = catfile($base_dir, $album_dir);
69    #die $DIRECTORY;
[1]70}
71
[4]72if ($ALL) {
73    die "Use of --all requires a --directory\n" unless $DIRECTORY;
[5]74    die "No track info found on MusicBrainz for $FLAC_FILE\n" unless $info;
[4]75    use YAML;
76    my $cuesheet = $flac->cuesheet;
77    my $count = scalar grep { /TRACK \d\d/ } @{ $flac->cuesheet };
78    print "Found $count tracks\n";
79    #TODO: default to just 01, 02, etc. if there is no $info
80    %TRACKS = map {
[13]81        $_ => catfile($DIRECTORY, sprintf('%02d.%s', $_, to_filename($info->{sprintf 'TRACK%02d.TITLE', $_})))
[4]82    } (1 .. $count);
[5]83    #print Dump(\%TRACKS);
84    for my $tracknum (sort { $a <=> $b } keys %TRACKS) {
85        printf "%2d: %s\n", $tracknum, $TRACKS{$tracknum};
86    }
[4]87    mkpath($DIRECTORY);
88}
89
[5]90#TODO: all the option of sorting by tracknum or not
91#while (my ($tracknum, $title) = each %TRACKS) {
92for my $tracknum (sort { $a <=> $b } keys %TRACKS) {
[1]93    if ($tracknum !~ /^\d+$/) {
94        warn "Don't know what to do with track number '$tracknum'";
95        next;
96    }
97    my $start = $tracknum . '.1';
98    my $end = $tracknum + 1 . '.1';
99    my $cmd = qq{flac -d --cue $start-$end $FLAC_FILE -o - };
100
101    if ($SOX_FILTER) {
102        $cmd .= qq{| sox -t wav - -t wav - $SOX_FILTER };
103    }
104
[5]105    my $title = quotemeta($TRACKS{$tracknum});
[1]106    if ($TYPE eq 'mp3') {
107        # bitrate of 192
108        $cmd .= qq{| lame -b 192};
109        # if there is track info, add it as ID3 tags
[3]110        if ($info) {
[13]111            my $track_key = sprintf 'TRACK%02d', $tracknum;
[15]112            $cmd .= sprintf q{ --tt %s --ta %s --tl %s --ty %d --tn %d},
113                quote($ASCII_TAGS ? unidecode($info->{"$track_key.TITLE"}) : $info->{"$track_key.TITLE"}),
114                quote($ASCII_TAGS ? unidecode($info->{"$track_key.ARTIST"}) : $info->{"$track_key.ARTIST"}),
115                quote($ASCII_TAGS ? unidecode($info->{ALBUM}) : $info->{ALBUM}),
116                ($info->{ORIGINALDATE} =~ /^(\d\d\d\d)/)[0],
[1]117                $tracknum;
118        }
119        $cmd .= qq{ - $title.mp3};
120    } elsif ($TYPE eq 'wav') {
121        $cmd .= qq{> $title.wav};
122    } else {
123        die "Unknown type: $TYPE\n";
124    }
125    #die $cmd;
126    system $cmd;
[5]127    die "\nFLAC decoding canceled\n" if ($? & 127);
[1]128
129    print "\n" if $SOX_FILTER;
130}
131
132sub quote {
133    my ($string) = @_;
134    $string =~ s/"/\\"/g;
135    return qq{"$string"};
136}
[4]137
138sub to_filename {
139    my @strings = @_;
140    return map {
[10]141        s/&/ and /g;
[15]142        unidecode($_);
[4]143        s/[^a-z0-9-_ ]+//gi;
144        s/ +/_/g;
145        lc;
146    } @strings;
147}
Note: See TracBrowser for help on using the repository browser.