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