File size: 4,116 Bytes
b3b2de2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
"""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