From 89b06132f617f37a36f79075131b05e14e68c8c6 Mon Sep 17 00:00:00 2001 From: Jesse Bannon Date: Thu, 16 Mar 2023 23:36:13 -0700 Subject: [PATCH] [BUGFIX] Add timeout for bad ffmpeg image conversions --- src/ytdl_sub/utils/ffmpeg.py | 6 ++++-- src/ytdl_sub/utils/thumbnail.py | 3 ++- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/src/ytdl_sub/utils/ffmpeg.py b/src/ytdl_sub/utils/ffmpeg.py index c67d61ce..2cb6f6a3 100644 --- a/src/ytdl_sub/utils/ffmpeg.py +++ b/src/ytdl_sub/utils/ffmpeg.py @@ -73,7 +73,7 @@ class FFMPEG: return f"{relative_file_path}.out.{extension}" @classmethod - def run(cls, ffmpeg_args: List[str]) -> None: + def run(cls, ffmpeg_args: List[str], timeout: Optional[float] = None) -> None: """ Runs an ffmpeg command. Should not include 'ffmpeg' as the beginning argument. @@ -81,6 +81,8 @@ class FFMPEG: ---------- ffmpeg_args: Arguments to pass to ffmpeg. Each one will be separated by a space. + timeout + Optional. timeout """ cls._ensure_installed() @@ -88,7 +90,7 @@ class FFMPEG: cmd.extend(ffmpeg_args) logger.debug("Running %s", " ".join(cmd)) with Logger.handle_external_logs(name="ffmpeg"): - subprocess.run(cmd, check=True, capture_output=True) + subprocess.run(cmd, check=True, capture_output=True, timeout=timeout) def _create_metadata_chapter_entry(start_sec: int, end_sec: int, title: str) -> List[str]: diff --git a/src/ytdl_sub/utils/thumbnail.py b/src/ytdl_sub/utils/thumbnail.py index 144f2838..60d428c3 100644 --- a/src/ytdl_sub/utils/thumbnail.py +++ b/src/ytdl_sub/utils/thumbnail.py @@ -78,7 +78,8 @@ def download_and_convert_url_thumbnail( tmp_output_path = FFMPEG.tmp_file_path( relative_file_path=thumbnail.name, extension="jpg" ) - FFMPEG.run(["-bitexact", "-i", thumbnail.name, tmp_output_path]) + # Add timeout of 1 second in case ffmpeg hangs from a bad thumbnail + FFMPEG.run(["-bitexact", "-i", thumbnail.name, tmp_output_path], timeout=1) # Have FileHandler handle the move to a potential cross-device FileHandler.move(tmp_output_path, output_thumbnail_path)