Changed to CommandlineCaller, added test
Now using CommandlineCaller using a custom logger function. Added test in test_timelapse_helpers.py for _process_ffmpeg_output
This commit is contained in:
1 parent
2fa18f5855
commit
b7adf115ee
2 files changed
+72
-33
No files matched your search
+29
-33
@@ -24,6 +24,7 @@ import octoprint.util as util
|
||||
from octoprint.settings import settings
|
||||
from octoprint.events import eventManager, Events
|
||||
from octoprint.util import monotonic_time
|
||||
from octoprint.util.commandline import CommandlineCaller
|
||||
|
||||
import sarge
|
||||
import collections
|
||||
@@ -785,6 +786,9 @@ class TimelapseRenderJob(object):
|
||||
|
||||
self._thread = None
|
||||
self._logger = logging.getLogger(__name__)
|
||||
self._duration_regex = re.compile("Duration: (\d{2}):(\d{2}):(\d{2})\.\d{2}")
|
||||
self._current_regex = re.compile("time=(\d{2}):(\d{2}):(\d{2})\.\d{2}")
|
||||
self._duration_i = 0
|
||||
|
||||
def process(self):
|
||||
"""Processes the job."""
|
||||
@@ -839,46 +843,18 @@ class TimelapseRenderJob(object):
|
||||
with self.render_job_lock:
|
||||
try:
|
||||
self._notify_callback("start", output)
|
||||
p = sarge.run(command_str, stdout=sarge.Capture(), stderr=sarge.Capture(), async=True)
|
||||
|
||||
self._logger.debug("Parsing ffmpeg output")
|
||||
|
||||
# Compile regex to make it faster
|
||||
duration_regex = re.compile("Duration: (\d{2}):(\d{2}):(\d{2})\.\d{2}")
|
||||
current_regex = re.compile("time=(\d{2}):(\d{2}):(\d{2})\.\d{2}")
|
||||
duration_s = ""
|
||||
|
||||
# Sarge sometimes doesn't populate commands fast enough, so we check if the command exists first, then poll the thread
|
||||
while len(p.commands) == 0 or p.commands[0].poll() is None:
|
||||
line = p.stderr.readline()
|
||||
|
||||
# We should be checking time more often, so try it first
|
||||
time = current_regex.search(line)
|
||||
if time is not None:
|
||||
current_s = self._parsetime(*time.groups())
|
||||
|
||||
progress = current_s / float(duration_s) * 100
|
||||
|
||||
# Update progress bar
|
||||
for callback in _update_callbacks:
|
||||
try:
|
||||
callback.sendRenderProgress(progress)
|
||||
except:
|
||||
self._logger.exception("Exception while pushing render progress")
|
||||
|
||||
else:
|
||||
duration = duration_regex.search(line)
|
||||
if duration is not None:
|
||||
duration_s = self._parsetime(*duration.groups())
|
||||
c = CommandlineCaller()
|
||||
c.on_log_stderr = self._process_ffmpeg_output
|
||||
returncode, stdout_text, stderr_text = c.call(command_str)
|
||||
|
||||
self._logger.debug("Done with parsing")
|
||||
|
||||
if p.returncode == 0:
|
||||
if returncode == 0:
|
||||
self._notify_callback("success", output)
|
||||
else:
|
||||
returncode = p.returncode
|
||||
stdout_text = p.stdout.text
|
||||
stderr_text = p.stderr.text
|
||||
self._logger.warn("Could not render movie, got return code %r: %s" % (returncode, stderr_text))
|
||||
self._notify_callback("fail", output, returncode=returncode, stdout=stdout_text, stderr=stderr_text, reason="returncode")
|
||||
except:
|
||||
@@ -887,8 +863,28 @@ class TimelapseRenderJob(object):
|
||||
finally:
|
||||
self._notify_callback("always", output)
|
||||
|
||||
def _process_ffmpeg_output(self, *lines):
|
||||
for line in lines:
|
||||
# We should be getting the time more often, so try it first
|
||||
time = self._current_regex.search(line)
|
||||
if time is not None and self._duration_i is not 0:
|
||||
current_s = self._convert_time(*time.groups())
|
||||
progress = current_s / float(self._duration_i) * 100
|
||||
|
||||
# Update progress bar
|
||||
for callback in _update_callbacks:
|
||||
try:
|
||||
callback.sendRenderProgress(progress)
|
||||
except:
|
||||
self._logger.exception("Exception while pushing render progress")
|
||||
|
||||
else:
|
||||
duration = self._duration_regex.search(line)
|
||||
if duration is not None:
|
||||
self._duration_i = self._convert_time(*duration.groups())
|
||||
|
||||
@staticmethod
|
||||
def _parsetime(hours, minutes, seconds):
|
||||
def _convert_time(hours, minutes, seconds):
|
||||
return (int(hours) * 60 + int(minutes)) * 60 + int(seconds)
|
||||
|
||||
@classmethod
|
||||
|
||||
@@ -180,3 +180,46 @@ class TimelapseTest(unittest.TestCase):
|
||||
raise ValueError("files must be either dict or list/tuple")
|
||||
|
||||
return result
|
||||
|
||||
def test_ffmpeg_parse(self):
|
||||
|
||||
# Test strings
|
||||
noChange1Str = " built on Jan 7 2014 22:07:02 with gcc 4.8.2 (GCC)"
|
||||
durationStr = " Duration: 00:00:18.60, start: 0.000000, bitrate: "
|
||||
noChange2Str = "0: Video: mpeg2video, yuv420p, 640x480, q=2-31, 10000 kb/s, 90k tbn, 25 tbc"
|
||||
progress1Str = "frame= 134 fps=0.0 q=1.6 size= 1528kB time=00:00:05.28 bitrate=2370.7kbits/s dup=80 drop=0 "
|
||||
progress2Str = "frame= 274 fps=270 q=2.0 size= 2748kB time=00:00:10.88 bitrate=2069.1kbits/s dup=164 drop=0 "
|
||||
|
||||
expectedProgress = 0
|
||||
|
||||
# Callback mock
|
||||
class test_callback:
|
||||
def sendRenderProgress(self, progress):
|
||||
self.assertAlmostEqual(progress, expectedProgress, 3)
|
||||
|
||||
# Register mock callback
|
||||
octoprint.timelapse.register_callback(test_callback)
|
||||
|
||||
r = octoprint.timelapse.TimelapseRenderJob("", "", "", "")
|
||||
self.assertEqual(r._duration_i, 0)
|
||||
|
||||
r._process_ffmpeg_output(noChange1Str)
|
||||
self.assertEqual(r._duration_i, 0)
|
||||
|
||||
r._process_ffmpeg_output(progress1Str)
|
||||
self.assertEqual(r._duration_i, 0)
|
||||
|
||||
r._process_ffmpeg_output(durationStr)
|
||||
self.assertEqual(r._duration_i, 18)
|
||||
|
||||
r._process_ffmpeg_output(noChange2Str)
|
||||
self.assertEqual(r._duration_i, 18)
|
||||
|
||||
expectedProgress = 27.777
|
||||
r._process_ffmpeg_output(progress1Str)
|
||||
self.assertEqual(r._duration_i, 18)
|
||||
|
||||
expectedProgress = 55.555
|
||||
r._process_ffmpeg_output(progress2Str)
|
||||
self.assertEqual(r._duration_i, 18)
|
||||
|
||||
Reference in new issue
Block a user