Skip to content

Commit 560330e

Browse files
committed
Allow specifying reencode's tmp dir
1 parent 562c997 commit 560330e

File tree

2 files changed

+28
-2
lines changed

2 files changed

+28
-2
lines changed

src/zimscraperlib/video/encoding.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ def reencode(
4040
*,
4141
delete_src: bool = False,
4242
failsafe: bool = True,
43+
existing_tmp_path: pathlib.Path | None = None,
4344
) -> tuple[bool, subprocess.CompletedProcess[str]]:
4445
"""Runs ffmpeg with given ffmpeg_args
4546
@@ -52,7 +53,8 @@ def reencode(
5253
failsafe - Run in failsafe mode
5354
"""
5455

55-
with tempfile.TemporaryDirectory() as tmp_dir:
56+
with existing_tmp_path or tempfile.TemporaryDirectory() as tmp_dir:
57+
5658
tmp_path = pathlib.Path(tmp_dir).joinpath(f"video.tmp{dst_path.suffix}")
5759
args = _build_ffmpeg_args(
5860
src_path=src_path,

tests/video/test_video.py

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,19 @@ def copy_media_and_reencode(
2929
dest: str,
3030
ffmpeg_args: list[str],
3131
test_files: dict[str, pathlib.Path],
32+
*,
33+
use_temp_dir_for_temp_file: bool = False,
3234
**kwargs: Any,
3335
):
3436
src_path = temp_dir.joinpath(src)
3537
dest_path = temp_dir.joinpath(dest)
3638
shutil.copy2(test_files[src_path.suffix[1:]], src_path)
37-
return reencode(src_path, dest_path, ffmpeg_args, **kwargs)
39+
if use_temp_dir_for_temp_file:
40+
return reencode(
41+
src_path, dest_path, ffmpeg_args, existing_tmp_path=temp_dir, **kwargs
42+
)
43+
else:
44+
return reencode(src_path, dest_path, ffmpeg_args, **kwargs)
3845

3946

4047
def test_config_defaults():
@@ -392,6 +399,23 @@ def test_reencode_media(
392399
assert expected["codecs"] == converted_details["codecs"]
393400

394401

402+
@pytest.mark.slow
403+
def test_reencode_media_with_tmp_dir(test_files: dict[str, pathlib.Path]):
404+
with tempfile.TemporaryDirectory() as t:
405+
temp_dir = pathlib.Path(t)
406+
copy_media_and_reencode(
407+
temp_dir,
408+
"video.mp4",
409+
"video.webm",
410+
VideoWebmLow().to_ffmpeg_args(),
411+
test_files,
412+
use_temp_dir_for_temp_file=True,
413+
)
414+
converted_details = get_media_info(temp_dir.joinpath("video.webm"))
415+
assert converted_details["duration"] == 2
416+
assert converted_details["codecs"] == ["vp9", "vorbis"]
417+
418+
395419
@pytest.mark.slow
396420
@pytest.mark.parametrize(
397421
"src,dest,ffmpeg_args,delete_src",

0 commit comments

Comments
 (0)