From 1311944669ece35060d50786fab0a8aaba8a5122 Mon Sep 17 00:00:00 2001 From: Mathias Koehler Date: Thu, 4 Sep 2014 18:37:48 +0200 Subject: [PATCH] Use bytes in output queue --- ffmpegwrapper/ffmpeg.py | 37 ++++++++++++++++++++++++++----------- test.py | 6 +++--- 2 files changed, 29 insertions(+), 14 deletions(-) diff --git a/ffmpegwrapper/ffmpeg.py b/ffmpegwrapper/ffmpeg.py index 89d3cc5..f9d8120 100644 --- a/ffmpegwrapper/ffmpeg.py +++ b/ffmpegwrapper/ffmpeg.py @@ -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) diff --git a/test.py b/test.py index 258a76a..a4e1240 100644 --- a/test.py +++ b/test.py @@ -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