]> diplodocus.org Git - flac-archive/blob - flac2mp3
(run_or_die): Print command if verbose mode.
[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 file I<file> to MP3 files. I<file> is
16 the kind of FLAC file B<fa-flacd> generates. That is, it contains a
17 cue sheet, one TITLE tag per track listed therein, and ARTIST, ALBUM,
18 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 $tag;
96 my $value;
97 my $artist;
98 my $album;
99 my $date;
100 my @titles;
101
102 open(TAGS, '-|', 'metaflac', '--export-vc-to=-', $fn)
103 or die("open(metaflac --export-vc-to=- $fn): $!");
104 while (<TAGS>) {
105 chomp;
106
107 ($tag, $value) = split(/=/, $_, 2);
108
109 if (/^ARTIST=/) {
110 $artist = $value;
111 } elsif (/^ALBUM=/) {
112 $album = $value;
113 } elsif (/^DATE=/) {
114 $date = $value;
115 } elsif (/TITLE/) {
116 push(@titles, $value);
117 }
118 }
119 close(TAGS) or die("close(metaflac --export-vc-to=- $fn): $?");
120
121 return ($artist, $album, $date, @titles);
122 }
123
124 sub flac2mp3 {
125 my $fn = shift;
126 my $title = shift;
127 my $artist = shift;
128 my $album = shift;
129 my $date = shift;
130 my $track = shift;
131 my $skip_arg = shift;
132 my $until_arg = shift;
133 my @tmp;
134 my $outfile;
135
136 if ($quiet) {
137 $flac_options = '--silent';
138 } else {
139 $flac_options = '';
140 }
141
142 if ($lame_options) {
143 push(@tmp, $lame_options);
144 } else {
145 push(@tmp, '--preset standard');
146 }
147 $quiet and push(@tmp, '--quiet');
148 $verbose and push(@tmp, '--verbose');
149 $lame_options = join(' ', @tmp);
150
151 # We'll be putting these in single quotes, so we need to escape
152 # any single quotes in the filename by closing the quote ('),
153 # putting an escaped quote (\'), and then reopening the quote (').
154 for ($fn, $title, $artist, $album, $date) {
155 s/'/'\\''/g;
156 }
157
158 $outfile = sprintf("$artist ($album) \%02s $title.mp3", $track);
159 $outfile =~ s/\//_/g;
160
161 $until_arg ||= '';
162 run_or_die(join(' ', "flac $flac_options -cd $skip_arg $until_arg '$fn'",
163 " | lame $lame_options --tt '$title' --ta '$artist'",
164 " --tl '$album' --ty '$date' --tn $track - '$outfile'"));
165 }
166
167 MAIN: {
168 my $help;
169 GetOptions(
170 'lame-options=s', \$lame_options,
171 'quiet|q' => \$quiet,
172 'verbose|v' => \$verbose,
173 'help|h|?' => \$help,
174 ) or pod2usage();
175 $help and pod2usage(-exitstatus=>0, -verbose=>1);
176
177 my $fn = shift or pod2usage();
178 my @args = get_decode_args($fn);
179 my ($artist, $album, $date, @titles) = get_tags($fn);
180
181 for my $i (0..$#titles) {
182 flac2mp3($fn, $titles[$i], $artist, $album, $date, $i + 1,
183 @{$args[$i]});
184 }
185 }
186
187 \f
188 __END__
189
190 =head1 OPTIONS
191
192 =over 4
193
194 =item B<--lame-options> I<lame-options>
195
196 Pass I<lame-options> to B<lame>. This ends up being passed to the
197 shell, so feel free to take advantage of that. You'll almost
198 certainly have to put I<lame-options> in single quotes.
199
200 =item B<-q> [B<--quiet>]
201
202 Suppress status information. This option is passed along to B<flac>
203 and B<lame>.
204
205 =item B<-v> [B<--verbose>]
206
207 Print diagnostic information. This option is passed along to B<flac>
208 and B<lame>.
209
210 =back
211
212 =head1 AUTHORS
213
214 Written by Eric Gillespie <epg@pretzelnet.org>.
215
216 =cut
217
218 # Local variables:
219 # cperl-indent-level: 4
220 # perl-indent-level: 4
221 # indent-tabs-mode: nil
222 # End:
223
224 # vi: set tabstop=4 expandtab: