]>
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.
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
.util
import run_or_die
60 from flac_archive
import flac
, taglib
61 from flac_archive
.tags
import Tags
63 ################################################################################
66 def flac2mp3(fn
, title
, artist
, album_artist
, album
, date
,
67 track
, skip_until
, pics
=None):
68 (title
, artist
, album
) = [(x
== None and 'unknown') or x
69 for x
in (title
, artist
, album
)]
74 flac_options
= '--silent'
80 if lame_options
!= None:
81 tmp
.append(lame_options
)
83 tmp
.append('--preset standard');
84 quiet
and tmp
.append('--quiet')
85 verbose
and tmp
.append('--verbose')
86 lame_options
= ' '.join(tmp
)
88 outfile
= ('%s (%s) %02d %s.mp3' % (artist
, album
,
89 track
, title
)).replace('/', '_')
91 # Escape any single quotes ' so we can quote this.
92 (fn
, title
, artist
, album_artist
,
93 album
, date
) = [(x
or '').replace("'", r
"'\''")
94 for x
in [fn
, title
, artist
, album_artist
, album
, date
]]
96 album_artist_options
= ''
98 album_artist_options
= "--tv 'TPE2=%s'" % album_artist
100 quoted_outfile
= ('%s (%s) %02d %s.mp3' % (artist
, album
,
101 track
, title
)).replace('/', '_')
102 run_or_die(3, "flac %s -cd %s '%s' | lame --id3v2-only %s --tt '%s' --ta '%s' --tl '%s' --ty '%s' --tn %d %s - '%s'"
103 % (flac_options
, ' '.join(skip_until
), fn
,
104 lame_options
, title
, artist
, album
, date
, track
,
105 album_artist_options
, quoted_outfile
))
108 taglib
.add_apic_frame_to_mp3(outfile
, pics
)
112 ################################################################################
115 def tformat(m
, s
, c
):
116 return '%02d:%02d.%02d' % (m
, s
, c
)
118 def get_decode_args(fn
):
121 p
= Popen(['metaflac', '--export-cuesheet-to=-', fn
], stdout
=PIPE
)
122 for line
in (x
.rstrip() for x
in p
.stdout
):
123 m
= re
.search(r
'INDEX 01 (\d\d):(\d\d):(\d\d)$', line
)
125 l
.append(map(int, m
.groups()))
127 # XXX dataloss! check status
130 for i
in xrange(len(l
)):
131 arg
= ['--skip=' + tformat(*l
[i
])]
139 arg
.append('--until=' + tformat(next
[0] - 1, 59, 74))
141 arg
.append('--until=' + tformat(next
[0], next
[1] - 1,
144 arg
.append('--until=' + tformat(next
[0], next
[1],
149 # If no cue sheet, stick a dummy in here.
156 """Return the ARTIST, ALBUM, and DATE tags followed by the TITLE tags
161 p
= Popen(['metaflac', '--export-tags-to=-', fn
], stdout
=PIPE
)
164 # XXX dataloss! check status
170 # Control the exit code for any uncaught exceptions.
172 parser
= OptionParser()
173 parser
.disable_interspersed_args()
174 parser
.add_option('-X', '--debug', action
='store_true', default
=False)
175 parser
.add_option('-j', '--jobs', type='int', default
=1)
176 parser
.add_option('--lame-options')
177 parser
.add_option('-q', '--quiet', action
='store_true', default
=False)
178 parser
.add_option('-v', '--verbose', action
='store_true', default
=False)
180 traceback
.print_exc()
184 # Raises SystemExit on invalid options in argv.
185 (options
, args
) = parser
.parse_args(argv
[1:])
186 except Exception, error
:
187 if isinstance(error
, SystemExit):
189 traceback
.print_exc()
194 global debug
, flac_options
, lame_options
, quiet
, verbose
195 debug
= options
.debug
196 lame_options
= options
.lame_options
197 quiet
= options
.quiet
198 verbose
= options
.verbose
203 args
= get_decode_args(fn
)
206 album
= tags
.gets('ALBUM', separator
=separator
)
207 discnum
= tags
.gets('DISCNUMBER')
208 track
= tags
.gets('TRACKNUMBER')
210 # lame doesn't seem to support disc number.
212 album
= '%s (disc %s)' % (album
, discnum
)
214 # Stupid hack: only a single-track file should have the
215 # TRACKNUMBER tag, so use it if set for the first pass through
216 # the loop. At the end of the loop, we'll set $track for the
217 # next run, so this continues to work for multi-track files.
223 pics
= flac
.get_pictures(fn
)
225 for i
in range(len(tags
)):
226 title
= tags
.gets('TITLE', track
, separator
)
227 part
= tags
.gets('PART', track
)
229 title
= '%s - %s' % (title
, part
)
230 artist
= tags
.get('ARTIST', track
)
231 artist
.extend(tags
.get('FEATURING', track
))
232 album_artist
= tags
.gets('ALBUMARTIST', track
)
233 jobs
.append([fn
, title
,
236 tags
.gets('DATE', track
),
237 track
, args
[i
], pics
])
239 except Exception, error
:
240 sys
.stderr
.write(getattr(error
, 'msg', ''))
241 traceback
.print_exc()
242 sys
.stderr
.write('Continuing...\n')
249 return lambda: flac2mp3(*job
)
250 org
.diplodocus
.jobs
.run(maxjobs
=options
.jobs
, debug
=debug
, get_job
=getjob
)
251 except Exception, error
:
252 if isinstance(error
, SystemExit):
254 # check all print_exc and format_exc in fa-flacd.py; i think
255 # for some i don't do this msg print check
256 sys
.stderr
.write(getattr(error
, 'msg', ''))
257 traceback
.print_exc()
262 if __name__
== '__main__':
263 sys
.exit(main(sys
.argv
))