Skip to content

kill ffmpeg process on finish up if enabled #122

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion ffmpeg_streaming/_media.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
"""
Expand All @@ -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):
Expand Down Expand Up @@ -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):
Expand Down
10 changes: 7 additions & 3 deletions ffmpeg_streaming/_process.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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")

Expand Down