From 27a5b882cc7987c7fae0cd14280906e3d51ce771 Mon Sep 17 00:00:00 2001 From: yomguy <> Date: Sun, 20 May 2007 23:20:38 +0000 Subject: [PATCH] Moved audio generator to ExporterCore.core_process --- telemeta/export/core.py | 32 +++++++++---- telemeta/export/flac.py | 84 +++++++++++++------------------- telemeta/export/mp3.py | 94 ++++++++++++++++-------------------- telemeta/export/ogg.py | 104 ++++++++++++++++++---------------------- telemeta/export/wav.py | 17 +++---- tests/export_test.py | 3 +- 6 files changed, 153 insertions(+), 181 deletions(-) diff --git a/telemeta/export/core.py b/telemeta/export/core.py index fd9c1bdb..39bd8f5e 100644 --- a/telemeta/export/core.py +++ b/telemeta/export/core.py @@ -149,17 +149,31 @@ class ExporterCore(Component): self.dest = os.path.join(self.dest,target_file) return self.dest - def stream(self,command): + def core_process(self,command,buffer_size,dest): """Streams encoded audio data through a generator""" + + __chunk = 0 + file_out = open(dest,'w') + proc = subprocess.Popen(command, - shell=True, - bufsize=self.buffer_size, - stdin=subprocess.PIPE, - stdout=subprocess.PIPE, - close_fds=True) - - chunk = proc.stdout.read(self.buffer_size) - return chunk + shell = True, + bufsize = buffer_size, + stdin = subprocess.PIPE, + stdout = subprocess.PIPE, + close_fds = True) + + __chunk = proc.stdout.read(buffer_size) + yield __chunk + file_out.write(__chunk) + + # Processing + while __chunk: + __chunk = proc.stdout.read(buffer_size) + yield __chunk + file_out.write(__chunk) + + #file_in.close() + file_out.close() def post_process(self, item_id, source, metadata, ext, cache_dir, options=None): diff --git a/telemeta/export/flac.py b/telemeta/export/flac.py index 212b61e4..6c1d9fc4 100644 --- a/telemeta/export/flac.py +++ b/telemeta/export/flac.py @@ -85,80 +85,64 @@ class FlacExporter(ExporterCore): media[tag] = str(self.metadata[tag]) media.save() - def process(self, item_id, source, metadata, options=None): - self.item_id = item_id - self.source = source - self.metadata = metadata - self.options = {} + def get_args(self,options=None): + """Get process options and return arguments for the encoder""" args = '' - if not options is None: self.options = options - - if 'verbose' in self.options and self.options['verbose'] != '0': - args = args - else: + if not ('verbose' in self.options and self.options['verbose'] != '0'): args = args + ' -s ' - + if 'flac_quality' in self.options: args = args+' -f -'+self.options['flac_quality'] else: args = args+' -f -'+self.quality_default else: args = args+' -s -f -'+self.quality_default - + return args + + def process(self, item_id, source, metadata, options=None): + self.item_id = item_id + self.source = source + self.metadata = metadata + #self.options = {} + self.args = self.get_args(options) + self.ext = self.get_file_extension() + self.command = 'sox "'+self.source+'" -q -w -r 44100 -t wav -c2 - '+ \ + '| flac '+self.args+' -c -' + + # Pre-proccessing try: - # Pre-proccessing (core) - self.ext = self.get_file_extension() self.dest = self.pre_process(self.item_id, self.source, self.metadata, self.ext, self.cache_dir, self.options) - - # Initializing - chunk = 0 - file_out = open(self.dest,'w') - - proc = subprocess.Popen( \ - 'sox "'+self.source+'" -q -w -r 44100 -t wav -c2 - '+ - '| flac '+args+' -c -', - shell=True, - bufsize=self.buffer_size, - stdin=subprocess.PIPE, - stdout=subprocess.PIPE, - close_fds=True) - - chunk = proc.stdout.read(self.buffer_size) - yield chunk - file_out.write(chunk) - - # Processing - while chunk: - chunk = proc.stdout.read(self.buffer_size) - yield chunk - file_out.write(chunk) - - #file_in.close() - file_out.close() + except: + yield 'ExporterError [3]: pre_process' - # Encoding - #os.system('flac '+args+' -o "'+self.dest+'" "'+ \ - # self.source+'" > /dev/null') + # Processing (streaming + cache writing) + try: + stream = self.core_process(self.command,self.buffer_size,self.dest) + for chunk in stream: + yield chunk + except: + yield 'ExporterError: core_process' - # Post-proccessing (self) - self.write_tags() + # Post-proccessing + try: + self.write_tags() self.post_process(self.item_id, self.source, self.metadata, self.ext, self.cache_dir, self.options) + except: + yield 'ExporterError: post_process' - # Output - #return self.dest - - except IOError: - yield 'ExporterError [3]: source file does not exist.' + # Encoding + #os.system('flac '+args+' -o "'+self.dest+'" "'+ \ + # self.source+'" > /dev/null') diff --git a/telemeta/export/mp3.py b/telemeta/export/mp3.py index 42155f38..a764e92e 100644 --- a/telemeta/export/mp3.py +++ b/telemeta/export/mp3.py @@ -90,13 +90,9 @@ class Mp3Exporter(ExporterCore): id3.add(frame) id3.save() - def process(self, item_id, source, metadata, options=None): - self.item_id = item_id - self.source = source - self.metadata = metadata - self.options = {} + def get_args(self,options=None): + """Get process options and return arguments for the encoder""" args = '' - if not options is None: self.options = options @@ -113,61 +109,51 @@ class Mp3Exporter(ExporterCore): args = args + ' -c -o ' else: args = args + ' -S -c -o ' - - if os.path.exists(self.source) and not iswav16(self.source): - self.source = self.decode() + + return args + + def process(self, item_id, source, metadata, options=None): + self.item_id = item_id + self.source = source + self.metadata = metadata + #self.options = {} + self.args = self.get_args(options) + self.ext = self.get_file_extension() + self.command = 'sox "'+self.source+'" -q -w -r 44100 -t wav -c2 - '+ \ + '| lame '+self.args+' --tc "default" - -' + # Pre-proccessing try: - # Pre-proccessing (core) - self.ext = self.get_file_extension() self.dest = self.pre_process(self.item_id, - self.source, - self.metadata, - self.ext, - self.cache_dir, - self.options) - - # Initializing - chunk = 0 - file_out = open(self.dest,'w') - - proc = subprocess.Popen( \ - 'sox "'+self.source+'" -q -w -r 44100 -t wav -c2 - '+ - '| lame '+args+' --tc "default" - -', - shell=True, - bufsize=self.buffer_size, - stdin=subprocess.PIPE, - stdout=subprocess.PIPE, - close_fds=True) - - chunk = proc.stdout.read(self.buffer_size) - yield chunk - file_out.write(chunk) - - # Processing - while chunk: - chunk = proc.stdout.read(self.buffer_size) + self.source, + self.metadata, + self.ext, + self.cache_dir, + self.options) + except: + yield 'ExporterError [3]: pre_process' + + # Processing (streaming + cache writing) + try: + stream = self.core_process(self.command,self.buffer_size,self.dest) + for chunk in stream: yield chunk - file_out.write(chunk) - - file_out.close() - - # Encoding - # os.system('lame '+args+' --tc "default" "'+self.source+ - # '" "'+self.dest+'"') - - # Post-proccessing (self) - self.write_tags() + except: + yield 'ExporterError: core_process' + + # Post-proccessing + try: + self.write_tags() self.post_process(self.item_id, self.source, self.metadata, self.ext, self.cache_dir, self.options) - - # Output - # return self.dest - - except IOError: - yield 'ExporterError [3]: source file does not exist.' - + except: + yield 'ExporterError: post_process' + + # Encoding + # os.system('lame '+args+' --tc "default" "'+self.source+ + # '" "'+self.dest+'"') + diff --git a/telemeta/export/ogg.py b/telemeta/export/ogg.py index e4c40220..6c4a2633 100644 --- a/telemeta/export/ogg.py +++ b/telemeta/export/ogg.py @@ -74,14 +74,9 @@ class OggExporter(ExporterCore): media[tag] = str(self.metadata[tag]) media.save() - - def process(self, item_id, source, metadata, options=None): - self.item_id = item_id - self.source = source - self.metadata = metadata - self.options = {} + def get_args(self,options=None): + """Get process options and return arguments for the encoder""" args = '' - if not options is None: self.options = options @@ -96,60 +91,55 @@ class OggExporter(ExporterCore): args = args + '-q '+self.options['ogg_quality'] else: args = args + '-b '+self.bitrate_default - else: args = ' -Q -b '+self.bitrate_default + return args - #if os.path.exists(self.source) and not iswav16(self.source): - # self.source = self.decode() - - # Pre-processing + + def process(self, item_id, source, metadata, options=None): + self.item_id = item_id + self.source = source + self.metadata = metadata + #self.options = {} + self.args = self.get_args(options) self.ext = self.get_file_extension() - self.dest = self.pre_process(self.item_id, - self.source, - self.metadata, - self.ext, - self.cache_dir, - self.options) - + self.command = 'sox "'+self.source+'" -q -w -r 44100 -t wav -c2 - '+ \ + '| oggenc '+self.args+' -' + + # Pre-proccessing try: - # Initializing - chunk = 0 - file_out = open(self.dest,'w') - - proc = subprocess.Popen( \ - 'sox "'+self.source+'" -q -w -r 44100 -t wav -c2 - '+ - '| oggenc '+args+' -', - shell=True, - bufsize=self.buffer_size, - stdin=subprocess.PIPE, - stdout=subprocess.PIPE, - close_fds=True) - - chunk = proc.stdout.read(self.buffer_size) - yield chunk - file_out.write(chunk) - - # Processing - while chunk: - chunk = proc.stdout.read(self.buffer_size) + self.dest = self.pre_process(self.item_id, + self.source, + self.metadata, + self.ext, + self.cache_dir, + self.options) + except: + yield 'ExporterError [3]: pre_process' + + # Processing (streaming + cache writing) + try: + stream = self.core_process(self.command,self.buffer_size,self.dest) + for chunk in stream: yield chunk - file_out.write(chunk) - - #file_in.close() - file_out.close() - - # Post-proccessing - #os.system('sox "'+self.source+'" -w -r 44100 -t wav -c2 - \ - # | oggenc '+args+' -o "'+self.dest+'" -') - - self.write_tags() + except: + yield 'ExporterError: core_process' + + # Post-proccessing + try: + self.write_tags() self.post_process(self.item_id, - self.source, - self.metadata, - self.ext, - self.cache_dir, - self.options) - - except IOError: - yield 'ExporterError [3]: source file does not exist.' + self.source, + self.metadata, + self.ext, + self.cache_dir, + self.options) + except: + yield 'ExporterError: post_process' + + + + # Post-proccessing + #os.system('sox "'+self.source+'" -w -r 44100 -t wav -c2 - \ + # | oggenc '+args+' -o "'+self.dest+'" -') + diff --git a/telemeta/export/wav.py b/telemeta/export/wav.py index 96e68f03..06c8f8a6 100644 --- a/telemeta/export/wav.py +++ b/telemeta/export/wav.py @@ -120,7 +120,7 @@ class WavExporter(ExporterCore): chunk = 0 file_in = open(self.source,'rb') file_out = open(self.dest,'w') - + chunk = file_in.read(self.buffer_size) yield chunk file_out.write(chunk) @@ -133,12 +133,6 @@ class WavExporter(ExporterCore): file_in.close() file_out.close() - - #if self.compare_md5_key(): - #os.system('cp -a "'+self.source+'" "'+ self.dest+'"') - #print 'COPIED' - - # Pre-proccessing (self) self.write_tags() # Create the md5 key @@ -156,9 +150,12 @@ class WavExporter(ExporterCore): self.cache_dir, self.options) - # Output - #return self.dest - except IOError: yield 'ExporterError [3]: source file does not exist.' + + + #if self.compare_md5_key(): + #os.system('cp -a "'+self.source+'" "'+ self.dest+'"') + #print 'COPIED' + diff --git a/tests/export_test.py b/tests/export_test.py index d6063d62..01d35a8c 100644 --- a/tests/export_test.py +++ b/tests/export_test.py @@ -27,7 +27,7 @@ metadata = {'identifier': 'Test', #collection 'date': '2004', 'publisher': 'Parisson', } -options = {'verbose': '0'} +options = {'verbose': '1'} class ExportTest(Component): @@ -45,6 +45,7 @@ class ExportTest(Component): for chunk in stream: pass + #print chunk compmgr = ComponentManager() test = ExportTest(compmgr) -- 2.39.5