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