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