diff --git a/ffmpeg_streaming/_media.py b/ffmpeg_streaming/_media.py index e8aab5b..e5e856e 100644 --- a/ffmpeg_streaming/_media.py +++ b/ffmpeg_streaming/_media.py @@ -39,9 +39,11 @@ def __init__(self, media, _format: Format, **options): self.key = None self.media = media self.format = _format - self.options = options self.pipe = None self.output_temp = False + self.process = None + self.kill_process_on_exit = options.pop('kill_process_on_exit', False) + self.options = options def finish_up(self): """ @@ -55,6 +57,8 @@ def finish_up(self): shutil.move(os.path.dirname(self.output_), os.path.dirname(str(self.output))) else: shutil.rmtree(os.path.dirname(str(self.output_)), ignore_errors=True) + if self.kill_process_on_exit: + self.process.__exit__() @abc.abstractmethod def set_up(self): @@ -103,6 +107,7 @@ def _run(self, ffmpeg_bin, monitor: callable = None, **options): @TODO: add documentation """ with Process(self, command_builder(ffmpeg_bin, self), monitor, **options) as process: + self.process = process self.pipe, err = process.run() async def async_run(self, ffmpeg_bin, monitor: callable = None, **options): diff --git a/ffmpeg_streaming/_process.py b/ffmpeg_streaming/_process.py index 5ddead8..7374b55 100644 --- a/ffmpeg_streaming/_process.py +++ b/ffmpeg_streaming/_process.py @@ -58,11 +58,14 @@ def __init__(self, media, commands: str, monitor: callable = None, **options): self.process = _p_open(commands, **options) self.media = media self.monitor = monitor + self.exited = False def __enter__(self): return self - def __exit__(self, exc_type, exc_val, exc_tb): + def __exit__(self, exc_type=None, exc_val=None, exc_tb=None): + logging.info("exiting ffmpeg process.") + self.exited = True self.process.kill() def _monitor(self): @@ -119,8 +122,9 @@ def run(self): if self.process.poll(): error = str(Process.err) if Process.err else str(Process.out) - logging.error('ffmpeg failed to execute command: {}'.format(error)) - raise RuntimeError('ffmpeg failed to execute command: ', error) + if not self.exited: + logging.error('ffmpeg failed to execute command: {}'.format(error)) + raise RuntimeError('ffmpeg failed to execute command: ', error) logging.info("ffmpeg executed command successfully")