From 32e6b252cb614fe2294387198509954146288f62 Mon Sep 17 00:00:00 2001 From: julia <> Date: Fri, 11 May 2007 15:09:34 +0000 Subject: [PATCH] Made Mp3Exporter a generator. The exported file is still written in the cache. --- telemeta/export/__init__.py | 6 +-- telemeta/export/core.py | 2 +- telemeta/export/flac.py | 3 +- telemeta/export/mp3.py | 46 +++++++++++++++++----- telemeta/export/ogg.py | 78 ++++++++++++++++++++++++------------- telemeta/urls.py | 2 - tests/export_test.py | 16 ++++---- 7 files changed, 103 insertions(+), 50 deletions(-) diff --git a/telemeta/export/__init__.py b/telemeta/export/__init__.py index 6ba2ef16..0e05c996 100644 --- a/telemeta/export/__init__.py +++ b/telemeta/export/__init__.py @@ -1,6 +1,6 @@ from telemeta.export.api import * from telemeta.export.core import * -from telemeta.export.ogg import * -from telemeta.export.flac import * -from telemeta.export.wav import * +#from telemeta.export.ogg import * +#from telemeta.export.flac import * +#from telemeta.export.wav import * from telemeta.export.mp3 import * \ No newline at end of file diff --git a/telemeta/export/core.py b/telemeta/export/core.py index 7e176328..0378a7e9 100644 --- a/telemeta/export/core.py +++ b/telemeta/export/core.py @@ -96,7 +96,7 @@ class ExporterCore(Component): xml.dom.ext.PrettyPrint(doc, xml_file) xml_file.close() - def pre_process(self, item_id, source, metadata, ext, + def pre_process(self, item_id, source, metadata, ext, \ cache_dir, options=None): """ Pre processing of the core. Prepare the export path and return it""" diff --git a/telemeta/export/flac.py b/telemeta/export/flac.py index 6806d919..ee6b247e 100644 --- a/telemeta/export/flac.py +++ b/telemeta/export/flac.py @@ -92,6 +92,7 @@ class FlacExporter(ExporterCore): if not options is None: self.options = options + if 'verbose' in self.options and self.options['verbose'] != '0': args = args else: @@ -118,7 +119,7 @@ class FlacExporter(ExporterCore): os.system('flac '+args+' -o "'+self.dest+'" "'+ \ self.source+'" > /dev/null') - # Pre-proccessing (self) + # Post-proccessing (self) self.write_tags() self.post_process(self.item_id, self.source, diff --git a/telemeta/export/mp3.py b/telemeta/export/mp3.py index 1cb58cad..87eda98e 100644 --- a/telemeta/export/mp3.py +++ b/telemeta/export/mp3.py @@ -12,6 +12,7 @@ import os import string +import subprocess from telemeta.export.core import * from telemeta.export.api import IExporter @@ -32,6 +33,7 @@ class Mp3Exporter(ExporterCore): self.dest = '' self.options = {} self.bitrate_default = '192' + self.buffer_size = 0xFFFF self.dub2id3_dict = {'title': 'TIT2', #title2 'creator': 'TCOM', #composer 'creator': 'TPE1', #lead @@ -95,7 +97,7 @@ class Mp3Exporter(ExporterCore): self.options = {} args = '' - if not options is None: + if not options is None: self.options = options if 'verbose' in self.options and self.options['verbose'] != '0': @@ -106,9 +108,8 @@ class Mp3Exporter(ExporterCore): if 'mp3_bitrate' in self.options: args = args+'-b '+self.options['mp3_bitrate'] else: - args = args+'-b '+self.bitrate_default - - #Copyrights, etc.. + args = args+'-b '+self.bitrate_default + #Copyrights, etc.. args = args + ' -c -o ' else: args = args + ' -S -c -o ' @@ -126,11 +127,38 @@ class Mp3Exporter(ExporterCore): self.cache_dir, self.options) + # Initializing + chunk = 0 + file_out = open(self.dest,'w') + + proc = subprocess.Popen( \ + #'sox "'+self.source+'" -w -r 44100 -t wav -c2 - '+ + #'| lame '+args+' --tc "default" -', + 'lame '+args+' --tc "default" "'+self.source+'" -', + 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() + # Encoding - os.system('lame '+args+' --tc "default" "'+self.source+ - '" "'+self.dest+'"') + # os.system('lame '+args+' --tc "default" "'+self.source+ + # '" "'+self.dest+'"') - # Pre-proccessing (self) + # Post-proccessing (self) self.write_tags() self.post_process(self.item_id, self.source, @@ -140,8 +168,8 @@ class Mp3Exporter(ExporterCore): self.options) # Output - return self.dest + # return self.dest except IOError: - return 'ExporterError [3]: source file does not exist.' + yield 'ExporterError [3]: source file does not exist.' diff --git a/telemeta/export/ogg.py b/telemeta/export/ogg.py index 3ebe7c85..509e9d28 100644 --- a/telemeta/export/ogg.py +++ b/telemeta/export/ogg.py @@ -12,6 +12,7 @@ import os import string +import subprocess from telemeta.export.core import * from telemeta.export.api import IExporter @@ -31,7 +32,8 @@ class OggExporter(ExporterCore): self.dest = '' self.options = {} self.bitrate_default = '192' - + self.buffer_size = 0xFFFF + def get_format(self): return 'OGG' @@ -71,7 +73,8 @@ class OggExporter(ExporterCore): for tag in self.metadata.keys(): 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 @@ -97,35 +100,56 @@ class OggExporter(ExporterCore): else: args = ' -Q -b '+self.bitrate_default - if os.path.exists(self.source) and not iswav16(self.source): - self.source = self.decode() - + #if os.path.exists(self.source) and not iswav16(self.source): + # self.source = self.decode() + + # Pre-processing + 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) + try: - # Pre-proccessing (core) - self.ext = self.get_file_extension() - self.dest = self.pre_process(self.item_id, + # Initializing + chunk = 0 + file_out = open(self.dest,'w') + + proc = subprocess.Popen( \ + 'sox "'+self.source+'" -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) + 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() + self.post_process(self.item_id, self.source, self.metadata, self.ext, self.cache_dir, self.options) - - # Encoding - os.system('oggenc '+args+' -o "'+self.dest+ - '" "'+self.source+'"') - - # Pre-proccessing (self) - 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: - return 'ExporterError [3]: source file does not exist.' - + yield 'ExporterError [3]: source file does not exist.' diff --git a/telemeta/urls.py b/telemeta/urls.py index c352c1ef..3779677e 100644 --- a/telemeta/urls.py +++ b/telemeta/urls.py @@ -80,5 +80,3 @@ urlpatterns = patterns('', (r'^images/(?P.*)$', 'django.views.static.serve', {'document_root': './telemeta/htdocs/images'}), ) - - diff --git a/tests/export_test.py b/tests/export_test.py index c0afd45c..cdf7d988 100644 --- a/tests/export_test.py +++ b/tests/export_test.py @@ -17,8 +17,9 @@ from telemeta.core import * from telemeta.core import ComponentManager cache_dir = 'cache/' -source = 'samples/wav/Cellar - Show Me - 02.wav' -item_id = '1' +source = 'samples/wav/The Chicken-James Brown.wav' +#source = 'samples/wav/Cellar - Show Me - 02.wav' +item_id = '10' metadata = {'identifier': 'Test', #collection 'title': 'Show Me', 'creator': 'Cellar', @@ -26,10 +27,10 @@ metadata = {'identifier': 'Test', #collection 'date': '2004', 'publisher': 'PArISs0n', } -options = {'verbose': '0'} - +options = {'verbose': '1'} class ExportTest(Component): + exporters = ExtensionPoint(IExporter) def run(self): @@ -40,10 +41,11 @@ class ExportTest(Component): print '| Testing exporter format: ' + format print "+------------------------------------------" exporter.set_cache_dir(cache_dir) - exporter.process(item_id,source,metadata,options) - #exporter.process(item_id,source,metadata) - + stream = exporter.process(item_id,source,metadata,options) + for chunk in stream: + pass + compmgr = ComponentManager() test = ExportTest(compmgr) test.run() -- 2.39.5