From: W. Trevor King Date: Sat, 12 Mar 2011 22:40:23 +0000 (-0500) Subject: Add mp3->flac conversion to mkogg.py and flesh out id3 tag parsing. X-Git-Url: http://git.tremily.us/?a=commitdiff_plain;h=c44c879deba4282079f5889e815d90402058fc16;p=blog.git Add mp3->flac conversion to mkogg.py and flesh out id3 tag parsing. --- diff --git a/posts/mkogg/mkogg.py b/posts/mkogg/mkogg.py index 22d7720..1de0691 100755 --- a/posts/mkogg/mkogg.py +++ b/posts/mkogg/mkogg.py @@ -22,8 +22,10 @@ Other target formats are also supported. Current conversions: * flac -> ogg * flac -> wav -> mp3 +* ogg -> wav -> flac * ogg -> wav -> mp3 -* mp3 -> ogg +* mp3 -> wav -> flac +* mp3 -> wav -> ogg External packages required for full functionality: @@ -246,14 +248,22 @@ class Converter (object): """ metadata = {} vorbis_keys = { + 'comm': 'comment', 'talb': 'album', - 'tpe1': 'artist', + 'tcom': 'composer', + 'tcon': 'genre', + 'tcop': 'copyright', 'tit2': 'title', + 'tpe1': 'artist', + 'tpe2': 'band', + 'tpe3': 'conductor', + 'tpos': 'part of set', 'trck': 'tracknumber', 'tyer': 'date', - 'tcon': 'genre', - 'comm': 'comment', } + drop_keys = [ + 'apic' # attached picture + ] in_v2 = False for line in stdout.splitlines(): if not in_v2: @@ -262,6 +272,8 @@ class Converter (object): continue key,value = [x.strip() for x in line.split(':', 1)] short_key = key.split()[0] + if short_key.lower() in drop_keys: + continue v_key = vorbis_keys[short_key.lower()] if v_key == 'genre': value = value.rsplit('(', 1)[0].strip() @@ -315,6 +327,10 @@ class Converter (object): invoke(['oggenc', '--quiet', '-q', '3', source, '-o', target]) convert_flac_to_ogg.handles_metadata = True + def convert_mp3_to_flac(self, source, target): + self.convert_mp3_to_wav(source, self._tempfile) + self.convert_wav_to_flac(self._tempfile, target) + def convert_mp3_to_ogg(self, source, target): self.convert_mp3_to_wav(source, self._tempfile) self.convert_wav_to_ogg(self._tempfile, target) @@ -328,6 +344,9 @@ class Converter (object): def convert_ogg_to_wav(self, source, target): self.convert_flac_to_wav(source_target) + def convert_wav_to_flac(self, source, target): + invoke(['flac', '-o', target, source]) + def convert_wav_to_mp3(self, source, target): invoke(['lame', '--quiet', '-V', '4', source, target]) @@ -358,6 +377,11 @@ class Converter (object): ['vorbiscomment', '--list', source]) return self._parse_vorbis_comments(stdout) + def set_flac_metadata(self, target, metadata): + stdin = '\n'.join(['%s=%s' % (k.upper(), v) + for k,v in sorted(metadata.iteritems())]) + invoke(['metaflac', '--import-tags-from=-', target], stdin=stdin) + def set_mp3_metadata(self, target, metadata): args = ['id3v2'] for key,arg in [('album', '--album'), ('artist', '--artist'), @@ -389,9 +413,8 @@ class Converter (object): def set_ogg_metadata(self, target, metadata): stdin = '\n'.join(['%s=%s' % (k.upper(), v) - for k,v in metadata.iteritems()]) + for k,v in sorted(metadata.iteritems())]) invoke(['vorbiscomment', '--write', target], stdin=stdin) - return self._parse_vorbis_comments(stdin) def test():