Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow specifying reencode's tmp dir #230

Open
wants to merge 1 commit into
base: strict_mode
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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:
benoit74 marked this conversation as resolved.
Show resolved Hide resolved

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 @@ -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",
Expand Down