]>
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>.
51 ''' #' # python-mode is sucks
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
.util
import run_or_die
60 ################################################################################
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 track
= int(track
) # XXX caller should int this
68 (skip_arg
, until_arg
) = skip_until
70 skip_arg
= until_arg
= ''
73 flac_options
= '--silent'
79 if lame_options
!= None:
80 tmp
.append(lame_options
)
82 tmp
.append('--preset standard');
83 quiet
and tmp
.append('--quiet')
84 verbose
and tmp
.append('--verbose')
85 lame_options
= ' '.join(tmp
)
87 # Escape any single quotes ' so we can quote this.
89 album
, date
) = [x
.replace("'", r
"'\''")
90 for x
in (fn
, title
, artist
, album
, date
)]
92 outfile
= ('%s (%s) %02d %s.mp3' % (artist
, album
,
93 track
, title
)).replace('/', '_')
95 run_or_die(3, "flac %s -cd %s %s '%s' | lame %s --tt '%s' --ta '%s' --tl '%s' --ty '%s' --tn %d - '%s'"
96 % (flac_options
, skip_arg
or '', until_arg
or '', fn
,
97 lame_options
, title
, artist
, album
, date
, track
, outfile
))
101 ################################################################################
104 def tformat(m
, s
, c
):
105 return '%02d:%02d.%02d' % (m
, s
, c
)
107 def get_decode_args(fn
):
110 p
= Popen(['metaflac', '--export-cuesheet-to=-', fn
], stdout
=PIPE
)
111 for line
in (x
.rstrip() for x
in p
.stdout
):
112 m
= re
.search(r
'INDEX 01 (\d\d):(\d\d):(\d\d)$', line
)
114 l
.append(map(int, m
.groups()))
116 # XXX dataloss! check status
119 for i
in xrange(len(l
)):
120 arg
= ['--skip=' + tformat(*l
[i
])]
128 arg
.append('--until=' + tformat(next
[0] - 1, 59, 74))
130 arg
.append('--until=' + tformat(next
[0], next
[1] - 1,
133 arg
.append('--until=' + tformat(next
[0], next
[1],
138 # If no cue sheet, stick a dummy in here.
145 '''Return the ARTIST, ALBUM, and DATE tags followed by the TITLE tags
148 date
= discnum
= track
= None
152 p
= Popen(['metaflac', '--export-vc-to=-', fn
], stdout
=PIPE
)
153 for line
in (x
.rstrip() for x
in p
.stdout
):
154 (tag
, value
) = line
.split('=', 1)
156 if re
.match(r
'ARTIST=', line
, re
.IGNORECASE
):
158 elif re
.match(r
'ALBUM=', line
, re
.IGNORECASE
):
160 elif re
.match(r
'DATE=', line
, re
.IGNORECASE
):
162 elif re
.match(r
'DISCNUMBER=', line
, re
.IGNORECASE
):
164 elif re
.match(r
'TRACKNUMBER=', line
, re
.IGNORECASE
):
167 # XXX Need to support per-track of everything but album and
168 # discnum, not just TITLE... Also unify with fa-flacd get_tags.
169 elif re
.match(r
'ARTIST\[', line
, re
.IGNORECASE
):
170 artists
.append(value
)
171 elif re
.match(r
'TITLE', line
, re
.IGNORECASE
):
173 # XXX dataloss! check status
176 # If no TITLEs, stick a dummy in here.
180 return (artist
, album
, date
, discnum
, track
, artists
, titles
)
183 # Control the exit code for any uncaught exceptions.
185 parser
= OptionParser()
186 parser
.disable_interspersed_args()
187 parser
.add_option('-X', '--debug', action
='store_true', default
=False)
188 parser
.add_option('-j', '--jobs', type='int', default
=1)
189 parser
.add_option('--lame-options')
190 parser
.add_option('-q', '--quiet', action
='store_true', default
=False)
191 parser
.add_option('-v', '--verbose', action
='store_true', default
=False)
193 traceback
.print_exc()
197 # Raises SystemExit on invalid options in argv.
198 (options
, args
) = parser
.parse_args(argv
[1:])
199 except Exception, error
:
200 if isinstance(error
, SystemExit):
202 traceback
.print_exc()
206 global debug
, flac_options
, lame_options
, quiet
, verbose
207 debug
= options
.debug
208 lame_options
= options
.lame_options
209 quiet
= options
.quiet
210 verbose
= options
.verbose
215 args
= get_decode_args(fn
)
216 (artist
, album
, date
, discnum
, track
,
217 artists
, titles
) = get_tags(fn
)
219 # lame doesn't seem to support disc number.
221 album
= '%s (disc %d)' % (album
, discnum
)
223 # Stupid hack: only a single-track file should have the
224 # TRACKNUMBER tag, so use it if set for the first pass through
225 # the loop. At the end of the loop, we'll set $track for the
226 # next run, so this continues to work for multi-track files.
230 for i
in range(len(titles
)):
233 jobs
.append([fn
, titles
[i
], artist
, album
,
234 date
, track
, args
[i
]])
236 except Exception, error
:
237 sys
.stderr
.write(getattr(error
, 'msg', ''))
238 traceback
.print_exc()
239 sys
.stderr
.write('Continuing...\n')
246 return lambda: flac2mp3(*job
)
247 org
.diplodocus
.jobs
.run(maxjobs
=options
.jobs
, debug
=debug
, get_job
=getjob
)
248 except Exception, error
:
249 if isinstance(error
, SystemExit):
251 # check all print_exc and format_exc in fa-flacd.py; i think
252 # for some i don't do this msg print check
253 sys
.stderr
.write(getattr(error
, 'msg', ''))
254 traceback
.print_exc()
259 if __name__
== '__main__':
260 sys
.exit(main(sys
.argv
))