]> diplodocus.org Git - flac-archive/blob - flac2mp3
Jul 25 18:48
[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 use lib $FindBin::Bin;
59
60 require 'tags.pl';
61 epg::flac::archive::tags->import(
62 qw[
63 track_tags
64 read_tags_metaflac
65 mangle_for_file_name
66 quote
67 two_digits
68 ]
69 );
70
71 sub flac2mp3 {
72 my $mp3 = shift;
73 my $flac = shift;
74 my $tags = shift;
75
76 my @version;
77 if (defined($tags->{version})) {
78 @version = ('(' . $tags->{version} . ')');
79 }
80
81 # This is an old TODO; what's wrong with --ty ?
82 # TODO: Look at TDOR, TDRL, TDRC for date.
83 say(
84 join(
85 ' ',
86 'flac',
87 '-cd',
88 quote($flac),
89 '|',
90 'lame',
91 '--id3v2-only',
92 '--id3v2-latin1',
93 '--pad-id3v2-size', 0,
94 '--preset standard',
95 '--ta',
96 quote($tags->{artist}),
97 '--tl',
98 quote($tags->{album}),
99 '--tn',
100 quote($tags->{tracknumber}),
101 '--tt',
102 quote(join(' ', $tags->{title}, @version)),
103 '--ty',
104 quote($tags->{date}),
105 '$pic_options',
106
107 #(map { ('--tv', quote("TPE2=$_")) } @{$albumartist}),
108 (map { ('--tv', quote("TPOS=$_")) } @{$tags->{discnumber}}),
109 '-',
110 quote($mp3),
111 )
112 );
113 }
114
115 sub filename {
116 my $tags = shift;
117 mangle_for_file_name(
118 join(' ',
119 @{$tags->{ARTIST}},
120 @{$tags->{ALBUM}},
121 (map { two_digits($_) } @{$tags->{DISCNUMBER} // []}),
122 (map { two_digits($_) } @{$tags->{TRACKNUMBER} // []}),
123 @{$tags->{TITLE}},
124 @{$tags->{VERSION} // []},
125 @{$tags->{PARTNUMBER} // []},
126 )
127 ) . '.mp3';
128 }
129
130 sub main {
131 for my $flac (@_) {
132 say('metaflac --export-picture-to=flac2mp3.cover.$$ ',
133 quote($flac), ' && pic_options="--ti flac2mp3.cover.$$"');
134
135 # TODO multi-track
136 my ($tags) = read_tags_metaflac($flac);
137 flac2mp3(filename($tags), $flac, {track_tags($tags)});
138 say('unset pic_options');
139 }
140 say('rm -f flac2mp3.cover.$$');
141
142 return 0;
143 }
144
145 if (!caller) {
146 exit(main(@ARGV));
147 }
148
149 1;