"""使用本地合成媒体验证音轨偏移和双声道,不启动模型。""" import os import shutil import struct import subprocess import wave from pathlib import Path from types import SimpleNamespace import pytest from app.adapters.provider.dialogue_media import run_cpu from app.domain.errors import MediaError def test_native_sidecar_requires_policy_and_is_a_protected_job_input(): from app.adapters.provider.dialogue_media import validate_cpu from app.services.jobs import input_ids inputs = {"video_asset_id": "a" * 32, "audio_asset_id": "b" * 32} p = {"start_frame": 0, "duration_frames": 124, "audio_policy": "native_av_v1"} validate_cpu("audio.dialogue.extract", inputs, p) assert set(input_ids(inputs, p)) == {"a" * 32, "b" * 32} for task, params in ( ("video.dialogue.select", p), ("audio.dialogue.extract", {"start_frame": 0, "duration_frames": 124}), ): with pytest.raises(MediaError): validate_cpu(task, inputs, params) @pytest.mark.skipif( not (shutil.which("ffmpeg") or os.environ.get("FFMPEG_BIN")), reason="ffmpeg unavailable" ) async def test_native_sidecar_preserves_pcm_tail_without_aac_offset_or_padding(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")) ) video, sidecar = tmp_path / "aac.mp4", tmp_path / "native.wav" # 画内 AAC 故意有另一段时序;侧车必须来自明确的素材引用,不能误用 AAC。 subprocess.run( [ ffmpeg, "-v", "error", "-y", "-f", "lavfi", "-i", "testsrc2=size=64x64:rate=24:duration=1", "-itsoffset", "0.25", "-f", "lavfi", "-i", "sine=frequency=440:sample_rate=32000:duration=1", "-c:v", "libx264", "-c:a", "aac", str(video), ], check=True, ) with wave.open(str(sidecar), "wb") as pcm: pcm.setparams((2, 2, 32000, 0, "NONE", "not compressed")) pcm.writeframes(struct.pack(" 8000 assert min(values[2001:90000:2]) > 16000 @pytest.mark.skipif( not (shutil.which("ffmpeg") or os.environ.get("FFMPEG_BIN")), reason="ffmpeg unavailable" ) async def test_native_sidecar_submission_checks_owner_and_protects_both_sources( system, other_client, tmp_path ): import io from datetime import datetime, timedelta, timezone from sqlalchemy import select from app.models import JobAsset 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")) ) system.settings.ffmpeg_bin, system.settings.ffprobe_bin = ffmpeg, ffprobe video = tmp_path / "upload.mp4" subprocess.run( [ ffmpeg, "-v", "error", "-y", "-f", "lavfi", "-i", "testsrc2=size=64x64:rate=24:duration=1", "-an", str(video), ], check=True, ) pcm = io.BytesIO() with wave.open(pcm, "wb") as audio: audio.setparams((2, 2, 32000, 0, "NONE", "not compressed")) audio.writeframes(struct.pack(" 6000 assert min(right[25000:34000]) > 12000