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