6 B<flac2mp3> - transcode FLAC file to MP3 files
10 B<flac2mp3> [B<--lame-options> I<lame-options>] [B<-j> I<jobs>] [B<-q>] [B<-v>] I<file> [...]
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.
19 Note that lame is retarded, and parses B<LANG> directly itself! So, in order
20 for it to transcode textual tags, you must specify the encoding in LANG, e.g.
27 =item B<--lame-options> I<lame-options>
29 Pass I<lame-options> to B<lame>. This ends up being passed to the
30 shell, so feel free to take advantage of that. You'll almost
31 certainly have to put I<lame-options> in single quotes.
33 =item B<-j> [B<--jobs>] I<jobs>
35 Run up to I<jobs> jobs instead of the default 1.
37 =item B<-q> [B<--quiet>]
39 Suppress status information. This option is passed along to B<flac>
42 =item B<-v> [B<--verbose>]
44 Print diagnostic information. This option is passed along to B<flac>
51 Written by Eric Gillespie <epg@pretzelnet.org>.
57 import os, re, sys, tempfile, traceback
58 from optparse import OptionParser
59 from subprocess import Popen, PIPE
61 import org.diplodocus.jobs
62 from org.diplodocus.util import run_or_die
64 from flac_archive import flac
65 from flac_archive.tags import Tags
67 ################################################################################
70 def flac2mp3(fn, title, artist, album_artist, album, date,
71 track, skip_until, pics=None):
72 (title, artist, album) = [(x == None and 'unknown') or x
73 for x in (title, artist, album)]
78 flac_options = '--silent'
84 if lame_options != None:
85 tmp.append(lame_options)
87 tmp.append('--preset standard');
88 quiet and tmp.append('--quiet')
89 verbose and tmp.append('--verbose')
90 lame_options = ' '.join(tmp)
92 outfile = ('%s (%s) %02d %s.mp3' % (artist, album,
93 track, title)).replace('/', '_')
95 # Escape any single quotes ' so we can quote this.
96 (fn, title, artist, album_artist,
97 album, date) = [(x or '').replace("'", r"'\''")
98 for x in [fn, title, artist, album_artist, album, date]]
100 album_artist_options = ''
102 album_artist_options = "--tv 'TPE2=%s'" % album_artist
104 quoted_outfile = ('%s (%s) %02d %s.mp3' % (artist, album,
105 track, title)).replace('/', '_')
109 (fd, picfn) = tempfile.mkstemp()
110 f = os.fdopen(fd, 'wb')
113 pic_options = "--ti '%s'" % picfn
115 run_or_die(3, "flac %s -cd %s '%s' | lame --id3v2-only --id3v2-latin1 --pad-id3v2-size 0 %s --tt '%s' --ta '%s' --tl '%s' --ty '%s' --tn %d %s %s - '%s'"
116 % (flac_options, ' '.join(skip_until), fn,
117 lame_options, title, artist, album, date, track,
118 pic_options, album_artist_options, quoted_outfile))
128 ################################################################################
131 def tformat(m, s, c):
132 return '%02d:%02d.%02d' % (m, s, c)
134 def get_decode_args(fn):
137 p = Popen(['metaflac', '--export-cuesheet-to=-', fn], stdout=PIPE)
138 for line in (x.rstrip() for x in p.stdout):
139 m = re.search(r'INDEX 01 (\d\d):(\d\d):(\d\d)$', line)
141 l.append(map(int, m.groups()))
143 # XXX dataloss! check status
146 for i in xrange(len(l)):
147 arg = ['--skip=' + tformat(*l[i])]
155 arg.append('--until=' + tformat(next[0] - 1, 59, 74))
157 arg.append('--until=' + tformat(next[0], next[1] - 1,
160 arg.append('--until=' + tformat(next[0], next[1],
165 # If no cue sheet, stick a dummy in here.
172 """Return the ARTIST, ALBUM, and DATE tags followed by the TITLE tags
177 p = Popen(['metaflac', '--export-tags-to=-', fn], stdout=PIPE)
180 # XXX dataloss! check status
186 # Control the exit code for any uncaught exceptions.
188 parser = OptionParser()
189 parser.disable_interspersed_args()
190 parser.add_option('-X', '--debug', action='store_true', default=False)
191 parser.add_option('-j', '--jobs', type='int', default=1)
192 parser.add_option('--lame-options')
193 parser.add_option('-q', '--quiet', action='store_true', default=False)
194 parser.add_option('-v', '--verbose', action='store_true', default=False)
196 traceback.print_exc()
200 # Raises SystemExit on invalid options in argv.
201 (options, args) = parser.parse_args(argv[1:])
202 except Exception, error:
203 if isinstance(error, SystemExit):
205 traceback.print_exc()
210 global debug, flac_options, lame_options, quiet, verbose
211 debug = options.debug
212 lame_options = options.lame_options
213 quiet = options.quiet
214 verbose = options.verbose
219 args = get_decode_args(fn)
222 album = tags.gets('ALBUM', separator=separator)
223 discnum = tags.gets('DISCNUMBER')
224 track = tags.gets('TRACKNUMBER')
226 # lame doesn't seem to support disc number.
228 album = '%s (disc %s)' % (album, discnum)
230 # Stupid hack: only a single-track file should have the
231 # TRACKNUMBER tag, so use it if set for the first pass through
232 # the loop. At the end of the loop, we'll set $track for the
233 # next run, so this continues to work for multi-track files.
239 pics = flac.get_pictures(fn)
241 for i in range(len(tags)):
242 title = tags.gets('TITLE', track, separator)
243 part = tags.gets('PART', track)
245 title = '%s - %s' % (title, part)
246 artist = tags.get('ARTIST', track)
247 artist.extend(tags.get('FEATURING', track))
248 album_artist = tags.gets('ALBUMARTIST', track)
249 jobs.append([fn, title,
252 tags.gets('DATE', track),
253 track, args[i], pics])
255 except Exception, error:
256 sys.stderr.write(getattr(error, 'msg', ''))
257 traceback.print_exc()
258 sys.stderr.write('Continuing...\n')
265 return lambda: flac2mp3(*job)
266 org.diplodocus.jobs.run(maxjobs=options.jobs, debug=debug, get_job=getjob)
267 except Exception, error:
268 if isinstance(error, SystemExit):
270 # check all print_exc and format_exc in fa-flacd.py; i think
271 # for some i don't do this msg print check
272 sys.stderr.write(getattr(error, 'msg', ''))
273 traceback.print_exc()
278 if __name__ == '__main__':
279 sys.exit(main(sys.argv))