From: epg <> Date: Thu, 19 Oct 2006 07:13:18 +0000 (+0000) Subject: (flac2mp3): Don't need to int(track) now that the loop in main does it. X-Git-Url: https://diplodocus.org/git/flac-archive/commitdiff_plain/6428ad817c863f36c00b24a8052cfc7343674d10?ds=inline;hp=05ccb9e6302c84c623781de8345dfd3d607ff9c8 (flac2mp3): Don't need to int(track) now that the loop in main does it. (Tags): New class that should be moved somewhere shared so fa-flacd and others can use it. Simplifies keeping track of per-disc and per-track tags. (get_tags): Build and return a Tags object instead of mucking about with different tags in inconsistent (and broken!) ways. (main): Oy, main needs to be split up. Change the main loop to use the Tags object, fix discnum-is-a-str-not-int bug, make track always an int here (previously was sometimes str), correctly handle multiple tracks per flac, and append PART to TITLE if present. --- diff --git a/flac2mp3 b/flac2mp3 index 6e76188..9b23519 100755 --- a/flac2mp3 +++ b/flac2mp3 @@ -63,7 +63,6 @@ from org.diplodocus.util import run_or_die def flac2mp3(fn, title, artist, album, date, track, skip_until): (title, artist, album, date) = [(x == None and 'unknown') or x for x in (title, artist, album, date)] - track = int(track) # XXX caller should int this try: (skip_arg, until_arg) = skip_until except ValueError: @@ -141,43 +140,58 @@ 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.''' - date = discnum = track = None - artists = [] - titles = [] + tags = Tags() p = Popen(['metaflac', '--export-vc-to=-', fn], stdout=PIPE) for line in (x.rstrip() for x in p.stdout): (tag, value) = line.split('=', 1) - if re.match(r'ARTIST=', line, re.IGNORECASE): - artist = value - elif re.match(r'ALBUM=', line, re.IGNORECASE): - album = value - elif re.match(r'DATE=', line, re.IGNORECASE): - date = value - elif re.match(r'DISCNUMBER=', line, re.IGNORECASE): - discnum = value - elif re.match(r'TRACKNUMBER=', line, re.IGNORECASE): - track = value - - # XXX Need to support per-track of everything but album and - # discnum, not just TITLE... Also unify with fa-flacd get_tags. - elif re.match(r'ARTIST\[', line, re.IGNORECASE): - artists.append(value) - elif re.match(r'TITLE', line, re.IGNORECASE): - titles.append(value) + m = re.search(r'\[([0-9]+)]$', tag) + if m != None: + tag = tag[:m.start()] + track = int(m.group(1)) + else: + track = None + + tags.set(tag, value, track) # XXX dataloss! check status status = p.wait() - # If no TITLEs, stick a dummy in here. - if len(titles) == 0: - titles.append(None) - - return (artist, album, date, discnum, track, artists, titles) + return tags def main(argv): # Control the exit code for any uncaught exceptions. @@ -213,12 +227,15 @@ def main(argv): for fn in args: try: args = get_decode_args(fn) - (artist, album, date, discnum, track, - artists, titles) = get_tags(fn) + + tags = get_tags(fn) + album = tags.gets('ALBUM') + discnum = tags.gets('DISCNUMBER') + track = tags.gets('TRACKNUMBER') # lame doesn't seem to support disc number. if discnum != None: - album = '%s (disc %d)' % (album, discnum) + album = '%s (disc %s)' % (album, discnum) # Stupid hack: only a single-track file should have the # TRACKNUMBER tag, so use it if set for the first pass through @@ -226,12 +243,19 @@ def main(argv): # next run, so this continues to work for multi-track files. if track == None: track = 1 - - for i in range(len(titles)): - if len(artists) > 0: - artist = artists[i] - jobs.append([fn, titles[i], artist, album, - date, track, args[i]]) + else: + track = int(track) + + for i in range(len(tags)): + title = tags.gets('TITLE', track) + part = tags.gets('PART', track) + if part != None: + title = '%s - %s' % (title, part) + jobs.append([fn, title, + tags.gets('ARTIST', track), + album, + tags.gets('DATE', track), + track, args[i]]) track = i + 2 except Exception, error: sys.stderr.write(getattr(error, 'msg', ''))