From ec95a3cbf9947150cc6750c15a224294aea94235 Mon Sep 17 00:00:00 2001 From: benoit74 Date: Thu, 19 Dec 2024 16:55:04 +0000 Subject: [PATCH] Allow specifying reencode's tmp dir --- src/zimscraperlib/video/encoding.py | 4 +++- tests/video/test_video.py | 26 +++++++++++++++++++++++++- 2 files changed, 28 insertions(+), 2 deletions(-) diff --git a/src/zimscraperlib/video/encoding.py b/src/zimscraperlib/video/encoding.py index 56c98e6..f2a9c20 100644 --- a/src/zimscraperlib/video/encoding.py +++ b/src/zimscraperlib/video/encoding.py @@ -40,6 +40,7 @@ def reencode( *, delete_src: bool = False, failsafe: bool = True, + existing_tmp_path: pathlib.Path | None = None, ) -> tuple[bool, subprocess.CompletedProcess[str]]: """Runs ffmpeg with given ffmpeg_args @@ -52,7 +53,8 @@ def reencode( failsafe - Run in failsafe mode """ - with tempfile.TemporaryDirectory() as tmp_dir: + with existing_tmp_path or tempfile.TemporaryDirectory() as tmp_dir: + tmp_path = pathlib.Path(tmp_dir).joinpath(f"video.tmp{dst_path.suffix}") args = _build_ffmpeg_args( src_path=src_path, diff --git a/tests/video/test_video.py b/tests/video/test_video.py index 684d66d..84a2125 100644 --- a/tests/video/test_video.py +++ b/tests/video/test_video.py @@ -29,12 +29,19 @@ def copy_media_and_reencode( dest: str, ffmpeg_args: list[str], test_files: dict[str, pathlib.Path], + *, + use_temp_dir_for_temp_file: bool = False, **kwargs: Any, ): src_path = temp_dir.joinpath(src) dest_path = temp_dir.joinpath(dest) shutil.copy2(test_files[src_path.suffix[1:]], src_path) - return reencode(src_path, dest_path, ffmpeg_args, **kwargs) + if use_temp_dir_for_temp_file: + return reencode( + src_path, dest_path, ffmpeg_args, existing_tmp_path=temp_dir, **kwargs + ) + else: + return reencode(src_path, dest_path, ffmpeg_args, **kwargs) def test_config_defaults(): @@ -392,6 +399,23 @@ def test_reencode_media( assert expected["codecs"] == converted_details["codecs"] +@pytest.mark.slow +def test_reencode_media_with_tmp_dir(test_files: dict[str, pathlib.Path]): + with tempfile.TemporaryDirectory() as t: + temp_dir = pathlib.Path(t) + copy_media_and_reencode( + temp_dir, + "video.mp4", + "video.webm", + VideoWebmLow().to_ffmpeg_args(), + test_files, + use_temp_dir_for_temp_file=True, + ) + converted_details = get_media_info(temp_dir.joinpath("video.webm")) + assert converted_details["duration"] == 2 + assert converted_details["codecs"] == ["vp9", "vorbis"] + + @pytest.mark.slow @pytest.mark.parametrize( "src,dest,ffmpeg_args,delete_src",