Skip to content

Commit

Permalink
All specifying reencode's tmp dir
Browse files Browse the repository at this point in the history
  • Loading branch information
benoit74 committed Dec 19, 2024
1 parent b66b8d6 commit 0f64df0
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 2 deletions.
4 changes: 3 additions & 1 deletion src/zimscraperlib/video/encoding.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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,
Expand Down
26 changes: 25 additions & 1 deletion tests/video/test_video.py
Original file line number Diff line number Diff line change
Expand Up @@ -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():
Expand Down Expand Up @@ -384,6 +391,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",
Expand Down

0 comments on commit 0f64df0

Please sign in to comment.