]> diplodocus.org Git - flac-archive/blob - flac2mp3
untested attempt to restore embedded cover art
[flac-archive] / flac2mp3
1 #!/usr/local/bin/perl
2
3 =head1 NAME
4
5 B<flac2mp3> - transcode FLAC file to MP3 files
6
7 =head1 SYNOPSIS
8
9 B<flac2mp3> [B<--lame-options> I<lame-options>] [B<-q>] [B<-v>] I<file> [...]
10
11 =head1 DESCRIPTION
12
13 B<flac2mp3> transcodes the FLAC files I<file> to MP3 files. I<file>
14 may be the kind of FLAC file B<fa-flacd> generates. That is, it
15 contains a cue sheet, one TITLE tag per track listed therein, and
16 ARTIST, ALBUM, and DATE tags.
17
18 Note that lame is retarded, and parses B<LANG> directly itself! So, in order
19 for it to transcode textual tags, you must specify the encoding in LANG, e.g.
20 LANG=en_US.utf-8
21
22 =head1 OPTIONS
23
24 =over 4
25
26 =item B<--lame-options> I<lame-options>
27
28 Pass I<lame-options> to B<lame>. This ends up being passed to the
29 shell, so feel free to take advantage of that. You'll almost
30 certainly have to put I<lame-options> in single quotes.
31
32 =item B<-q> [B<--quiet>]
33
34 Suppress status information. This option is passed along to B<flac>
35 and B<lame>.
36
37 =item B<-v> [B<--verbose>]
38
39 Print diagnostic information. This option is passed along to B<flac>
40 and B<lame>.
41
42 =back
43
44 =head1 AUTHORS
45
46 Written by Eric Gillespie <epg@pretzelnet.org>.
47
48 =cut
49
50 package epg::flac::archive::mp3;
51
52 use v5.12;
53 use warnings;
54
55 use File::Temp;
56 use FindBin;
57
58 require "$FindBin::Bin/tags.pl";
59 epg::flac::archive::tags->import(
60 qw[
61 disc_tags
62 read_tags
63 mangle_for_file_name
64 quote
65 ]);
66
67 sub flac2mp3 {
68 my %a = @_;
69 my $quoted_flac = quote($a{flac});
70
71 say('if metaflac --export-picture-to=flac2mp3.cover.$$ ', $quoted_flac, '; then');
72 say(' pic_options="--ti flac2mp3.cover.$$"');
73 say('fi');
74
75 # This is an old TODO; what's wrong with --ty ?
76 # TODO: Look at TDOR, TDRL, TDRC for date.
77 say(join(' ',
78 'flac',
79 '-cd',
80 $quoted_flac,
81 '|',
82 'lame',
83 '--id3v2-only',
84 '--id3v2-latin1',
85 '--pad-id3v2-size', 0,
86 '--preset standard',
87 '--ta',
88 quote($a{artist}),
89 '--tl',
90 quote($a{album}),
91 '--tn',
92 quote($a{track}),
93 '--tt',
94 quote($a{title}),
95 '--ty',
96 quote($a{date}),
97 '$pic_options',
98 (map { ('--tv', quote("TPE2=$_")) } @{$a{albumartist}}),
99 (map { ('--tv', quote("TPOS=$_")) } @{$a{discnumber}}),
100 '-',
101 quote(
102 mangle_for_file_name(
103 join(' ',
104 $a{artist},
105 $a{album},
106 (map { sprintf('%02d', $_) } @{$a{discnumber}}),
107 $a{track},
108 $a{title},
109 ))
110 . '.mp3'
111 )
112 ));
113 say('unset pic_options');
114 say('rm -f flac2mp3.cover.$$');
115 }
116
117 sub read_tags_metaflac {
118 my $fn = shift;
119 open(my $fh, '-|', 'metaflac', '--no-utf8-convert', '--export-tags-to=-', $fn) || die("metalfac: $!");
120 my @result = read_tags($fh);
121 if (!close($fh)) {
122 if ($! == 0) {
123 die("metaflac exited $?")
124 }
125 die("close(metaflac): $!")
126 }
127 @result
128 }
129
130 sub main {
131 for my $fn (@_) {
132 my ($tags) = read_tags_metaflac($fn);
133 my ($album, $artist, $date, $discnumber) = disc_tags($tags);
134
135 # TODO resurrect whole-disc FLAC?
136 # Stupid hack: only a single-track file should have the
137 # TRACKNUMBER tag, so use it if set for the first pass through
138 # the loop. At the end of the loop, we'll set $track for the
139 # next run, so this continues to work for multi-track files.
140 # if track == None:
141 # track = 1
142 # else:
143 # track = int(track)
144 {
145 my $tracknumber = epg::flac::archive::tags::one(TRACKNUMBER => $tags);
146 my $title = epg::flac::archive::tags::one(TITLE => $tags);
147 # TODO restore PARTNUMBER and VERSION net time i need them
148 flac2mp3(
149 artist => $artist,
150 album => $album,
151 date => $date,
152 discnumber => $discnumber,
153 track => $tracknumber,
154 title => $title,
155 flac => $fn,
156 )
157 }
158 }
159
160 return 0;
161 }
162
163 if (!caller) {
164 exit(main(@ARGV))
165 }
166
167 1;