From 74ad26edc2cf0d8aa8d5d485d708de1a34aa75c0 Mon Sep 17 00:00:00 2001 From: Jon Bergli Heier Date: Sun, 14 Aug 2011 21:59:35 +0200 Subject: Some recoding work, added ogg encoder. --- app.py | 6 ++++-- config.py | 22 ++++++++++++++-------- directory.py | 2 +- recode.py | 36 +++++++++++++++++++----------------- 4 files changed, 38 insertions(+), 28 deletions(-) diff --git a/app.py b/app.py index e70283c..c2ef0a3 100755 --- a/app.py +++ b/app.py @@ -65,7 +65,8 @@ class Application(object): f = File(path) # see json_play() if not os.path.splitext(path)[1] in ('.mp3', '.ogg'): - decoder, encoder = ('ffmpeg',)*2 + decoder = 'ffmpeg' + encoder = config.get('encoder') f.start_recode(decoder, encoder) start_response('200 OK', [('Content-Type', 'text/plain')]) @@ -80,7 +81,8 @@ class Application(object): # TODO: replace this with some sane logic if not os.path.splitext(path)[1] in ('.mp3', '.ogg'): cache_path = f.get_cache_path() - decoder, encoder = ('ffmpeg',)*2 + decoder = 'ffmpeg' + encoder = config.get('encoder') if not os.path.exists(cache_path): f.start_recode(decoder, encoder, environ['sessionid']) else: diff --git a/config.py b/config.py index 88826cb..7fd35ad 100644 --- a/config.py +++ b/config.py @@ -1,19 +1,25 @@ try: - from configparser import ConfigParser + from configparser import ConfigParser, NoOptionError except ImportError: - from ConfigParser import ConfigParser + from ConfigParser import ConfigParser, NoOptionError class Config(object): - def __init__(self, filename = 'config'): - self.config_section = 'foo' + config_section = 'foo' + def __init__(self, filename = 'config'): self.config = ConfigParser() self.config.read(filename) - def get(self, key): - return self.config.get(self.config_section, key) + def get(self, key, section = config_section, default = None): + try: + return self.config.get(section, key) + except NoOptionError: + if default != None: + return default + else: + raise - def getint(self, key): - return self.config.getint(self.config_section, key) + def getint(self, key, section = config_section): + return self.config.getint(section, key) config = Config() diff --git a/directory.py b/directory.py index 5a0c318..6860155 100644 --- a/directory.py +++ b/directory.py @@ -86,7 +86,7 @@ class File(DirectoryEntry): def get_cache_path(self): cache_path = os.path.join(config.get('cache_dir'), self.path) - cache_path = os.path.splitext(cache_path)[0] + '.mp3' + cache_path = os.path.splitext(cache_path)[0] + '.ogg' return cache_path def get_cache_file(self): diff --git a/recode.py b/recode.py index 6caa2a9..8b3ebb0 100644 --- a/recode.py +++ b/recode.py @@ -1,4 +1,5 @@ -import subprocess, tempfile, threading +import subprocess, tempfile, threading, os +from config import config decoders = {} encoders = {} @@ -29,21 +30,22 @@ class Codec(object): # end of metastuff -class FFmpeg(Codec): +class FFmpeg(Decoder): decoder_name = 'ffmpeg' - encoder_name = 'ffmpeg' def decode(self, source, dest, *args): - ret = subprocess.call((x.format(infile = source, outfile = dest) for x in 'ffmpeg -loglevel quiet -i {infile} -y {outfile}'.split())) - print 'decoding returned', ret + cmd = (x.format(infile = source, outfile = dest) for x in 'ffmpeg -loglevel quiet -i {infile} -y {outfile}'.split()) + p = subprocess.Popen(cmd, stderr = subprocess.PIPE, close_fds = True) + p.stderr.close() + p.wait() - def encode(self, source, dest, *args): - ret = subprocess.call((x.format(infile = source, outfile = dest) for x in 'ffmpeg -loglevel quiet -i {infile} -y {outfile}'.split())) - print 'encoding returned', ret +class Ogg(Encoder): + encoder_name = 'ogg' - def recode(self, source, dest, *args): - ret = subprocess.call((x.format(infile = source, outfile = dest) for x in 'ffmpeg -loglevel quiet -i {infile} -y {outfile}'.split())) - print 'recoding returned', ret + def encode(self, source, dest, *args): + options = config.get('options', 'encoder/ogg', '') + cmd = ['oggenc', '-Q'] + options.split() + [source, '-o', dest] + subprocess.call(cmd) class Recoder(object): def __init__(self, decoder, encoder): @@ -56,12 +58,12 @@ class Recoder(object): print self.decoder self.decoder.recode(source, dest) else: - temp = tempfile.NamedTemporaryFile(mode = 'wb', prefix = 'foo', suffix = os.path.splitext(dest)[1], delete = True) - print 'decoding' - self.decoder.decode(source, temp.name) - print 'encoding' - self.encoder.encode(temp.name, dest) - temp.close() + with tempfile.NamedTemporaryFile(mode = 'wb', prefix = 'foo', suffix = '.wav', delete = True) as temp: + print 'temp file:', temp.name + print 'decoding' + self.decoder.decode(source, temp.name) + print 'encoding' + self.encoder.encode(temp.name, dest) class RecodeThread(threading.Thread): lock = threading.Lock() -- cgit v1.2.3