Skip to content

Commit db251ee

Browse files
authored
Merge pull request #230 from openzim/reencode_tmp_file
Allow specifying reencode's tmp dir
2 parents 562c997 + 92cb99e commit db251ee

File tree

2 files changed

+29
-2
lines changed

2 files changed

+29
-2
lines changed

src/zimscraperlib/video/encoding.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
from copy import deepcopy
66

77
from zimscraperlib import logger
8+
from zimscraperlib.filesystem import path_from
89
from zimscraperlib.logging import nicer_args_join
910

1011

@@ -40,6 +41,7 @@ def reencode(
4041
*,
4142
delete_src: bool = False,
4243
failsafe: bool = True,
44+
existing_tmp_path: pathlib.Path | None = None,
4345
) -> tuple[bool, subprocess.CompletedProcess[str]]:
4446
"""Runs ffmpeg with given ffmpeg_args
4547
@@ -52,7 +54,8 @@ def reencode(
5254
failsafe - Run in failsafe mode
5355
"""
5456

55-
with tempfile.TemporaryDirectory() as tmp_dir:
57+
with path_from(existing_tmp_path or tempfile.TemporaryDirectory()) as tmp_dir:
58+
5659
tmp_path = pathlib.Path(tmp_dir).joinpath(f"video.tmp{dst_path.suffix}")
5760
args = _build_ffmpeg_args(
5861
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)