From 9f9fe2054c2fd5ef624d06cd6731718693475426 Mon Sep 17 00:00:00 2001 From: Tage Johansson Date: Thu, 28 Mar 2024 14:18:54 +0100 Subject: [PATCH] Prolong timeout for ffmpeg termination. --- chess_cli/record.py | 9 +++++---- chess_cli/record_cmds.py | 2 ++ 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/chess_cli/record.py b/chess_cli/record.py index cbaf78c..69a7792 100644 --- a/chess_cli/record.py +++ b/chess_cli/record.py @@ -45,7 +45,7 @@ FRAMES_PER_BUFFER: int = 8192 ERROR_REGEX: re.Pattern = re.compile("error", flags=re.IGNORECASE) -PROCESS_TERMINATE_TIMEOUT: float = 3.0 # Timeout for a process to terminate before killing it. +PROCESS_TERMINATE_TIMEOUT: float = 20.0 # Timeout for a process to terminate before killing it. def _find_ffmpeg_exe() -> list[bytes] | None: @@ -214,7 +214,7 @@ def set_mark(self, comment: str | None = None) -> None: assert len(self.boards) > 0 self.marks.append((len(self.boards) - 1, comment)) - async def stop(self) -> float: + async def stop(self, timeout: float | None = None) -> float: """Stop the recording. No pause/resumes can take place after this. Returns the length of the recording @@ -235,7 +235,7 @@ async def stop(self) -> float: print("Terminating FFMpeg.") self.ffmpeg_process.terminate() try: - await asyncio.wait_for(self.ffmpeg_process.wait(), PROCESS_TERMINATE_TIMEOUT) + await asyncio.wait_for(self.ffmpeg_process.wait(), timeout) except TimeoutError: print("Warning: ffmpeg did not listen to SIGTERM so we have to kill it.") self.ffmpeg_process.kill() @@ -497,13 +497,14 @@ async def save_recording( marks_file: PurePath | None = None, override_output_file: bool = False, no_cleanup: bool = False, + timeout: float | None = None, ) -> float: """Stop and save the recording. Returns the length of the recording in seconds. """ assert self.recording is not None - duration = await self.recording.stop() + duration = await self.recording.stop(timeout=timeout) await self.recording.save( output_file=output_file.with_suffix(".mp4"), marks_file=m.with_suffix(".txt") if (m := marks_file) is not None else None, diff --git a/chess_cli/record_cmds.py b/chess_cli/record_cmds.py index 39a4071..8cbae2d 100644 --- a/chess_cli/record_cmds.py +++ b/chess_cli/record_cmds.py @@ -40,6 +40,7 @@ class RecordCmds(Record): action="store_true", help="Don't remove any created temporary files. Mostly useful for debugging.", ) + record_save_argparser.add_argument("--timeout", type=float, help="A timeout for stopping ffmpeg.") record_subcmds.add_parser("delete", help="Delete the ongoing recording.") record_mark_argparser = record_subcmds.add_parser( "mark", help="Mark the current position so that its timestamp can be remembered." @@ -92,6 +93,7 @@ async def do_record(self, args) -> None: marks_file=args.marks_file, override_output_file=args.override, no_cleanup=args.no_cleanup, + timeout=args.timeout, ) print(f"Recording successfully saved to {args.output_file}") print(f"It was {show_time(duration)} long.")