]> diplodocus.org Git - flac-archive/blob - flac2mp3
Don't stupidly take only one argument; take multiple files.
[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 return @args;
89 }
90
91 # Return the ARTIST, ALBUM, and DATE tags followed by the TITLE tags
92 # in the file FN.
93 sub get_tags {
94 my $fn = shift;
95 my $artists = shift;
96 my $titles = shift;
97 my $tag;
98 my $value;
99 my $artist;
100 my $album;
101 my $date;
102 my $track;
103
104 open(TAGS, '-|', 'metaflac', '--export-vc-to=-', $fn)
105 or die("open(metaflac --export-vc-to=- $fn): $!");
106 while (<TAGS>) {
107 chomp;
108
109 ($tag, $value) = split(/=/, $_, 2);
110
111 if (/^ARTIST=/i) {
112 $artist = $value;
113 } elsif (/^ALBUM=/i) {
114 $album = $value;
115 } elsif (/^DATE=/i) {
116 $date = $value;
117 } elsif (/^ARTIST\[/i) {
118 push(@$artists, $value);
119 } elsif (/^TRACKNUMBER=/i) {
120 $track = $value;
121
122 # Intentionally don't match the = on this one, to support the
123 # TITLE[1] .. TITLE[n] tag style.
124 } elsif (/^TITLE/i) {
125 push(@$titles, $value);
126 }
127 }
128 close(TAGS) or die("close(metaflac --export-vc-to=- $fn): $?");
129
130 return ($artist, $album, $date, $track);
131 }
132
133 sub arg {
134 my $arg = shift;
135 my $var = shift;
136
137 if (defined($$var)) {
138 $$var = "$arg '$$var'";
139 } else {
140 $$var = ''
141 }
142 }
143
144 sub flac2mp3 {
145 my $fn = shift;
146 my $title = shift;
147 my $artist = shift;
148 my $album = shift;
149 my $date = shift;
150 my $track = shift;
151 my $skip_arg = shift;
152 my $until_arg = shift;
153 my @tmp;
154 my $outfile;
155
156 if ($quiet) {
157 $flac_options = '--silent';
158 } else {
159 $flac_options = '';
160 }
161
162 if ($lame_options) {
163 push(@tmp, $lame_options);
164 } else {
165 push(@tmp, '--preset standard');
166 }
167 $quiet and push(@tmp, '--quiet');
168 $verbose and push(@tmp, '--verbose');
169 $lame_options = join(' ', @tmp);
170
171 # We'll be putting these in single quotes, so we need to escape
172 # any single quotes in the filename by closing the quote ('),
173 # putting an escaped quote (\'), and then reopening the quote (').
174 for ($fn, $title, $artist, $album, $date) {
175 defined and s/'/'\\''/g;
176 }
177
178 $outfile = sprintf("$artist ($album) \%02s $title.mp3", $track);
179 $outfile =~ s/\//_/g;
180
181 arg('--tt', \$title);
182 arg('--ta', \$artist);
183 arg('--tl', \$album);
184 arg('--ty', \$date);
185 arg('--tn', \$track);
186
187 $until_arg ||= '';
188 run_or_die(join(' ', "flac $flac_options -cd $skip_arg $until_arg '$fn'",
189 " | lame $lame_options $title $artist $album $date $track",
190 " - '$outfile'"));
191 }
192
193 MAIN: {
194 my $help;
195 GetOptions(
196 'lame-options=s', \$lame_options,
197 'quiet|q' => \$quiet,
198 'verbose|v' => \$verbose,
199 'help|h|?' => \$help,
200 ) or pod2usage();
201 $help and pod2usage(-exitstatus=>0, -verbose=>1);
202
203 @ARGV or pod2usage();
204 for my $fn (@ARGV) {
205 my @args = get_decode_args($fn);
206 my (@artists, @titles);
207 my ($artist, $album, $date, $track) = get_tags($fn, \@artists,
208 \@titles);
209
210 # Stupid hack: only a single-track file should have the
211 # TRACKNUMBER tag, so use it if set for the first pass through
212 # the loop. At the end of the loop, we'll set $track for the
213 # next run, so this continues to work for multi-track files.
214 $track ||= 1;
215
216 for my $i (0..$#titles) {
217 flac2mp3($fn, $titles[$i], ($artists[$i] or $artist), $album, $date,
218 $track, (defined($args[$i]) and @{$args[$i]} or ''));
219 $track = $i + 2;
220 }
221 }
222 }
223
224 \f
225 __END__
226
227 =head1 OPTIONS
228
229 =over 4
230
231 =item B<--lame-options> I<lame-options>
232
233 Pass I<lame-options> to B<lame>. This ends up being passed to the
234 shell, so feel free to take advantage of that. You'll almost
235 certainly have to put I<lame-options> in single quotes.
236
237 =item B<-q> [B<--quiet>]
238
239 Suppress status information. This option is passed along to B<flac>
240 and B<lame>.
241
242 =item B<-v> [B<--verbose>]
243
244 Print diagnostic information. This option is passed along to B<flac>
245 and B<lame>.
246
247 =back
248
249 =head1 AUTHORS
250
251 Written by Eric Gillespie <epg@pretzelnet.org>.
252
253 =cut
254
255 # Local variables:
256 # cperl-indent-level: 4
257 # perl-indent-level: 4
258 # indent-tabs-mode: nil
259 # End:
260
261 # vi: set tabstop=4 expandtab: