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