cmvr-visual/backend/tests/test_dialogue_media.py

168 lines
4.8 KiB
Python

from types import SimpleNamespace
from pathlib import Path
import shutil
import subprocess
import os
import pytest
from app.adapters.provider.dialogue_media import validate_cpu, run_cpu
from app.adapters.media import inspect_video_track
from app.domain.errors import MediaError
def test_select_rejects_unknown_fields_and_non_integer_frames():
for params in (
{"start_frame": 0, "duration_frames": True},
{"start_frame": 0, "duration_frames": 24, "loop": True},
):
with pytest.raises(MediaError):
validate_cpu("video.dialogue.select", {"video_asset_id": "a" * 32}, params)
def test_review_uses_explicit_effective_frame_interval():
validate_cpu(
"video.dialogue.review",
{
"video_asset_id": "a" * 32,
"expected": {
"source_start_frame": 0,
"source_end_frame": 24,
"expected_motion": True,
"fps": 24,
},
},
{},
)
with pytest.raises(MediaError):
validate_cpu(
"video.dialogue.review",
{
"video_asset_id": "a" * 32,
"expected": {"source_start_frame": 0, "source_end_frame": True},
},
{},
)
@pytest.mark.skipif(
not (shutil.which("ffmpeg") or os.environ.get("FFMPEG_BIN")), reason="ffmpeg unavailable"
)
async def test_selected_video_keeps_exact_frames_and_discards_audio(tmp_path):
ffmpeg = os.environ.get("FFMPEG_BIN") or shutil.which("ffmpeg")
ffprobe = (
os.environ.get("FFPROBE_BIN")
or shutil.which("ffprobe")
or str(Path(ffmpeg).with_name("ffprobe.exe"))
)
source = tmp_path / "source.mp4"
subprocess.run(
[
ffmpeg,
"-v",
"error",
"-y",
"-f",
"lavfi",
"-i",
"testsrc2=size=128x128:rate=24:duration=2",
"-f",
"lavfi",
"-i",
"sine=frequency=440:duration=2",
"-c:v",
"libx264",
"-pix_fmt",
"yuv420p",
"-c:a",
"aac",
"-shortest",
str(source),
],
check=True,
)
work = tmp_path / "work"
work.mkdir()
settings = SimpleNamespace(
ffmpeg_bin=ffmpeg, ffprobe_bin=ffprobe, max_output_bytes=10**8, workspace_root=tmp_path
)
provider = SimpleNamespace(settings=settings, _asset=lambda aid, assets: assets[aid])
result = await run_cpu(
provider,
"video.dialogue.select",
{"video_asset_id": "a" * 32},
{"start_frame": 12, "duration_frames": 24},
{"a" * 32: source},
work,
)
track = await inspect_video_track(result.outputs[0], settings)
assert track["frames"] == 24 and track["fps"] == 24
import json
streams = json.loads(
subprocess.check_output(
[ffprobe, "-v", "error", "-show_streams", "-of", "json", str(result.outputs[0])]
)
)["streams"]
assert [s["codec_type"] for s in streams] == ["video"]
with pytest.raises(MediaError):
await run_cpu(
provider,
"video.dialogue.select",
{"video_asset_id": "a" * 32},
{"start_frame": 30, "duration_frames": 24},
{"a" * 32: source},
work,
)
@pytest.mark.skipif(
not (shutil.which("ffmpeg") or os.environ.get("FFMPEG_BIN")), reason="ffmpeg unavailable"
)
async def test_extract_original_audio_uses_selected_sample_interval(tmp_path):
import wave
import struct
ffmpeg = os.environ.get("FFMPEG_BIN") or shutil.which("ffmpeg")
provider = SimpleNamespace(
settings=SimpleNamespace(ffmpeg_bin=ffmpeg), _asset=lambda aid, assets: assets[aid]
)
source = tmp_path / "audio.mkv"
subprocess.run(
[
ffmpeg,
"-v",
"error",
"-y",
"-f",
"lavfi",
"-i",
"testsrc2=size=64x64:rate=24:duration=2",
"-f",
"lavfi",
"-i",
"aevalsrc=0.5*between(t\\,1.25\\,1.5):s=48000:d=2",
"-c:v",
"libx264",
"-c:a",
"pcm_s16le",
str(source),
],
check=True,
)
source_id = "a" * 32
out = await run_cpu(
provider,
"audio.dialogue.extract",
{"video_asset_id": source_id},
{"start_frame": 24, "duration_frames": 24},
{source_id: source},
tmp_path,
)
assert out.result["sample_count"] == 48000
with wave.open(str(out.outputs[0]), "rb") as w:
samples = struct.unpack("<" + "h" * 96000, w.readframes(48000))[::2]
assert (
max(samples[:11000]) == 0 and min(samples[13000:23000]) > 4000 and max(samples[25000:]) == 0
)