]>
diplodocus.org Git - flac-archive/blob - flac2mp3
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
.tags
import Tags
66 ################################################################################
69 def flac2mp3(fn
, title
, artist
, album_artist
, album
, discnum
, date
,
71 (title
, artist
, album
) = [(x
== None and 'unknown') or x
72 for x
in (title
, artist
, album
)]
77 flac_options
= '--silent'
83 if lame_options
!= None:
84 tmp
.append(lame_options
)
86 tmp
.append('--preset standard');
87 quiet
and tmp
.append('--quiet')
88 verbose
and tmp
.append('--verbose')
89 lame_options
= ' '.join(tmp
)
93 # Escape any single quotes ' so we can quote this.
94 (fn
, title
, artist
, album_artist
,
95 album
, date
) = [(x
or '').replace("'", r
"'\''")
96 for x
in [fn
, title
, artist
, album_artist
, album
, date
]]
98 album_artist_options
= ''
100 album_artist_options
= "--tv 'TPE2=%s'" % album_artist
102 outfile_album
= album
105 outfile_album
= '%s (disc %s)' % (album
, discnum
)
106 discnum_options
= "--tv 'TPOS=%d'" % int(discnum
)
108 quoted_outfile
= ('%s (%s) %02d %s.mp3' % (artist
, outfile_album
,
109 track
, title
)).replace('/', '_')
112 return quoted_outfile
.replace(r
"'\''", "'")
115 (fd
, picfn
) = tempfile
.mkstemp()
117 p
= Popen(['metaflac', '--export-picture-to', picfn
, unquoted_fn
],
120 stderr
= ''.join(p
.stderr
)
121 # Hacky check for flac with no album art
122 if 'no PICTURE block' in stderr
:
123 # That's fine, just no picture.
127 sys
.stderr
.write('metaflac exited %d: %s\n' % (status
, stderr
))
129 pic_options
= "--ti '%s'" % picfn
131 # TODO: Look at TDOR, TDRL, TDRC for date.
132 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 - '%s'"
133 % (flac_options
, ' '.join(skip_until
), fn
,
134 lame_options
, title
, artist
, album
, date
, track
,
135 pic_options
, album_artist_options
,
136 discnum_options
, quoted_outfile
))
145 ################################################################################
148 def tformat(m
, s
, c
):
149 return '%02d:%02d.%02d' % (m
, s
, c
)
151 def get_decode_args(fn
):
154 p
= Popen(['metaflac', '--no-utf8-convert', '--export-cuesheet-to=-', fn
],
156 for line
in (x
.rstrip() for x
in p
.stdout
):
157 m
= re
.search(r
'INDEX 01 (\d\d):(\d\d):(\d\d)$', line
)
159 l
.append(map(int, m
.groups()))
161 # XXX dataloss! check status
164 for i
in xrange(len(l
)):
165 arg
= ['--skip=' + tformat(*l
[i
])]
173 arg
.append('--until=' + tformat(next
[0] - 1, 59, 74))
175 arg
.append('--until=' + tformat(next
[0], next
[1] - 1,
178 arg
.append('--until=' + tformat(next
[0], next
[1],
183 # If no cue sheet, stick a dummy in here.
190 """Return the ARTIST, ALBUM, and DATE tags followed by the TITLE tags
195 p
= Popen(['metaflac', '--no-utf8-convert', '--export-tags-to=-', fn
],
199 # XXX dataloss! check status
205 # Control the exit code for any uncaught exceptions.
207 parser
= OptionParser()
208 parser
.disable_interspersed_args()
209 parser
.add_option('-X', '--debug', action
='store_true', default
=False)
210 parser
.add_option('-j', '--jobs', type='int', default
=1)
211 parser
.add_option('--lame-options')
212 parser
.add_option('-q', '--quiet', action
='store_true', default
=False)
213 parser
.add_option('-v', '--verbose', action
='store_true', default
=False)
214 parser
.add_option('--check-missing-files', action
='store_true',
217 traceback
.print_exc()
221 # Raises SystemExit on invalid options in argv.
222 (options
, args
) = parser
.parse_args(argv
[1:])
223 except Exception, error
:
224 if isinstance(error
, SystemExit):
226 traceback
.print_exc()
231 global debug
, flac_options
, lame_options
, quiet
, verbose
233 check_missing
= options
.check_missing_files
234 debug
= options
.debug
235 lame_options
= options
.lame_options
236 quiet
= options
.quiet
237 verbose
= options
.verbose
242 args
= get_decode_args(fn
)
245 album
= tags
.gets('ALBUM', separator
=separator
)
246 discnum
= tags
.gets('DISCNUMBER')
247 track
= tags
.gets('TRACKNUMBER')
249 # Stupid hack: only a single-track file should have the
250 # TRACKNUMBER tag, so use it if set for the first pass through
251 # the loop. At the end of the loop, we'll set $track for the
252 # next run, so this continues to work for multi-track files.
258 for i
in range(len(tags
)):
259 title
= tags
.gets('TITLE', track
, separator
)
260 part
= tags
.gets('PART', track
)
262 title
= '%s - %s' % (title
, part
)
263 version
= tags
.gets('VERSION', track
)
265 title
= '%s (%s)' % (title
, version
)
266 artist
= tags
.get('ARTIST', track
)
267 artist
.extend(tags
.get('FEATURING', track
))
268 album_artist
= tags
.gets('ALBUMARTIST', track
)
270 mp3
= flac2mp3(fn
, title
,
272 album_artist
, album
, discnum
,
273 tags
.gets('DATE', track
),
275 if not os
.path
.exists(mp3
):
279 jobs
.append((fn
, title
,
281 album_artist
, album
, discnum
,
282 tags
.gets('DATE', track
),
285 except Exception, error
:
286 sys
.stderr
.write(getattr(error
, 'msg', ''))
287 traceback
.print_exc()
288 sys
.stderr
.write('Continuing...\n')
295 return lambda: flac2mp3(*job
)
296 org
.diplodocus
.jobs
.run(maxjobs
=options
.jobs
, debug
=debug
, get_job
=getjob
)
297 except Exception, error
:
298 if isinstance(error
, SystemExit):
300 # check all print_exc and format_exc in fa-flacd.py; i think
301 # for some i don't do this msg print check
302 sys
.stderr
.write(getattr(error
, 'msg', ''))
303 traceback
.print_exc()
308 if __name__
== '__main__':
309 sys
.exit(main(sys
.argv
))