]>
diplodocus.org Git - flac-archive/blob - flac2mp3
1 #! /usr/bin/env python2.4
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.
23 =item B<--lame-options> I<lame-options>
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.
29 =item B<-j> [B<--jobs>] I<jobs>
31 Run up to I<jobs> jobs instead of the default 1.
33 =item B<-q> [B<--quiet>]
35 Suppress status information. This option is passed along to B<flac>
38 =item B<-v> [B<--verbose>]
40 Print diagnostic information. This option is passed along to B<flac>
47 Written by Eric Gillespie <epg@pretzelnet.org>.
53 import re
, sys
, traceback
54 from optparse
import OptionParser
55 from subprocess
import Popen
, PIPE
57 import org
.diplodocus
.jobs
58 from org
.diplodocus
import flac
, taglib
59 from org
.diplodocus
.util
import run_or_die
61 ################################################################################
64 def flac2mp3(fn
, title
, artist
, album
, date
, track
, skip_until
, pics
=None):
65 (title
, artist
, album
) = [(x
== None and 'unknown') or x
66 for x
in (title
, artist
, album
)]
71 flac_options
= '--silent'
77 if lame_options
!= None:
78 tmp
.append(lame_options
)
80 tmp
.append('--preset standard');
81 quiet
and tmp
.append('--quiet')
82 verbose
and tmp
.append('--verbose')
83 lame_options
= ' '.join(tmp
)
85 outfile
= ('%s (%s) %02d %s.mp3' % (artist
, album
,
86 track
, title
)).replace('/', '_')
88 # Escape any single quotes ' so we can quote this.
90 album
, date
) = [x
.replace("'", r
"'\''")
91 for x
in (fn
, title
, artist
, album
, date
)]
93 quoted_outfile
= ('%s (%s) %02d %s.mp3' % (artist
, album
,
94 track
, title
)).replace('/', '_')
96 run_or_die(3, "flac %s -cd %s '%s' | lame --add-id3v2 %s --tt '%s' --ta '%s' --tl '%s' --ty '%s' --tn %d - '%s'"
97 % (flac_options
, ' '.join(skip_until
), fn
,
98 lame_options
, title
, artist
, album
, date
, track
,
102 taglib
.add_apic_frame_to_mp3(outfile
, pics
)
106 ################################################################################
109 def tformat(m
, s
, c
):
110 return '%02d:%02d.%02d' % (m
, s
, c
)
112 def get_decode_args(fn
):
115 p
= Popen(['metaflac', '--export-cuesheet-to=-', fn
], stdout
=PIPE
)
116 for line
in (x
.rstrip() for x
in p
.stdout
):
117 m
= re
.search(r
'INDEX 01 (\d\d):(\d\d):(\d\d)$', line
)
119 l
.append(map(int, m
.groups()))
121 # XXX dataloss! check status
124 for i
in xrange(len(l
)):
125 arg
= ['--skip=' + tformat(*l
[i
])]
133 arg
.append('--until=' + tformat(next
[0] - 1, 59, 74))
135 arg
.append('--until=' + tformat(next
[0], next
[1] - 1,
138 arg
.append('--until=' + tformat(next
[0], next
[1],
143 # If no cue sheet, stick a dummy in here.
149 # XXX other things should usue this; flac files, for example, should
150 # get PART as part of the filelname, same as mp3s.
156 # All files have at least one track.
157 return max(1, len(self
._tags
))
158 def get(self
, key
, track
=None):
162 return self
._tags
[track
][key
]
164 return self
._global
[key
]
167 def gets(self
, key
, track
=None):
168 value
= self
.get(key
, track
)
171 return '\n'.join(value
)
172 def set(self
, key
, value
, track
=None):
178 tags
= self
._tags
[track
]
180 tags
= self
._tags
[track
] = {}
183 tags
[key
].append(value
)
186 """Return the ARTIST, ALBUM, and DATE tags followed by the TITLE tags
191 p
= Popen(['metaflac', '--export-tags-to=-', fn
], stdout
=PIPE
)
192 for line
in (x
.rstrip() for x
in p
.stdout
):
193 (tag
, value
) = line
.split('=', 1)
195 m
= re
.search(r
'\[([0-9]+)]$', tag
)
197 tag
= tag
[:m
.start()]
198 track
= int(m
.group(1))
202 tags
.set(tag
, value
, track
)
203 # XXX dataloss! check status
209 # Control the exit code for any uncaught exceptions.
211 parser
= OptionParser()
212 parser
.disable_interspersed_args()
213 parser
.add_option('-X', '--debug', action
='store_true', default
=False)
214 parser
.add_option('-j', '--jobs', type='int', default
=1)
215 parser
.add_option('--lame-options')
216 parser
.add_option('-q', '--quiet', action
='store_true', default
=False)
217 parser
.add_option('-v', '--verbose', action
='store_true', default
=False)
219 traceback
.print_exc()
223 # Raises SystemExit on invalid options in argv.
224 (options
, args
) = parser
.parse_args(argv
[1:])
225 except Exception, error
:
226 if isinstance(error
, SystemExit):
228 traceback
.print_exc()
232 global debug
, flac_options
, lame_options
, quiet
, verbose
233 debug
= options
.debug
234 lame_options
= options
.lame_options
235 quiet
= options
.quiet
236 verbose
= options
.verbose
241 args
= get_decode_args(fn
)
244 album
= tags
.gets('ALBUM')
245 discnum
= tags
.gets('DISCNUMBER')
246 track
= tags
.gets('TRACKNUMBER')
248 # lame doesn't seem to support disc number.
250 album
= '%s (disc %s)' % (album
, discnum
)
252 # Stupid hack: only a single-track file should have the
253 # TRACKNUMBER tag, so use it if set for the first pass through
254 # the loop. At the end of the loop, we'll set $track for the
255 # next run, so this continues to work for multi-track files.
261 pics
= flac
.get_pictures(fn
)
263 for i
in range(len(tags
)):
264 title
= tags
.gets('TITLE', track
)
265 part
= tags
.gets('PART', track
)
267 title
= '%s - %s' % (title
, part
)
268 jobs
.append([fn
, title
,
269 tags
.gets('ARTIST', track
),
271 tags
.gets('DATE', track
),
272 track
, args
[i
], pics
])
274 except Exception, error
:
275 sys
.stderr
.write(getattr(error
, 'msg', ''))
276 traceback
.print_exc()
277 sys
.stderr
.write('Continuing...\n')
284 return lambda: flac2mp3(*job
)
285 org
.diplodocus
.jobs
.run(maxjobs
=options
.jobs
, debug
=debug
, get_job
=getjob
)
286 except Exception, error
:
287 if isinstance(error
, SystemExit):
289 # check all print_exc and format_exc in fa-flacd.py; i think
290 # for some i don't do this msg print check
291 sys
.stderr
.write(getattr(error
, 'msg', ''))
292 traceback
.print_exc()
297 if __name__
== '__main__':
298 sys
.exit(main(sys
.argv
))