From: W. Trevor King Date: Thu, 12 Jul 2012 03:40:44 +0000 (-0400) Subject: mkogg.py: don't require explicit methods for conversion via wav. X-Git-Url: http://git.tremily.us/?a=commitdiff_plain;h=f4fae39702b053bb5db1ae1a8fb7ae9a259df554;p=blog.git mkogg.py: don't require explicit methods for conversion via wav. Only require explit conversion methords for * -> wav and wav -> *. With this and metadata getters/setters, conversion will work fine, and we the number of methods will scale better with an increasing number of file types. You should still write a direct conversion method if you can avoid the wav intermediate. --- diff --git a/posts/mkogg/mkogg.py b/posts/mkogg/mkogg.py index 7690de6..174e712 100755 --- a/posts/mkogg/mkogg.py +++ b/posts/mkogg/mkogg.py @@ -1,6 +1,6 @@ #!/usr/bin/env python # -# Copyright (C) 2009-2011 W. Trevor King +# Copyright (C) 2009-2012 W. Trevor King # # This program is free software: you can redistribute it and/or modify # it under the terms of the GNU Lesser General Public License as @@ -189,8 +189,16 @@ class Converter (object): if ext == self._target_extension: shutil.copy(source, target) return - convert = getattr(self, 'convert_{}_to_{}'.format( - ext, self._target_extension)) + try: + convert = getattr(self, 'convert_{}_to_{}'.format( + ext, self._target_extension)) + except AttributeError: + to_wav = getattr(self, 'convert_{}_to_wav'.format(ext)) + from_wav = getattr(self, 'convert_wav_to_{}'.format( + self._target_extension)) + def convert(source, target): + to_wav(source, self._tempfile) + from_wav(self._tempfile, target) convert(source, target) if not getattr(convert, 'handles_metadata', False): get_metadata = getattr(self, 'get_{}_metadata'.format(ext)) @@ -289,10 +297,6 @@ class Converter (object): return id3_encoding raise ValueError(text_list) - def convert_flac_to_mp3(self, source, target): - self.convert_flac_to_wav(source, self._tempfile) - self.convert_wav_to_mp3(self._tempfile, target) - def convert_flac_to_wav(self, source, target): invoke(['ogg123', '-d', 'wav', '-f', target, source]) @@ -300,24 +304,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) def convert_mp3_to_wav(self, source, target): invoke(['mpg123', '-w', target, source]) - def convert_ogg_to_flac(self, source, target): - self.convert_ogg_to_wav(source, self._tempfile) - self.convert_wav_to_flac(self._tempfile, target) - - def convert_ogg_to_mp3(self, source, target): - self.convert_flac_to_mp3(source, target) - def convert_ogg_to_wav(self, source, target): self.convert_flac_to_wav(source, target)