]> diplodocus.org Git - flac-archive/blob - flac2mp3
Store TRACKNUMBER tag in multi-file mode (oops).
[flac-archive] / flac2mp3
1 #! /usr/bin/env perl
2
3 # $Id$
4
5 =head1 NAME
6
7 B<flac2mp3> - transcode FLAC file to MP3 files
8
9 =head1 SYNOPSIS
10
11 B<flac2mp3> [B<--lame-options> I<lame-options>] [B<-q>] [B<-v>] I<file> [...]
12
13 =head1 DESCRIPTION
14
15 B<flac2mp3> transcodes the FLAC files I<file> to MP3 files. I<file>
16 may be the kind of FLAC file B<fa-flacd> generates. That is, it
17 contains a cue sheet, one TITLE tag per track listed therein, and
18 ARTIST, ALBUM, and DATE tags.
19
20 =cut
21
22 use strict;
23 use warnings;
24
25 use POSIX ':sys_wait_h';
26 use Pod::Usage;
27 use Getopt::Long qw(:config gnu_getopt no_ignore_case);
28
29 my $flac_options;
30 my $lame_options;
31 my $quiet;
32 my $verbose;
33
34 sub run_or_die {
35 my $command = shift;
36 my $status;
37
38 $verbose and print(STDERR "$command\n");
39 $status = system($command);
40
41 if (WIFEXITED($status)) {
42 if (($status = WEXITSTATUS($status)) != 0) {
43 die("$command exited with status $status");
44 }
45 } elsif (WIFSIGNALED($status)) {
46 die("$command killed with signal ", WTERMSIG($status));
47 } elsif (WIFSTOPPED($status)) {
48 die("$command stopped with signal ", WSTOPSIG($status));
49 } else {
50 die("Major horkage on system($command): \$? = $? \$! = $!");
51 }
52 }
53
54 sub tformat {
55 return sprintf('%02d:%02d.%02d', @_);
56 }
57
58 sub get_decode_args {
59 my $fn = shift;
60 my @l;
61
62 open(F, '-|', 'metaflac', '--export-cuesheet-to=-', $fn);
63 while (<F>) {
64 /INDEX 01 (\d\d):(\d\d):(\d\d)$/ or next;
65 push(@l, [$1, $2, $3]);
66 }
67
68 my @args;
69 for my $i (0..$#l) {
70 my $arg = ["--skip=" . tformat(@{$l[$i]})];
71 my $next = $l[$i+1];
72 if (defined($next)) {
73 if ($next->[2] == 0) {
74 if ($next->[1] == 0) {
75 push(@$arg, '--until=' . tformat($next->[0] - 1, 59, 74));
76 } else {
77 push(@$arg, '--until=' . tformat($next->[0], $next->[1] - 1,
78 74));
79 }
80 } else {
81 push(@$arg, '--until=' . tformat($next->[0], $next->[1],
82 $next->[2] - 1));
83 }
84 }
85 push(@args, $arg);
86 }
87
88 # If no cue sheet, stick a dummy in here.
89 if (@args == 0) {
90 @args = ([]);
91 }
92
93 return @args;
94 }
95
96 # Return the ARTIST, ALBUM, and DATE tags followed by the TITLE tags
97 # in the file FN.
98 sub get_tags {
99 my $fn = shift;
100 my $artists = shift;
101 my $titles = shift;
102 my $tag;
103 my $value;
104 my $artist;
105 my $album;
106 my $date;
107 my $discnum;
108 my $track;
109
110 open(TAGS, '-|', 'metaflac', '--export-vc-to=-', $fn)
111 or die("open(metaflac --export-vc-to=- $fn): $!");
112 while (<TAGS>) {
113 chomp;
114
115 ($tag, $value) = split(/=/, $_, 2);
116
117 if (/^ARTIST=/i) {
118 $artist = $value;
119 } elsif (/^ALBUM=/i) {
120 $album = $value;
121 } elsif (/^DATE=/i) {
122 $date = $value;
123 } elsif (/^DISCNUMBER=/i) {
124 $discnum = int($value);
125 } elsif (/^ARTIST\[/i) {
126 push(@$artists, $value);
127 } elsif (/^TRACKNUMBER=/i) {
128 $track = $value;
129
130 # Intentionally don't match the = on this one, to support the
131 # TITLE[1] .. TITLE[n] tag style.
132 } elsif (/^TITLE/i) {
133 push(@$titles, $value);
134 }
135 }
136 close(TAGS) or die("close(metaflac --export-vc-to=- $fn): $?");
137
138 # If no TITLEs, stick a dummy in here.
139 if (@$titles == 0) {
140 push(@$titles, undef);
141 }
142
143 return ($artist, $album, $date, $discnum, $track);
144 }
145
146 sub arg {
147 my $arg = shift;
148 my $var = shift;
149
150 if (defined($$var)) {
151 $$var = "$arg '$$var'";
152 } else {
153 $$var = ''
154 }
155 }
156
157 sub flac2mp3 {
158 my $fn = shift;
159 my $title = (shift or 'unknown');
160 my $artist = (shift or 'unknown');
161 my $album = (shift or 'unknown');
162 my $date = (shift or 'unknown');
163 my $track = int(shift);
164 my $skip_arg = shift;
165 my $until_arg = shift;
166 my @tmp;
167 my $outfile;
168
169 if ($quiet) {
170 $flac_options = '--silent';
171 } else {
172 $flac_options = '';
173 }
174
175 if ($lame_options) {
176 push(@tmp, $lame_options);
177 } else {
178 push(@tmp, '--preset standard');
179 }
180 $quiet and push(@tmp, '--quiet');
181 $verbose and push(@tmp, '--verbose');
182 $lame_options = join(' ', @tmp);
183
184 # We'll be putting these in single quotes, so we need to escape
185 # any single quotes in the filename by closing the quote ('),
186 # putting an escaped quote (\'), and then reopening the quote (').
187 for ($fn, $title, $artist, $album, $date) {
188 defined and s/'/'\\''/g;
189 }
190
191 $outfile = sprintf("$artist ($album) \%02s $title.mp3", $track);
192 $outfile =~ s/\//_/g;
193
194 arg('--tt', \$title);
195 arg('--ta', \$artist);
196 arg('--tl', \$album);
197 arg('--ty', \$date);
198 arg('--tn', \$track);
199
200 $skip_arg ||= '';
201 $until_arg ||= '';
202 run_or_die(join(' ', "flac $flac_options -cd $skip_arg $until_arg '$fn'",
203 " | lame $lame_options $title $artist $album $date $track",
204 " - '$outfile'"));
205 }
206
207 MAIN: {
208 my $help;
209 GetOptions(
210 'lame-options=s', \$lame_options,
211 'quiet|q' => \$quiet,
212 'verbose|v' => \$verbose,
213 'help|h|?' => \$help,
214 ) or pod2usage();
215 $help and pod2usage(-exitstatus=>0, -verbose=>1);
216
217 @ARGV or pod2usage();
218 for my $fn (@ARGV) {
219 my @args = get_decode_args($fn);
220 my (@artists, @titles);
221 my ($artist, $album, $date, $discnum, $track) = get_tags($fn, \@artists,
222 \@titles);
223
224 # lame doesn't seem to support disc number.
225 defined($discnum) and $album .= " (disc $discnum)";
226
227 # Stupid hack: only a single-track file should have the
228 # TRACKNUMBER tag, so use it if set for the first pass through
229 # the loop. At the end of the loop, we'll set $track for the
230 # next run, so this continues to work for multi-track files.
231 $track ||= 1;
232
233 for my $i (0..$#titles) {
234 flac2mp3($fn, $titles[$i], ($artists[$i] or $artist), $album, $date,
235 $track, @{$args[$i]});
236 $track = $i + 2;
237 }
238 }
239 }
240
241 \f
242 __END__
243
244 =head1 OPTIONS
245
246 =over 4
247
248 =item B<--lame-options> I<lame-options>
249
250 Pass I<lame-options> to B<lame>. This ends up being passed to the
251 shell, so feel free to take advantage of that. You'll almost
252 certainly have to put I<lame-options> in single quotes.
253
254 =item B<-q> [B<--quiet>]
255
256 Suppress status information. This option is passed along to B<flac>
257 and B<lame>.
258
259 =item B<-v> [B<--verbose>]
260
261 Print diagnostic information. This option is passed along to B<flac>
262 and B<lame>.
263
264 =back
265
266 =head1 AUTHORS
267
268 Written by Eric Gillespie <epg@pretzelnet.org>.
269
270 =cut
271
272 # Local variables:
273 # cperl-indent-level: 4
274 # perl-indent-level: 4
275 # indent-tabs-mode: nil
276 # End:
277
278 # vi: set tabstop=4 expandtab: