LT / tests /test_video_format.py
soorena68's picture
Upload 49 files
b3b2de2 verified
Raw
History Blame Contribute Delete
4.12 kB
"""Tests for video_format — MP4 -> GIF/WebM delivery transcode.
Runs against the real bundled ffmpeg (via imageio_ffmpeg). The module is shared
byte-for-byte with the wan2.2 Space, so this also covers that Space's behaviour.
"""
import pathlib
import subprocess
import sys
import pytest
sys.path.insert(0, str(pathlib.Path(__file__).resolve().parent.parent))
import video_format # noqa: E402
def _ffmpeg() -> str:
imageio_ffmpeg = pytest.importorskip("imageio_ffmpeg")
return imageio_ffmpeg.get_ffmpeg_exe()
@pytest.fixture
def sample_mp4(tmp_path: pathlib.Path) -> str:
"""A tiny 1s 128x128 H.264 MP4 generated with ffmpeg's testsrc."""
out = tmp_path / "sample.mp4"
subprocess.run(
[
_ffmpeg(), "-hide_banner", "-y",
"-f", "lavfi", "-i", "testsrc=duration=1:size=128x128:rate=10",
"-pix_fmt", "yuv420p", str(out),
],
check=True,
capture_output=True,
)
return str(out)
def _decodes(path: str) -> bool:
"""True if ffmpeg can fully decode `path` without errors."""
proc = subprocess.run(
[_ffmpeg(), "-hide_banner", "-v", "error", "-i", path, "-f", "null", "-"],
capture_output=True,
)
return proc.returncode == 0
# --- format-key normalisation -------------------------------------------------
@pytest.mark.parametrize(
"raw,expected",
[
("mp4", "mp4"), ("gif", "gif"), ("webm", "webm"),
("MP4", "mp4"), (" WebM ", "webm"), (".gif", "gif"),
("", "mp4"), (None, "mp4"), ("mov", "mp4"), ("avi", "mp4"),
],
)
def test_normalize_format(raw, expected):
assert video_format.normalize_format(raw) == expected
def test_ext_and_content_type():
assert video_format.ext_for("gif") == ".gif"
assert video_format.ext_for("webm") == ".webm"
assert video_format.ext_for("anything-unknown") == ".mp4"
assert video_format.content_type_for("gif") == "image/gif"
assert video_format.content_type_for("webm") == "video/webm"
assert video_format.content_type_for("mp4") == "video/mp4"
def test_supported_formats_mp4_first():
assert video_format.supported_formats()[0] == "mp4"
assert set(video_format.supported_formats()) == {"mp4", "gif", "webm"}
# --- transcoding --------------------------------------------------------------
def test_mp4_is_passthrough(sample_mp4):
"""mp4 (and unknown formats) return the input path unchanged — no transcode."""
assert video_format.convert(sample_mp4, "mp4") == sample_mp4
assert video_format.convert(sample_mp4, "mov") == sample_mp4
def test_convert_to_gif(sample_mp4, tmp_path):
out = str(tmp_path / "out.gif")
result = video_format.convert(sample_mp4, "gif", out)
assert result == out
data = pathlib.Path(out).read_bytes()
assert data[:4] == b"GIF8" # GIF87a / GIF89a magic
assert len(data) > 0
def test_convert_to_webm(sample_mp4, tmp_path):
out = str(tmp_path / "out.webm")
result = video_format.convert(sample_mp4, "webm", out)
assert result == out
data = pathlib.Path(out).read_bytes()
assert data[:4] == b"\x1a\x45\xdf\xa3" # EBML/Matroska magic
assert _decodes(out)
def test_convert_self_allocates_path_with_right_suffix(sample_mp4):
"""With no out_path, a temp file with the format's extension is created."""
result = video_format.convert(sample_mp4, "webm")
assert result != sample_mp4
assert result.endswith(".webm")
assert pathlib.Path(result).exists()
pathlib.Path(result).unlink()
def test_failure_falls_back_to_input(tmp_path):
"""A non-video input can't be transcoded -> caller keeps the original path."""
bogus = tmp_path / "not-a-video.mp4"
bogus.write_text("this is not an mp4")
out = str(tmp_path / "out.gif")
result = video_format.convert(str(bogus), "gif", out)
assert result == str(bogus) # fell back to the input path
assert not pathlib.Path(out).exists() # half-written output cleaned up