From 0124eb8691a81f281018219e134f009b37fdd6a4 Mon Sep 17 00:00:00 2001 From: wantt Date: Tue, 10 Aug 2021 09:11:24 +0800 Subject: [PATCH 1/2] add timeout support for run and run_async --- ffmpeg/_run.py | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/ffmpeg/_run.py b/ffmpeg/_run.py index eadb90a4..d6781d81 100644 --- a/ffmpeg/_run.py +++ b/ffmpeg/_run.py @@ -172,7 +172,7 @@ def get_args(stream_spec, overwrite_output=False): @output_operator() -def compile(stream_spec, cmd='ffmpeg', overwrite_output=False): +def compile(stream_spec, cmd='ffmpeg', overwrite_output=False,timeout=-1): """Build command-line for invoking ffmpeg. The :meth:`run` function uses this to build the command line @@ -187,6 +187,9 @@ def compile(stream_spec, cmd='ffmpeg', overwrite_output=False): cmd = [cmd] elif type(cmd) != list: cmd = list(cmd) + if timeout >0: + cmd.insert(0,'%d'%timeout) + cmd.insert(0,'timeout') return cmd + get_args(stream_spec, overwrite_output=overwrite_output) @@ -199,7 +202,8 @@ def run_async( pipe_stderr=False, quiet=False, overwrite_output=False, - cwd=None + cwd=None, + timeout=-1 ): """Asynchronously invoke ffmpeg for the supplied node graph. @@ -300,7 +304,8 @@ def run( input=None, quiet=False, overwrite_output=False, - cwd=None + cwd=None, + timeout=-1 ): """Invoke ffmpeg for the supplied node graph. From b06b0860fca959c423a4a3214df21f843af6ddb4 Mon Sep 17 00:00:00 2001 From: wantt Date: Fri, 13 Aug 2021 10:19:12 +0800 Subject: [PATCH 2/2] add timeout for run and run_async --- ffmpeg/_run.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/ffmpeg/_run.py b/ffmpeg/_run.py index d6781d81..9d17de69 100644 --- a/ffmpeg/_run.py +++ b/ffmpeg/_run.py @@ -282,7 +282,7 @@ def run_async( .. _subprocess Popen: https://docs.python.org/3/library/subprocess.html#popen-objects """ - args = compile(stream_spec, cmd, overwrite_output=overwrite_output) + args = compile(stream_spec, cmd, overwrite_output=overwrite_output,timeout=timeout) stdin_stream = subprocess.PIPE if pipe_stdin else None stdout_stream = subprocess.PIPE if pipe_stdout else None stderr_stream = subprocess.PIPE if pipe_stderr else None @@ -329,7 +329,8 @@ def run( pipe_stderr=capture_stderr, quiet=quiet, overwrite_output=overwrite_output, - cwd=cwd + cwd=cwd, + timeout=timeout ) out, err = process.communicate(input) retcode = process.poll()