"width is None and height is None" was spelled out at analysis._require_video, analysis.analyse's diagnosis and pipeline's shots params/skip policy. A computed property on MediaFacts names the fact once. - models: MediaFacts.has_video (foundation file; the code-review record authorizes this deliberate edit per ADR-0001) - analysis/pipeline: the three sites ask media.has_video - tests: the property over a video and an audio-only MediaFacts
234 lines
7.2 KiB
Python
234 lines
7.2 KiB
Python
"""Round-trip and default tests for every pydantic model."""
|
|
|
|
from __future__ import annotations
|
|
|
|
from pathlib import Path
|
|
|
|
from video_analyse.models import (
|
|
Artifacts,
|
|
Highlight,
|
|
MediaFacts,
|
|
MediaRef,
|
|
ReportCore,
|
|
Segment,
|
|
Shot,
|
|
ShotDescription,
|
|
SpeakerTurn,
|
|
Transcript,
|
|
VideoReport,
|
|
Word,
|
|
)
|
|
|
|
|
|
def _transcript() -> Transcript:
|
|
return Transcript(
|
|
language="en",
|
|
duration=12.5,
|
|
backend="faster-whisper",
|
|
model="large-v3-turbo",
|
|
segments=[
|
|
Segment(
|
|
start=0.0,
|
|
end=1.5,
|
|
text="hello world",
|
|
words=[Word(text="hello", start=0.0, end=0.7, probability=0.93)],
|
|
speaker="S1",
|
|
)
|
|
],
|
|
speaker_turns=[SpeakerTurn(start=0.0, end=12.5, speaker="S1")],
|
|
)
|
|
|
|
|
|
def _shot(tmp_path: Path) -> Shot:
|
|
return Shot(
|
|
index=0,
|
|
start=0.0,
|
|
end=4.2,
|
|
keyframe=tmp_path / "keyframes" / "000.jpg",
|
|
phash="f0e1d2c3b4a59687",
|
|
transcript_slice="S1: hello world",
|
|
description=ShotDescription(
|
|
summary="A person speaks to camera",
|
|
on_screen_text="HELLO",
|
|
shot_type="close-up",
|
|
people=["presenter"],
|
|
notable_objects=["microphone"],
|
|
setting="studio",
|
|
),
|
|
)
|
|
|
|
|
|
def test_media_facts_roundtrip() -> None:
|
|
media = MediaFacts(
|
|
duration=10.0,
|
|
width=640,
|
|
height=360,
|
|
fps=12.0,
|
|
has_audio=True,
|
|
size_bytes=12345,
|
|
format="mp4",
|
|
video_codec="h264",
|
|
audio_codec="aac",
|
|
extra={"bit_rate": "48k"},
|
|
)
|
|
assert "path" not in MediaFacts.model_fields
|
|
assert "source" not in MediaFacts.model_fields
|
|
|
|
dumped = media.model_dump_json()
|
|
restored = MediaFacts.model_validate_json(dumped)
|
|
assert restored == media
|
|
assert restored.extra["bit_rate"] == "48k"
|
|
|
|
|
|
def test_media_facts_extra_drops_local_paths() -> None:
|
|
media = MediaFacts(
|
|
duration=1.0,
|
|
has_audio=True,
|
|
size_bytes=10,
|
|
extra={
|
|
"ffprobe": {
|
|
"format": {"filename": "/tmp/private/video.mp4", "format_name": "mp4"},
|
|
"streams": [{"codec_type": "video", "filename": "/tmp/private/video.mp4"}],
|
|
},
|
|
"title": "Safe title",
|
|
},
|
|
)
|
|
|
|
assert media.extra == {
|
|
"ffprobe": {"format": {"format_name": "mp4"}, "streams": [{"codec_type": "video"}]},
|
|
"title": "Safe title",
|
|
}
|
|
|
|
|
|
def test_media_ref_roundtrip(tmp_path: Path) -> None:
|
|
ref = MediaRef(source="https://example.com/clip.mp4", local_path=tmp_path / "clip.mp4")
|
|
|
|
restored = MediaRef.model_validate_json(ref.model_dump_json())
|
|
|
|
assert restored == ref
|
|
assert isinstance(restored.local_path, Path)
|
|
|
|
|
|
def test_nullable_media_facts_fields_default_to_none() -> None:
|
|
media = MediaFacts(
|
|
duration=3.0,
|
|
has_audio=True,
|
|
size_bytes=1,
|
|
)
|
|
assert media.width is None
|
|
assert media.fps is None
|
|
assert media.video_codec is None
|
|
assert media.format is None
|
|
assert media.extra == {}
|
|
|
|
|
|
def test_media_facts_has_video_follows_the_resolution() -> None:
|
|
"""A known width or height is what "has a video stream" means, in one place."""
|
|
video = MediaFacts(duration=3.0, width=640, height=360, has_audio=True, size_bytes=1)
|
|
audio_only = MediaFacts(duration=3.0, has_audio=True, size_bytes=1)
|
|
|
|
assert video.has_video is True
|
|
assert audio_only.has_video is False
|
|
|
|
|
|
def test_word_and_segment_defaults() -> None:
|
|
word = Word(text="hi", start=0.0, end=0.2)
|
|
assert word.probability is None
|
|
segment = Segment(start=0.0, end=0.2, text="hi")
|
|
assert segment.words == []
|
|
assert segment.speaker is None
|
|
# default containers are per-instance, not shared
|
|
segment.words.append(word)
|
|
assert Segment(start=0.0, end=0.2, text="hi").words == []
|
|
|
|
|
|
def test_transcript_roundtrip(tmp_path: Path) -> None:
|
|
transcript = _transcript()
|
|
restored = Transcript.model_validate_json(transcript.model_dump_json())
|
|
assert restored == transcript
|
|
assert restored.segments[0].words[0].probability == 0.93
|
|
assert restored.speaker_turns[0].speaker == "S1"
|
|
assert restored.backend == "faster-whisper"
|
|
|
|
|
|
def test_shot_roundtrip_with_nested_description(tmp_path: Path) -> None:
|
|
shot = _shot(tmp_path)
|
|
restored = Shot.model_validate_json(shot.model_dump_json())
|
|
assert restored == shot
|
|
assert isinstance(restored.keyframe, Path)
|
|
assert restored.description is not None
|
|
assert restored.description.shot_type == "close-up"
|
|
|
|
bare = Shot(index=1, start=4.2, end=8.0)
|
|
assert bare.keyframe is None
|
|
assert bare.phash is None
|
|
assert bare.transcript_slice == ""
|
|
assert bare.description is None
|
|
|
|
|
|
def test_shot_description_requires_known_shot_type() -> None:
|
|
desc = ShotDescription(summary="s", shot_type="screen")
|
|
assert desc.people == [] and desc.notable_objects == [] and desc.on_screen_text is None
|
|
assert ShotDescription.model_validate_json(desc.model_dump_json()) == desc
|
|
|
|
|
|
def test_report_core_and_highlight() -> None:
|
|
core = ReportCore(
|
|
title="Talk",
|
|
summary="A short talk",
|
|
topics=["ai"],
|
|
people=["Ada"],
|
|
highlights=[Highlight(start=1.0, end=2.0, description="key line")],
|
|
)
|
|
restored = ReportCore.model_validate_json(core.model_dump_json())
|
|
assert restored == core
|
|
assert restored.highlights[0].description == "key line"
|
|
|
|
|
|
def test_video_report_roundtrip(tmp_path: Path) -> None:
|
|
report = VideoReport(
|
|
source="video.mp4",
|
|
title="Talk",
|
|
duration=10.0,
|
|
summary="A short talk about things",
|
|
topics=["ai", "video"],
|
|
people=["Ada"],
|
|
highlights=[Highlight(start=1.0, end=2.0, description="key line")],
|
|
shots=[_shot(tmp_path)],
|
|
transcript=_transcript(),
|
|
metadata={"ffprobe": {"format_name": "mov,mp4"}},
|
|
)
|
|
restored = VideoReport.model_validate_json(report.model_dump_json())
|
|
assert restored == report
|
|
assert restored.transcript is not None
|
|
assert restored.shots[0].keyframe == tmp_path / "keyframes" / "000.jpg"
|
|
assert isinstance(restored.shots[0].keyframe, Path)
|
|
|
|
# transcript is optional
|
|
no_transcript = report.model_copy(update={"transcript": None})
|
|
assert VideoReport.model_validate_json(no_transcript.model_dump_json()).transcript is None
|
|
|
|
|
|
def test_artifacts_defaults_and_roundtrip(tmp_path: Path) -> None:
|
|
artifacts = Artifacts(source="video.mp4", workdir=tmp_path)
|
|
assert artifacts.media is None
|
|
assert artifacts.ref is None
|
|
assert artifacts.transcript is None
|
|
assert artifacts.shots == []
|
|
assert artifacts.report is None
|
|
|
|
restored = Artifacts.model_validate_json(artifacts.model_dump_json())
|
|
assert restored == artifacts
|
|
assert isinstance(restored.workdir, Path)
|
|
|
|
with_ref = Artifacts(
|
|
source="video.mp4",
|
|
workdir=tmp_path,
|
|
media=MediaFacts(duration=10.0, has_audio=True, size_bytes=123),
|
|
ref=MediaRef(source="video.mp4", local_path=tmp_path / "video.mp4"),
|
|
)
|
|
restored = Artifacts.model_validate_json(with_ref.model_dump_json())
|
|
assert restored == with_ref
|
|
assert restored.ref is not None
|
|
assert isinstance(restored.ref.local_path, Path)
|