]> diplodocus.org Git - flac-archive/blob - flac2mp3
(get_decod_args): The third time field is *sectors*, not hundredths of
[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> 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 Pod::Usage;
26
27 sub tformat {
28 my $min = shift;
29 my $sec = shift;
30 my $hun = shift;
31 return "$min:$sec.$hun";
32 }
33
34 sub get_decode_args {
35 my $fn = shift;
36 my @l;
37
38 open(F, '-|', 'metaflac', '--export-cuesheet-to=-', $fn);
39 while (<F>) {
40 /INDEX 01 (\d\d):(\d\d):(\d\d)$/ or next;
41 push(@l, [$1, $2, $3]);
42 }
43
44 my @args;
45 for my $i (0..$#l) {
46 my $arg = ["--skip=" . tformat(@{$l[$i]})];
47 my $next = $l[$i+1];
48 if (defined($next)) {
49 if ($next->[2] == 0) {
50 if ($next->[1] == 0) {
51 push(@$arg, '--until=' . tformat($next->[0] - 1, 59, 74));
52 } else {
53 push(@$arg, '--until=' . tformat($next->[0], $next->[1] - 1,
54 74));
55 }
56 } else {
57 push(@$arg, '--until=' . tformat($next->[0], $next->[1],
58 $next->[2] - 1));
59 }
60 }
61 push(@args, $arg);
62 }
63
64 return @args;
65 }
66
67 # Return the ARTIST, ALBUM, and DATE tags followed by the TITLE tags
68 # in the file FN.
69 sub get_tags {
70 my $fn = shift;
71 my $tag;
72 my $value;
73 my $artist;
74 my $album;
75 my $date;
76 my @titles;
77
78 open(TAGS, '-|', 'metaflac', '--export-vc-to=-', $fn)
79 or die("open(metaflac --export-vc-to=- $fn): $!");
80 while (<TAGS>) {
81 chomp;
82
83 ($tag, $value) = split(/=/, $_, 2);
84
85 if (/^ARTIST=/) {
86 $artist = $value;
87 } elsif (/^ALBUM=/) {
88 $album = $value;
89 } elsif (/^DATE=/) {
90 $date = $value;
91 } elsif (/TITLE=/) {
92 push(@titles, $value);
93 }
94 }
95 close(TAGS) or die("close(metaflac --export-vc-to=- $fn): $?");
96
97 return ($artist, $album, $date, @titles);
98 }
99
100 sub flac2mp3 {
101 my $fn = shift;
102 my $title = shift;
103 my $artist = shift;
104 my $album = shift;
105 my $date = shift;
106 my $track = shift;
107 my $skip_arg = shift;
108 my $until_arg = shift;
109 my $outfile;
110
111 # We'll be putting these in single quotes, so we need to escape
112 # any single quotes in the filename by closing the quote ('),
113 # putting an escaped quote (\'), and then reopening the quote (').
114 for ($fn, $title, $artist, $album, $date) {
115 s/'/'\\''/;
116 }
117
118 $outfile = sprintf("$artist ($album) \%02s $title.mp3", $track);
119 $outfile =~ s/\//_/g;
120
121 $until_arg ||= '';
122 # XXX
123 system("flac -cd $skip_arg $until_arg '$fn' | lame --preset standard --tt '$title' --ta '$artist' --tl '$album' --ty '$date' --tn $track - '$outfile'");
124 }
125
126 MAIN: {
127 my $fn = shift or pod2usage();
128 my @args = get_decode_args($fn);
129 my ($artist, $album, $date, @titles) = get_tags($fn);
130
131 for my $i (0..$#titles) {
132 flac2mp3($fn, $titles[$i], $artist, $album, $date, $i + 1,
133 @{$args[$i]});
134 }
135 }
136
137 \f
138 __END__
139
140 =head1 AUTHORS
141
142 Written by Eric Gillespie <epg@pretzelnet.org>.
143
144 =cut
145
146 # Local variables:
147 # cperl-indent-level: 4
148 # perl-indent-level: 4
149 # indent-tabs-mode: nil
150 # End:
151
152 # vi: set tabstop=4 expandtab: