]> diplodocus.org Git - flac-archive/blob - flac2mp3
Use album.release where possible, restrict
[flac-archive] / flac2mp3
1 #! /usr/bin/env python2.4
2
3 '''
4 =head1 NAME
5
6 B<flac2mp3> - transcode FLAC file to MP3 files
7
8 =head1 SYNOPSIS
9
10 B<flac2mp3> [B<--lame-options> I<lame-options>] [B<-j> I<jobs>] [B<-q>] [B<-v>] I<file> [...]
11
12 =head1 DESCRIPTION
13
14 B<flac2mp3> transcodes the FLAC files I<file> to MP3 files. I<file>
15 may be the kind of FLAC file B<fa-flacd> generates. That is, it
16 contains a cue sheet, one TITLE tag per track listed therein, and
17 ARTIST, ALBUM, and DATE tags.
18
19 =head1 OPTIONS
20
21 =over 4
22
23 =item B<--lame-options> I<lame-options>
24
25 Pass I<lame-options> to B<lame>. This ends up being passed to the
26 shell, so feel free to take advantage of that. You'll almost
27 certainly have to put I<lame-options> in single quotes.
28
29 =item B<-j> [B<--jobs>] I<jobs>
30
31 Run up to I<jobs> jobs instead of the default 1.
32
33 =item B<-q> [B<--quiet>]
34
35 Suppress status information. This option is passed along to B<flac>
36 and B<lame>.
37
38 =item B<-v> [B<--verbose>]
39
40 Print diagnostic information. This option is passed along to B<flac>
41 and B<lame>.
42
43 =back
44
45 =head1 AUTHORS
46
47 Written by Eric Gillespie <epg@pretzelnet.org>.
48
49 =cut
50
51 ''' #' # python-mode is sucks
52
53 import re, sys, traceback
54 from optparse import OptionParser
55 from subprocess import Popen, PIPE
56
57 import org.diplodocus.jobs
58 from org.diplodocus.util import run_or_die
59
60 ################################################################################
61 # The child processes
62
63 def flac2mp3(fn, title, artist, album, date, track, skip_until):
64 (title, artist, album, date) = [(x == None and 'unknown') or x
65 for x in (title, artist, album, date)]
66 try:
67 (skip_arg, until_arg) = skip_until
68 except ValueError:
69 skip_arg = until_arg = ''
70
71 if quiet:
72 flac_options = '--silent'
73 else:
74 flac_options = ''
75
76 tmp = []
77 global lame_options
78 if lame_options != None:
79 tmp.append(lame_options)
80 else:
81 tmp.append('--preset standard');
82 quiet and tmp.append('--quiet')
83 verbose and tmp.append('--verbose')
84 lame_options = ' '.join(tmp)
85
86 # Escape any single quotes ' so we can quote this.
87 (fn, title, artist,
88 album, date) = [x.replace("'", r"'\''")
89 for x in (fn, title, artist, album, date)]
90
91 outfile = ('%s (%s) %02d %s.mp3' % (artist, album,
92 track, title)).replace('/', '_')
93
94 run_or_die(3, "flac %s -cd %s %s '%s' | lame %s --tt '%s' --ta '%s' --tl '%s' --ty '%s' --tn %d - '%s'"
95 % (flac_options, skip_arg or '', until_arg or '', fn,
96 lame_options, title, artist, album, date, track, outfile))
97
98 return 0
99
100 ################################################################################
101 # The master process
102
103 def tformat(m, s, c):
104 return '%02d:%02d.%02d' % (m, s, c)
105
106 def get_decode_args(fn):
107 l = []
108
109 p = Popen(['metaflac', '--export-cuesheet-to=-', fn], stdout=PIPE)
110 for line in (x.rstrip() for x in p.stdout):
111 m = re.search(r'INDEX 01 (\d\d):(\d\d):(\d\d)$', line)
112 if m != None:
113 l.append(map(int, m.groups()))
114 status = p.wait()
115 # XXX dataloss! check status
116
117 args = []
118 for i in xrange(len(l)):
119 arg = ['--skip=' + tformat(*l[i])]
120 try:
121 next = l[i + 1]
122 except IndexError:
123 next = None
124 if next != None:
125 if next[2] == 0:
126 if next[1] == 0:
127 arg.append('--until=' + tformat(next[0] - 1, 59, 74))
128 else:
129 arg.append('--until=' + tformat(next[0], next[1] - 1,
130 74))
131 else:
132 arg.append('--until=' + tformat(next[0], next[1],
133 next[2] - 1))
134
135 args.append(arg)
136
137 # If no cue sheet, stick a dummy in here.
138 if len(args) == 0:
139 args = [[]]
140
141 return args
142
143 # XXX other things should usue this; flac files, for example, should
144 # get PART as part of the filelname, same as mp3s.
145 class Tags(object):
146 def __init__(self):
147 self._tags = {}
148 def __len__(self):
149 return len(self._tags)
150 def get(self, key, track=None):
151 key = key.upper()
152 try:
153 if track == None:
154 return self._tags[None][key]
155 try:
156 return self._tags[track][key]
157 except KeyError:
158 return self._tags[None][key]
159 except KeyError:
160 return None
161 def gets(self, key, track=None):
162 value = self.get(key, track)
163 if value == None:
164 return None
165 return '\n'.join(value)
166 def set(self, key, value, track=None):
167 if track not in self._tags:
168 self._tags[track] = {}
169 if key not in self._tags[track]:
170 self._tags[track][key] = []
171 self._tags[track][key].append(value)
172
173 def get_tags(fn):
174 '''Return the ARTIST, ALBUM, and DATE tags followed by the TITLE tags
175 in the file FN.'''
176
177 tags = Tags()
178
179 p = Popen(['metaflac', '--export-vc-to=-', fn], stdout=PIPE)
180 for line in (x.rstrip() for x in p.stdout):
181 (tag, value) = line.split('=', 1)
182
183 m = re.search(r'\[([0-9]+)]$', tag)
184 if m != None:
185 tag = tag[:m.start()]
186 track = int(m.group(1))
187 else:
188 track = None
189
190 tags.set(tag, value, track)
191 # XXX dataloss! check status
192 status = p.wait()
193
194 return tags
195
196 def main(argv):
197 # Control the exit code for any uncaught exceptions.
198 try:
199 parser = OptionParser()
200 parser.disable_interspersed_args()
201 parser.add_option('-X', '--debug', action='store_true', default=False)
202 parser.add_option('-j', '--jobs', type='int', default=1)
203 parser.add_option('--lame-options')
204 parser.add_option('-q', '--quiet', action='store_true', default=False)
205 parser.add_option('-v', '--verbose', action='store_true', default=False)
206 except:
207 traceback.print_exc()
208 return 2
209
210 try:
211 # Raises SystemExit on invalid options in argv.
212 (options, args) = parser.parse_args(argv[1:])
213 except Exception, error:
214 if isinstance(error, SystemExit):
215 return 1
216 traceback.print_exc()
217 return 2
218
219 try:
220 global debug, flac_options, lame_options, quiet, verbose
221 debug = options.debug
222 lame_options = options.lame_options
223 quiet = options.quiet
224 verbose = options.verbose
225
226 jobs = []
227 for fn in args:
228 try:
229 args = get_decode_args(fn)
230
231 tags = get_tags(fn)
232 album = tags.gets('ALBUM')
233 discnum = tags.gets('DISCNUMBER')
234 track = tags.gets('TRACKNUMBER')
235
236 # lame doesn't seem to support disc number.
237 if discnum != None:
238 album = '%s (disc %s)' % (album, discnum)
239
240 # Stupid hack: only a single-track file should have the
241 # TRACKNUMBER tag, so use it if set for the first pass through
242 # the loop. At the end of the loop, we'll set $track for the
243 # next run, so this continues to work for multi-track files.
244 if track == None:
245 track = 1
246 else:
247 track = int(track)
248
249 for i in range(len(tags)):
250 title = tags.gets('TITLE', track)
251 part = tags.gets('PART', track)
252 if part != None:
253 title = '%s - %s' % (title, part)
254 jobs.append([fn, title,
255 tags.gets('ARTIST', track),
256 album,
257 tags.gets('DATE', track),
258 track, args[i]])
259 track = i + 2
260 except Exception, error:
261 sys.stderr.write(getattr(error, 'msg', ''))
262 traceback.print_exc()
263 sys.stderr.write('Continuing...\n')
264
265 def getjob(reap):
266 try:
267 job = jobs.pop(0)
268 except IndexError:
269 return
270 return lambda: flac2mp3(*job)
271 org.diplodocus.jobs.run(maxjobs=options.jobs, debug=debug, get_job=getjob)
272 except Exception, error:
273 if isinstance(error, SystemExit):
274 raise
275 # check all print_exc and format_exc in fa-flacd.py; i think
276 # for some i don't do this msg print check
277 sys.stderr.write(getattr(error, 'msg', ''))
278 traceback.print_exc()
279 return 2
280
281 return 0
282
283 if __name__ == '__main__':
284 sys.exit(main(sys.argv))