[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 | |
---|
[11] | 9 | use FindBin; |
---|
| 10 | use lib "$FindBin::RealBin/lib"; |
---|
| 11 | |
---|
[4] | 12 | use Getopt::Long qw{:config no_ignore_case}; |
---|
[10] | 13 | use File::Spec::Functions qw{catfile splitpath}; |
---|
[4] | 14 | use File::Path; |
---|
| 15 | use Audio::FLAC::Header; |
---|
[3] | 16 | use MusicBrainz; |
---|
[15] | 17 | use Text::Unidecode; |
---|
[1] | 18 | |
---|
[30] | 19 | # default to using tags |
---|
| 20 | my $TAGS = 1; |
---|
| 21 | |
---|
[1] | 22 | GetOptions( |
---|
| 23 | 'D=s' => \my %TRACKS, |
---|
| 24 | 't=s' => \my $TYPE, |
---|
| 25 | 'x=s' => \my $SOX_FILTER, |
---|
[4] | 26 | 'all|a' => \my $ALL, |
---|
| 27 | 'dir|d=s' => \my $DIRECTORY, |
---|
[15] | 28 | 'ascii-tags' => \my $ASCII_TAGS, |
---|
[30] | 29 | 'tags!' => \$TAGS, |
---|
[47] | 30 | 'force' => \my $FORCE, |
---|
[1] | 31 | ); |
---|
| 32 | |
---|
| 33 | my $FLAC_FILE = shift or die "Need a flac file to decode"; |
---|
| 34 | |
---|
| 35 | # default to mp3 |
---|
| 36 | $TYPE ||= 'mp3'; |
---|
| 37 | |
---|
[4] | 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 | |
---|
[1] | 43 | # for getting track metadata from MusicBrainz |
---|
[3] | 44 | my $info; |
---|
[30] | 45 | if ($ALL || ($TYPE eq 'mp3' && $TAGS)) { |
---|
[13] | 46 | (my $properties_file = $FLAC_FILE) =~ s/\.flac$/.properties/; |
---|
| 47 | if (-e $properties_file) { |
---|
| 48 | require Config::Properties; |
---|
[1] | 49 | |
---|
[15] | 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'); |
---|
[13] | 52 | $info = $properties->getProperties; |
---|
| 53 | } else { |
---|
| 54 | |
---|
[17] | 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 |
---|
[13] | 57 | |
---|
| 58 | $info = get_musicbrainz_info($discid); |
---|
| 59 | } |
---|
[12] | 60 | exit unless $info; |
---|
[10] | 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); |
---|
[47] | 70 | if (-e $DIRECTORY and not $FORCE) { |
---|
| 71 | die "$DIRECTORY already exists, skipping. (Use --force to overwrite)\n"; |
---|
| 72 | } |
---|
[10] | 73 | #die $DIRECTORY; |
---|
[1] | 74 | } |
---|
| 75 | |
---|
[4] | 76 | if ($ALL) { |
---|
| 77 | die "Use of --all requires a --directory\n" unless $DIRECTORY; |
---|
[5] | 78 | die "No track info found on MusicBrainz for $FLAC_FILE\n" unless $info; |
---|
[4] | 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 { |
---|
[13] | 85 | $_ => catfile($DIRECTORY, sprintf('%02d.%s', $_, to_filename($info->{sprintf 'TRACK%02d.TITLE', $_}))) |
---|
[4] | 86 | } (1 .. $count); |
---|
[5] | 87 | #print Dump(\%TRACKS); |
---|
| 88 | for my $tracknum (sort { $a <=> $b } keys %TRACKS) { |
---|
| 89 | printf "%2d: %s\n", $tracknum, $TRACKS{$tracknum}; |
---|
| 90 | } |
---|
[4] | 91 | mkpath($DIRECTORY); |
---|
| 92 | } |
---|
| 93 | |
---|
[5] | 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) { |
---|
[1] | 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 | |
---|
[5] | 109 | my $title = quotemeta($TRACKS{$tracknum}); |
---|
[1] | 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 |
---|
[3] | 114 | if ($info) { |
---|
[13] | 115 | my $track_key = sprintf 'TRACK%02d', $tracknum; |
---|
[15] | 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], |
---|
[1] | 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; |
---|
[5] | 131 | die "\nFLAC decoding canceled\n" if ($? & 127); |
---|
[1] | 132 | |
---|
| 133 | print "\n" if $SOX_FILTER; |
---|
| 134 | } |
---|
| 135 | |
---|
| 136 | sub quote { |
---|
| 137 | my ($string) = @_; |
---|
| 138 | $string =~ s/"/\\"/g; |
---|
| 139 | return qq{"$string"}; |
---|
| 140 | } |
---|
[4] | 141 | |
---|
| 142 | sub to_filename { |
---|
| 143 | my @strings = @_; |
---|
| 144 | return map { |
---|
[10] | 145 | s/&/ and /g; |
---|
[15] | 146 | unidecode($_); |
---|
[4] | 147 | s/[^a-z0-9-_ ]+//gi; |
---|
| 148 | s/ +/_/g; |
---|
| 149 | lc; |
---|
| 150 | } @strings; |
---|
| 151 | } |
---|