Use bytes in output queue

This commit is contained in:
Mathias Koehler 2014-09-04 18:37:48 +02:00
parent 5ad21350cc
commit 1311944669
2 changed files with 29 additions and 14 deletions

View file

@ -5,7 +5,7 @@
Your entrypoint for every Task you want to do with FFmpeg
:copyright: (c) 2013 by Mathias Koehler.
:copyright: (c) 2014 by Mathias Koehler.
:license: BSD, see LICENSE for more details.
"""
@ -72,18 +72,18 @@ class FFmpegProcess(object):
def _queue_output(self, out, queue):
"""Read the output from the command bytewise. On every newline
the line is put to the queue."""
line = ''
running = self.process.poll() is None
line = bytearray()
running = self.running
while running:
chunk = out.read(1).decode('utf-8')
if chunk == '':
running = self.process.poll() is None
byte = out.read(1)
if byte == b'':
running = self.running
continue
line += chunk
if chunk in ('\n', '\r'):
queue.put(line, timeout=0.4)
line = ''
line += byte
if byte in (b'\n', b'\r'):
queue.put(''.join(line.decode('utf8')), timeout=0.4)
line = bytearray()
out.close()
def run(self, daemon=True):
@ -101,6 +101,18 @@ class FFmpegProcess(object):
thread.start()
return self
@property
def running(self):
return self.process.poll() is None
@property
def successful(self):
return self.process.returncode == 0
@property
def failed(self):
return not (self.successful or self.running)
def readlines(self, keepends=False):
"""Yield lines from the queue that were collected from the
command. You can specify if you want to keep newlines at the ends.
@ -140,7 +152,7 @@ class FFmpeg(ParameterContainer):
:param args: A list of Containers that should be appended
"""
def __init__(self, binary='ffmaeg', *args):
def __init__(self, binary='ffmpeg', *args):
self.binary = binary
self.process = None
ParameterContainer.__init__(self, *args)
@ -168,3 +180,6 @@ class FFmpeg(ParameterContainer):
def __iter__(self):
return chain([self.binary], ParameterContainer.__iter__(self))
def __str__(self):
return" ".join(self)

View file

@ -16,14 +16,14 @@ class FFmpegTestCase(unittest.TestCase):
popen = self.patcher.start()
self.instance = popen.return_value
read_value = list('this is a line\nthis too\n')
read_value = bytearray(b'this is a line\nthis too\n')
poll = lambda: None if read_value else 0
def read(*args):
try:
return read_value.pop(0).encode('utf-8')
return bytearray([read_value.pop(0)])
except IndexError:
return ''.encode('utf-8')
return b''
self.instance.poll.side_effect = poll
self.instance.stdout.read.side_effect = read