]> diplodocus.org Git - flac-archive/blob - flac2mp3
perltidy -bt=2 -pt=2 -nvc
[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 track_tags
62 read_tags_metaflac
63 mangle_for_file_name
64 quote
65 two_digits
66 ]
67 );
68
69 sub flac2mp3 {
70 my $mp3 = shift;
71 my $flac = shift;
72 my $tags = shift;
73
74 # This is an old TODO; what's wrong with --ty ?
75 # TODO: Look at TDOR, TDRL, TDRC for date.
76 say(
77 join(
78 ' ',
79 'flac',
80 '-cd',
81 quote($flac),
82 '|',
83 'lame',
84 '--id3v2-only',
85 '--id3v2-latin1',
86 '--pad-id3v2-size', 0,
87 '--preset standard',
88 '--ta',
89 quote($tags->{artist}),
90 '--tl',
91 quote($tags->{album}),
92 '--tn',
93 quote($tags->{tracknumber}),
94 '--tt',
95 quote($tags->{title}),
96 '--ty',
97 quote($tags->{date}),
98 '$pic_options',
99
100 #(map { ('--tv', quote("TPE2=$_")) } @{$albumartist}),
101 (map { ('--tv', quote("TPOS=$_")) } @{$tags->{discnumber}}),
102 '-',
103 quote($mp3),
104 )
105 );
106 }
107
108 sub main {
109 for my $flac (@_) {
110 say('metaflac --export-picture-to=flac2mp3.cover.$$ ',
111 quote($flac), ' && pic_options="--ti flac2mp3.cover.$$"');
112 my %tags = track_tags(read_tags_metaflac($flac));
113 flac2mp3(
114 mangle_for_file_name(
115 join(' ',
116 $tags{artist},
117 $tags{album},
118 (map { two_digits($_) } @{$tags{discnumber}}),
119 two_digits($tags{tracknumber}),
120 $tags{title},
121 )
122 )
123 . '.mp3',
124 $flac,
125 \%tags,
126 );
127 say('unset pic_options');
128 }
129 say('rm -f flac2mp3.cover.$$');
130
131 return 0;
132 }
133
134 if (!caller) {
135 exit(main(@ARGV));
136 }
137
138 1;