X-Git-Url: https://diplodocus.org/git/flac-archive/blobdiff_plain/578decd98560dc6a1ddb55da9b432bf776585a64..8690768dfd3ceece4f3d5f505d1716f645e030fc:/flac2mp3 diff --git a/flac2mp3 b/flac2mp3 index 6e76188..1884f61 100755 --- a/flac2mp3 +++ b/flac2mp3 @@ -1,6 +1,6 @@ -#! /usr/bin/env python2.4 +#!/usr/bin/python -''' +""" =head1 NAME B - transcode FLAC file to MP3 files @@ -16,6 +16,10 @@ may be the kind of FLAC file B generates. That is, it contains a cue sheet, one TITLE tag per track listed therein, and ARTIST, ALBUM, and DATE tags. +Note that lame is retarded, and parses B directly itself! So, in order +for it to transcode textual tags, you must specify the encoding in LANG, e.g. +LANG=en_US.utf-8 + =head1 OPTIONS =over 4 @@ -48,26 +52,27 @@ Written by Eric Gillespie . =cut -''' #' # python-mode is sucks +""" -import re, sys, traceback +import os, re, sys, tempfile, traceback from optparse import OptionParser from subprocess import Popen, PIPE import org.diplodocus.jobs from org.diplodocus.util import run_or_die +from flac_archive import flac +from flac_archive.tags import Tags + ################################################################################ # The child processes -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: - skip_arg = until_arg = '' +def flac2mp3(fn, title, artist, album_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,17 +89,39 @@ def flac2mp3(fn, title, artist, album, date, track, skip_until): verbose and tmp.append('--verbose') lame_options = ' '.join(tmp) - # 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('/', '_') - run_or_die(3, "flac %s -cd %s %s '%s' | lame %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)) + # Escape any single quotes ' so we can quote this. + (fn, title, artist, album_artist, + album, date) = [(x or '').replace("'", r"'\''") + for x in [fn, title, artist, album_artist, album, date]] + + album_artist_options = '' + if album_artist: + album_artist_options = "--tv 'TPE2=%s'" % album_artist + + quoted_outfile = ('%s (%s) %02d %s.mp3' % (artist, album, + track, title)).replace('/', '_') + + pic_options = None + if pics: + (fd, picfn) = tempfile.mkstemp() + f = os.fdopen(fd, 'wb') + f.write(pics[0][7]) + f.close() + pic_options = "--ti '%s'" % picfn + try: + run_or_die(3, "flac %s -cd %s '%s' | lame --id3v2-only --id3v2-latin1 --pad-id3v2-size 0 %s --tt '%s' --ta '%s' --tl '%s' --ty '%s' --tn %d %s %s - '%s'" + % (flac_options, ' '.join(skip_until), fn, + lame_options, title, artist, album, date, track, + pic_options, album_artist_options, quoted_outfile)) + finally: + if pic_options: + try: + os.unlink(picfn) + except: + pass return 0 @@ -142,42 +169,18 @@ def get_decode_args(fn): return args 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.""" - date = discnum = track = None - artists = [] - titles = [] + tags = Tags() + + p = Popen(['metaflac', '--export-tags-to=-', fn], stdout=PIPE) + tags.load(p.stdout) - 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) # 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. @@ -202,6 +205,7 @@ def main(argv): traceback.print_exc() return 2 + separator = ' ' try: global debug, flac_options, lame_options, quiet, verbose debug = options.debug @@ -213,12 +217,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', separator=separator) + 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 +233,24 @@ 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) + + pics = flac.get_pictures(fn) + + for i in range(len(tags)): + title = tags.gets('TITLE', track, separator) + part = tags.gets('PART', track) + if part != None: + title = '%s - %s' % (title, part) + artist = tags.get('ARTIST', track) + artist.extend(tags.get('FEATURING', track)) + album_artist = tags.gets('ALBUMARTIST', track) + jobs.append([fn, title, + ', '.join(artist), + album_artist, album, + tags.gets('DATE', track), + track, args[i], pics]) track = i + 2 except Exception, error: sys.stderr.write(getattr(error, 'msg', ''))