X-Git-Url: https://diplodocus.org/git/flac-archive/blobdiff_plain/bd4b0cf0cecfd135c579924b155a021c6be99d5c..31cfad82f74bdd8d803c214e4755bd9c82d8e4db:/flac2mp3 diff --git a/flac2mp3 b/flac2mp3 index 20ab476..fa6e26d 100755 --- a/flac2mp3 +++ b/flac2mp3 @@ -1,6 +1,6 @@ #! /usr/bin/env python2.4 -''' +""" =head1 NAME B - transcode FLAC file to MP3 files @@ -48,7 +48,7 @@ Written by Eric Gillespie . =cut -''' #' # python-mode is sucks +""" import re, sys, traceback from optparse import OptionParser @@ -58,16 +58,16 @@ import org.diplodocus.jobs from org.diplodocus import flac, taglib from org.diplodocus.util import run_or_die +from flac_archive.tags import Tags + ################################################################################ # The child processes -def flac2mp3(fn, title, artist, album, date, track, skip_until, pic=None): - (title, artist, album, date) = [(x == None and 'unknown') or x - for x in (title, artist, album, date)] - try: - (skip_arg, until_arg) = skip_until - except ValueError: - skip_arg = until_arg = '' +def flac2mp3(fn, title, artist, album, date, track, skip_until, pics=None): + (title, artist, album) = [(x == None and 'unknown') or x + for x in (title, artist, album)] + if date == None: + date = '' if quiet: flac_options = '--silent' @@ -84,20 +84,24 @@ def flac2mp3(fn, title, artist, album, date, track, skip_until, pic=None): verbose and tmp.append('--verbose') lame_options = ' '.join(tmp) + outfile = ('%s (%s) %02d %s.mp3' % (artist, album, + track, title)).replace('/', '_') + # Escape any single quotes ' so we can quote this. (fn, title, artist, album, date) = [x.replace("'", r"'\''") for x in (fn, title, artist, album, date)] - outfile = ('%s (%s) %02d %s.mp3' % (artist, album, - track, title)).replace('/', '_') + quoted_outfile = ('%s (%s) %02d %s.mp3' % (artist, album, + track, title)).replace('/', '_') - run_or_die(3, "flac %s -cd %s %s '%s' | lame --add-id3v2 %s --tt '%s' --ta '%s' --tl '%s' --ty '%s' --tn %d - '%s'" - % (flac_options, skip_arg or '', until_arg or '', fn, - lame_options, title, artist, album, date, track, outfile)) + run_or_die(3, "flac %s -cd %s '%s' | lame --add-id3v2 %s --tt '%s' --ta '%s' --tl '%s' --ty '%s' --tn %d - '%s'" + % (flac_options, ' '.join(skip_until), fn, + lame_options, title, artist, album, date, track, + quoted_outfile)) - if pic != None: - taglib.add_apic_frame_to_mp3(outfile, pic[0], pic[1], pic[2]) + if pics != None: + taglib.add_apic_frame_to_mp3(outfile, pics) return 0 @@ -144,71 +148,20 @@ def get_decode_args(fn): return args -# XXX other things should usue this; flac files, for example, should -# get PART as part of the filelname, same as mp3s. -class Tags(object): - def __init__(self): - self._tags = {} - def __len__(self): - return len(self._tags) - def get(self, key, track=None): - key = key.upper() - try: - if track == None: - return self._tags[None][key] - try: - return self._tags[track][key] - except KeyError: - return self._tags[None][key] - except KeyError: - return None - def gets(self, key, track=None): - value = self.get(key, track) - if value == None: - return None - return '\n'.join(value) - def set(self, key, value, track=None): - if track not in self._tags: - self._tags[track] = {} - if key not in self._tags[track]: - self._tags[track][key] = [] - self._tags[track][key].append(value) - def get_tags(fn): - '''Return the ARTIST, ALBUM, and DATE tags followed by the TITLE tags - in the file FN.''' + """Return the ARTIST, ALBUM, and DATE tags followed by the TITLE tags + in the file FN.""" tags = Tags() p = Popen(['metaflac', '--export-tags-to=-', fn], stdout=PIPE) - for line in (x.rstrip() for x in p.stdout): - (tag, value) = line.split('=', 1) - - m = re.search(r'\[([0-9]+)]$', tag) - if m != None: - tag = tag[:m.start()] - track = int(m.group(1)) - else: - track = None + tags.load(p.stdout) - tags.set(tag, value, track) # XXX dataloss! check status status = p.wait() return tags -def find_pic(fn, tags): - pic = tags.get('__flac2mp3_PICTURE') - - if not isinstance(pic, tuple): - for i in flac.get_pictures(fn): - if i[1] == flac.PICTURE_TYPE_FRONT_COVER: - pic = i[:3] - break - tags.set('__flac2mp3_PICTURE', pic) - - return pic - def main(argv): # Control the exit code for any uncaught exceptions. try: @@ -232,6 +185,7 @@ def main(argv): traceback.print_exc() return 2 + separator = ' ' try: global debug, flac_options, lame_options, quiet, verbose debug = options.debug @@ -245,7 +199,7 @@ def main(argv): args = get_decode_args(fn) tags = get_tags(fn) - album = tags.gets('ALBUM') + album = tags.gets('ALBUM', separator=separator) discnum = tags.gets('DISCNUMBER') track = tags.gets('TRACKNUMBER') @@ -262,16 +216,18 @@ def main(argv): else: track = int(track) + pics = flac.get_pictures(fn) + for i in range(len(tags)): - title = tags.gets('TITLE', track) + title = tags.gets('TITLE', track, separator) part = tags.gets('PART', track) if part != None: title = '%s - %s' % (title, part) jobs.append([fn, title, - tags.gets('ARTIST', track), + tags.gets('ARTIST', track, separator=', '), album, tags.gets('DATE', track), - track, args[i], find_pic(fn, tags)]) + track, args[i], pics]) track = i + 2 except Exception, error: sys.stderr.write(getattr(error, 'msg', ''))