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