nex_animation/backend/tests/test_delivery.py

371 lines
22 KiB
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

"""图2 节点 10~12:人工审核 → 时间线编排(镜头/配音/字幕/配乐)→ 合成 → 导出。
覆盖长视频合成引擎的数据面(时间线重算、转场压缩、音画对齐、前置体检)
与渲染面(导出预设、时长校验、成片下载、字幕烧录降级)。
"""
from __future__ import annotations
import io
import math
import struct
import wave
from tests.conftest import absolute
from app.constants import canvas_for
from app.services import ffmpeg as ff
# ---------------------------------------------------------------- 公共准备
def _prepare(api, studio, script_text, make_image_png, *, approve: bool = True):
"""导入分镜 → 生成(含配音)→ 全部审核合格,返回项目与镜头 id。"""
pid = studio["project_id"]
api.post(f"/api/projects/{pid}/import",
data={"script_text": script_text, "default_duration": 2},
files=[("images", ("s01.png", make_image_png(480, 270, "0x226699"), "image/png")),
("images", ("s02.png", make_image_png(480, 270, "0x993355"), "image/png"))])
api.post(f"/api/projects/{pid}/tasks", json={"with_voiceover": True})
api.drain(pid)
shots = api.get(f"/api/projects/{pid}/shots")["shots"]
if approve:
for shot in shots:
api.post(f"/api/projects/{pid}/shots/{shot['id']}/approve", json={})
return pid, [s["id"] for s in shots]
def _make_wav(seconds: float = 3.0, freq: float = 440.0, rate: int = 44100) -> bytes:
"""生成一段真实可播放的正弦波 WAV,用作背景音乐音轨。"""
frames = bytearray()
for i in range(int(seconds * rate)):
value = int(9000 * math.sin(2 * math.pi * freq * i / rate))
frames += struct.pack("<hh", value, value)
buffer = io.BytesIO()
with wave.open(buffer, "wb") as handle:
handle.setnchannels(2)
handle.setsampwidth(2)
handle.setframerate(rate)
handle.writeframes(bytes(frames))
return buffer.getvalue()
# ---------------------------------------------------------------- 时间线数据面
def test_timeline_requires_approved_shots_and_lays_out_voice_and_subtitles(
api, studio, script_text, make_image_png):
pid, shot_ids = _prepare(api, studio, script_text, make_image_png, approve=False)
# 未审核合格 → 只允许显式包含未审核镜头
api.post(f"/api/projects/{pid}/timeline", json={"only_approved": True}, expect=400)
built = api.post(f"/api/projects/{pid}/timeline", json={"only_approved": False})
timeline = built["result"]["timeline"]
# 镜头尚未全部审核合格 → 只排草稿时间线,项目仍停在审核阶段(不被合成覆盖)
assert built["summary"]["status"] == "reviewing"
assert built["summary"]["current_stage"] in ("review", "quality_check")
assert len(timeline["segments"]) == 2 and timeline["version"] == 1
for shot in api.get(f"/api/projects/{pid}/shots")["shots"]:
api.post(f"/api/projects/{pid}/shots/{shot['id']}/approve", json={})
response = api.post(f"/api/projects/{pid}/timeline",
json={"name": "主时间线", "only_approved": True})
rebuilt = response["result"]
# 全部审核合格 + 已有时间线 → 项目进入合成阶段,流程节点指向 compose
assert response["summary"]["status"] == "composing"
assert response["summary"]["current_stage"] == "compose"
assert rebuilt["version"] == 2 and rebuilt["segments"] == 2
assert rebuilt["missing_voiceover"] == []
timeline = api.get(f"/api/projects/{pid}/timeline")["timeline"]
assert timeline["is_current"] is True and timeline["version"] == 2
assert timeline["width"] == 854 and timeline["height"] == 480 and timeline["fps"] == 24
segments = timeline["segments"]
assert [s["shot_id"] for s in segments] == shot_ids
assert segments[0]["start_ms"] == 0
assert segments[0]["duration_ms"] >= 2000 and segments[1]["start_ms"] == segments[0]["duration_ms"]
assert timeline["total_duration_ms"] == sum(s["duration_ms"] for s in segments)
assert all(s["source_url"].startswith("/media/") and absolute(s["source_path"]).exists()
for s in segments)
# 配音进时间线,并自动按台词生成字幕
voice = [c for c in timeline["audio_clips"] if c["kind"] == "voiceover"]
assert len(voice) == 2 and all(c["duration_ms"] > 200 for c in voice)
assert sorted(c["start_ms"] for c in voice) == [segments[0]["start_ms"] + 160,
segments[1]["start_ms"] + 160]
assert len(timeline["subtitles"]) >= 2
assert "".join(c["text"] for c in timeline["subtitles"]).replace(" ", "") == "我们出发吧前方就是目标"
report = api.get(f"/api/projects/{pid}/timeline/report")["report"]
assert report["ready"] is True and report["segments"] == 2 and report["voice_clips"] == 2
assert not [i for i in report["issues"] if i["level"] == "error"]
timelines = api.get(f"/api/projects/{pid}/timelines")["timelines"]
assert [t["version"] for t in timelines] == [2, 1] and [t["is_current"] for t in timelines] == [True, False]
def test_timeline_edits_recompute_timing_order_and_subtitle_files(api, studio, script_text, make_image_png):
pid, shot_ids = _prepare(api, studio, script_text, make_image_png)
timeline = api.post(f"/api/projects/{pid}/timeline", json={})["result"]["timeline"]
first, second = timeline["segments"]
baseline = timeline["total_duration_ms"]
# 1) 叠化转场以重叠方式压缩总时长,后一片段起点自动前移
patched = api.patch(f"/api/projects/{pid}/timeline/segments/{first['id']}",
json={"transition_out": "dissolve", "transition_duration_ms": 500,
"volume": 0.8})["result"]
after = patched["timeline"]
assert patched["recompute"]["total_duration_ms"] == baseline - 500
assert after["segments"][0]["volume"] == 0.8
assert after["segments"][1]["start_ms"] == after["segments"][0]["duration_ms"] - 500
assert after["total_duration_ms"] == patched["recompute"]["total_duration_ms"]
api.patch(f"/api/projects/{pid}/timeline/segments/{first['id']}",
json={"transition_out": "not_a_transition"}, expect=400)
# 2) 拖拽排序 → 顺序、起点、配音与字幕全部重排
reordered = api.post(f"/api/projects/{pid}/timeline/reorder",
json={"shot_ids": list(reversed(shot_ids))})["result"]
assert [s["shot_id"] for s in reordered["timeline"]["segments"]] == list(reversed(shot_ids))
assert [s["order_index"] for s in reordered["timeline"]["segments"]] == [1, 2]
assert reordered["timeline"]["segments"][0]["start_ms"] == 0
assert sorted(c["start_ms"] for c in reordered["timeline"]["audio_clips"] if c["kind"] == "voiceover") == [
160, reordered["timeline"]["segments"][1]["start_ms"] + 160]
# 3) 人工改写字幕 → 可下载 srt / ass
api.post(f"/api/projects/{pid}/timeline/subtitles",
json={"shot_id": shot_ids[0], "text": "目标坐标已锁定", "speaker": "林澈"})
cue = next(c for c in api.get(f"/api/projects/{pid}/timeline")["timeline"]["subtitles"]
if c["shot_id"] == shot_ids[0] and c["text"] == "目标坐标已锁定")
assert cue["start_ms"] >= 0 and cue["end_ms"] > cue["start_ms"]
cues_of_shot = [c for c in api.get(f"/api/projects/{pid}/timeline")["timeline"]["subtitles"]
if c["shot_id"] == shot_ids[0]]
assert [c["text"] for c in cues_of_shot] == ["目标坐标已锁定"] # 人工接管后不再残留自动切分的旧字幕
assert cues_of_shot[0]["end_ms"] <= timeline["total_duration_ms"]
srt = api.get(f"/api/projects/{pid}/timeline/subtitles", params={"fmt": "srt"})
assert "目标坐标已锁定" in srt.text and "-->" in srt.text and "林澈:" in srt.text
ass = api.get(f"/api/projects/{pid}/timeline/subtitles", params={"fmt": "ass"})
assert "[Script Info]" in ass.text and "Dialogue:" in ass.text and "目标坐标已锁定" in ass.text
api.get(f"/api/projects/{pid}/timeline/subtitles", params={"fmt": "vtt"}, expect=400)
# 4) 追加配乐音轨(循环铺满全片 + 闪避配音)
wav = _make_wav(3.0)
added = api.post(f"/api/projects/{pid}/timeline/audio",
files={"file": ("bgm.wav", wav, "audio/wav")},
data={"kind": "bgm", "label": "背景音乐", "loop": "true",
"gain_db": "-18", "ducking": "true"})["result"]
clips = [c for c in added["timeline"]["audio_clips"] if c["kind"] == "bgm"]
assert len(clips) == 1 and clips[0]["id"] == added["clip_id"]
assert clips[0]["loop"] is True and clips[0]["ducking"] is True and clips[0]["gain_db"] == -18.0
assert clips[0]["source_url"].startswith("/media/") and absolute(clips[0]["source_path"]).exists()
assert clips[0]["duration_ms"] >= 2900
# 5) 全局设置与体检报告
settings = api.patch(f"/api/projects/{pid}/timeline/settings",
json={"settings": {"bgm_gain_db": -20.0, "burn_subtitle": False,
"target_lufs": -18.0}})["result"]
assert settings["settings"]["bgm_gain_db"] == -20.0 and settings["settings"]["burn_subtitle"] is False
report = api.get(f"/api/projects/{pid}/timeline/report")["report"]
assert report["ready"] is True and report["bgm_clips"] == 1 and report["burn_subtitle"] is False
assert report["total_duration_ms"] == settings["timeline"]["total_duration_ms"]
# 回归:任何一次重算都会重排字幕,但绝不能把字幕整批清空(SQLite 主键复用踩过的坑)
assert report["subtitles"] == len(settings["timeline"]["subtitles"]) == 2
fresh = api.get(f"/api/projects/{pid}/timeline/subtitles", params={"fmt": "srt"})
# 人工改写过的字幕文案会写回镜头,重排时间轴后仍然是人工版本(另一镜保持自动文案)
assert "目标坐标已锁定" in fresh.text and "前方就是目标" in fresh.text
# 重算之后仍可继续人工接管某条镜头字幕
api.post(f"/api/projects/{pid}/timeline/subtitles",
json={"shot_id": shot_ids[0], "text": "人工改写的收尾字幕", "speaker": "林澈"})
assert "人工改写的收尾字幕" in api.get(f"/api/projects/{pid}/timeline/subtitles",
params={"fmt": "srt"}).text
def test_subtitle_cue_edit_updates_in_place(api, studio, script_text, make_image_png):
"""按 cue_id 改单条字幕:移动时间/改文案不能新增重复行,清空文案即删除。"""
pid, shot_ids = _prepare(api, studio, script_text, make_image_png)
timeline = api.post(f"/api/projects/{pid}/timeline", json={})["result"]["timeline"]
first = timeline["segments"][0]
cue = next(c for c in timeline["subtitles"] if c["shot_id"] == first["shot_id"])
others = [c for c in timeline["subtitles"] if c["shot_id"] != first["shot_id"]]
moved = api.post(f"/api/projects/{pid}/timeline/subtitles",
json={"cue_id": cue["id"], "shot_id": first["shot_id"],
"text": "这条字幕改过三次", "speaker": "林澈",
"start_ms": cue["start_ms"] + 200, "end_ms": cue["end_ms"] + 200})["result"]["cue"]
assert moved["id"] == cue["id"] and moved["start_ms"] == cue["start_ms"] + 200
of_shot = [c for c in api.get(f"/api/projects/{pid}/timeline")["timeline"]["subtitles"]
if c["shot_id"] == first["shot_id"]]
assert [c["id"] for c in of_shot] == [cue["id"]] # 没有多出重复字幕
# 单条字幕的镜头:人工文案写回镜头,重排时间轴后仍是人工版本
assert api.get(f"/api/projects/{pid}/shots/{first['shot_id']}")["shot"]["subtitle_text"] == "这条字幕改过三次"
after = api.patch(f"/api/projects/{pid}/timeline/segments/{first['id']}",
json={"duration_ms": first["duration_ms"] - 200})["result"]["timeline"]
assert [c["text"] for c in after["subtitles"] if c["shot_id"] == first["shot_id"]] == ["这条字幕改过三次"]
# 2) 清空文案 → 删除该条字幕,其余字幕不动
api.post(f"/api/projects/{pid}/timeline/subtitles",
json={"cue_id": cue["id"], "shot_id": first["shot_id"], "text": ""})
remaining = api.get(f"/api/projects/{pid}/timeline")["timeline"]["subtitles"]
assert all(c["id"] != cue["id"] for c in remaining)
assert len(remaining) == len(others)
# 3) 未知 cue / 不属于该时间线的 cue → 400
api.post(f"/api/projects/{pid}/timeline/subtitles",
json={"cue_id": 999_999, "shot_id": shot_ids[0], "text": "x"}, expect=400)
def test_compose_blocked_when_timeline_has_missing_media(api, studio, script_text, make_image_png):
pid, _ = _prepare(api, studio, script_text, make_image_png)
api.post(f"/api/projects/{pid}/timeline", json={})
api.post(f"/api/projects/{pid}/compose", json={"preset": "not_a_preset"}, expect=400)
api.post(f"/api/projects/{pid}/timeline/audio",
data={"kind": "bgm", "label": "丢失的配乐",
"source_path": f"media/{pid}/assets/does-not-exist.mp3"})
report = api.get(f"/api/projects/{pid}/timeline/report")["report"]
assert report["ready"] is False
errors = [i["message"] for i in report["issues"] if i["level"] == "error"]
assert any("丢失的配乐" in m for m in errors)
blocked = api.post(f"/api/projects/{pid}/compose", json={"preset": "draft_720p"}, expect=409)
assert "阻塞" in blocked["detail"]
assert api.get(f"/api/projects/{pid}/exports")["exports"] == []
def test_compose_fails_loudly_when_burn_subtitle_requested_without_filter(
api, studio, script_text, make_image_png):
"""本机缺字幕滤镜时,显式要求烧录必须报错,绝不静默产出无字幕成片。"""
if ff.subtitle_filter():
import pytest
pytest.skip("本机 ffmpeg 支持字幕烧录,无需验证降级路径")
pid, _ = _prepare(api, studio, script_text, make_image_png)
api.post(f"/api/projects/{pid}/timeline", json={})
result = api.post(f"/api/projects/{pid}/compose",
json={"preset": "draft_720p", "burn_subtitle": True})["result"]
assert result["exports"] == []
task = api.get(f"/api/tasks/{result['task_id']}")["task"]
assert task["status"] == "failed" and "字幕" in task["error_message"]
assert api.get(f"/api/projects/{pid}/exports")["count"] == 0
actions = {e["action"] for e in api.get(f"/api/projects/{pid}/events")["events"]}
assert "compose.failed" in actions
def test_compose_exports_final_film_and_download(api, studio, script_text, make_image_png):
pid, _ = _prepare(api, studio, script_text, make_image_png)
built = api.post(f"/api/projects/{pid}/timeline",
json={"settings": {"burn_subtitle": True}})["result"]["timeline"]
api.patch(f"/api/projects/{pid}/timeline/segments/{built['segments'][0]['id']}",
json={"transition_out": "dissolve", "transition_duration_ms": 400})
api.post(f"/api/projects/{pid}/timeline/subtitles",
json={"shot_id": built["segments"][0]["shot_id"],
"text": "跃迁开始", "speaker": "林澈"})
api.post(f"/api/projects/{pid}/timeline/subtitles",
json={"text": "星海纪元", "speaker": "旁白", "start_ms": 0, "end_ms": 900})
api.post(f"/api/projects/{pid}/timeline/audio",
files={"file": ("bgm.wav", _make_wav(3.0), "audio/wav")},
data={"kind": "bgm", "label": "背景音乐", "loop": "true", "ducking": "true"})
total_ms = api.get(f"/api/projects/{pid}/timeline")["timeline"]["total_duration_ms"]
result = api.post(f"/api/projects/{pid}/compose",
json={"preset": "draft_720p", "name": "星海纪元·预告",
"burn_subtitle": False})["result"]
assert result["drained"]["timed_out"] is False and len(result["exports"]) == 1
export = result["exports"][0]
assert export["status"] == "succeeded", export["error_message"]
assert export["duration_ms"] and abs(export["duration_ms"] - total_ms) / total_ms < 0.06
assert export["size_bytes"] > 20_000 and export["width"] >= 854 and export["height"] % 2 == 0
assert abs(export["width"] / export["height"] - 16 / 9) < 0.03
assert export["fps"] == 24 and len(export["checksum"]) == 32
assert all(c in "0123456789abcdef" for c in export["checksum"])
assert export["url"].startswith("/media/") and absolute(export["path"]).exists()
assert export["subtitle_burned"] is False
# 渲染面二次校验:真实 ffprobe + 下载接口
probed = ff.probe(absolute(export["path"]))
assert abs(probe_ms(probed) - export["duration_ms"]) <= 250
assert probed.width == export["width"] and probed.height == export["height"]
fetched = api.get(f"/api/exports/{export['id']}/download")
assert fetched.headers["content-type"].startswith("video/mp4")
assert len(fetched.content) == export["size_bytes"]
assert api.get(f"/api/exports/{export['id']}")["export"]["name"] == "星海纪元·预告"
# 导出完成 → 项目进入交付态,流程条 export 节点点亮
summary = api.get(f"/api/projects/{pid}/summary")["summary"]
assert summary["status"] == "delivered" and summary["current_stage"] == "export"
workflow = {n["key"]: n for n in summary["workflow"]}
assert workflow["compose"]["state"] == "done" and workflow["export"]["state"] == "done"
assert len(summary["stats"]["exports"]) == 1 and summary["stats"]["exports"][0]["status"] == "succeeded"
assert summary["stats"]["latest_export"]["id"] == export["id"]
compose_task = api.get(f"/api/tasks/{result['task_id']}")["task"]
assert compose_task["status"] == "succeeded" and compose_task["output_url"].startswith("/media/")
assert "compose.succeeded" in {e["action"] for e in api.get(f"/api/projects/{pid}/events")["events"]}
def test_compose_vertical_preset_keeps_9_16_canvas(api, studio, script_text, make_image_png):
pid = studio["project_id"]
api.patch(f"/api/projects/{pid}", json={"aspect_ratio": "9:16", "resolution": "720p"})
pid, _ = _prepare(api, studio, script_text, make_image_png)
timeline = api.post(f"/api/projects/{pid}/timeline", json={})["result"]["timeline"]
# 母版画布 = 交付画布。以前竖屏把档位号当长边用(720p → 404x720),
# 于是模型原生出的 736x1312 先被砍成 404x720,再放大回 720x1280 导出,
# 有效细节只剩四分之一——这条断言当时钉住的正是那个 bug。
assert (timeline["width"], timeline["height"]) == (720, 1280)
result = api.post(f"/api/projects/{pid}/compose",
json={"preset": "vertical_720p", "burn_subtitle": False})["result"]
assert result["drained"]["timed_out"] is False and len(result["exports"]) == 1
export = result["exports"][0]
assert export["status"] == "succeeded", export["error_message"]
assert (export["width"], export["height"]) == (720, 1280)
probed = ff.probe(absolute(export["path"]))
assert (probed.width, probed.height) == (720, 1280)
def test_canvas_uses_short_edge_as_the_tier():
"""档位号 = 短边,横竖同一套规则,且必须和导出预设咬合。
竖屏分支曾经算出 404x720:时间线母版、关键帧、喂给视频模型的首帧
全部跟着缩水,而 `vertical_720p` 预设一直是 720x1280,
两边自相矛盾,成片等于「缩小 3.2 倍再放大回去」。
"""
assert canvas_for("720p", "16:9") == (1280, 720)
assert canvas_for("720p", "9:16") == (720, 1280)
assert canvas_for("1080p", "9:16") == (1080, 1920)
assert canvas_for("480p", "9:16") == (480, 852)
for tier, preset in (("720p", "vertical_720p"), ("1080p", "vertical_1080p"),
("720p", "draft_720p"), ("1080p", "web_1080p")):
ratio = "9:16" if preset.startswith("vertical") else "16:9"
conf = ff.EXPORT_PRESETS[preset]
assert canvas_for(tier, ratio) == (conf["width"], conf["height"]), preset
def test_normalize_filter_picks_resampler_by_direction():
"""放大用 lanczos+轻锐化、缩小用 area:默认 bilinear 放大出来的 1080p 是一层雾。"""
up = ff.normalize_filter(1080, 1920, 24, src_size=(736, 1312))
assert "flags=lanczos" in up and "unsharp" in up
down = ff.normalize_filter(720, 1280, 24, src_size=(1312, 736))
assert "flags=area" in down and "unsharp" not in down
unknown = ff.normalize_filter(720, 1280, 24)
assert "flags=lanczos" in unknown # 拿不到源尺寸时不退回 bilinear
def test_compose_degrades_to_external_subtitle_file(api, studio, script_text, make_image_png):
"""时间线要求烧字幕,但本机能力不足 → 按能力降级(内嵌 → 外挂)并留痕,成片仍导出成功。
本机 ffmpeg 带 libass 时烧录成功、无降级事件;缺 libass 时降到内嵌软字幕轨
(内嵌也不行再降到外挂 SRT),两种情况下都必须留下字幕并记录 compose.subtitle_skipped。
"""
pid, _ = _prepare(api, studio, script_text, make_image_png)
api.post(f"/api/projects/{pid}/timeline", json={"settings": {"subtitle_mode": "burn"}})
result = api.post(f"/api/projects/{pid}/compose", json={"preset": "draft_720p"})["result"]
assert len(result["exports"]) == 1
export = result["exports"][0]
assert export["status"] == "succeeded", export["error_message"]
expected = bool(ff.subtitle_filter())
assert export["subtitle_burned"] is expected
actions = [e["action"] for e in api.get(f"/api/projects/{pid}/events")["events"]]
assert ("compose.subtitle_skipped" in actions) is (not expected)
if not expected: # 降级路径必须真的把字幕留在成片里(内嵌轨)而不是直接丢掉
assert ff.probe(absolute(export["path"])).subtitle_tracks == [] or \
ff.can_embed_subtitles(".mp4")
assert export["size_bytes"] > 20_000
sidecar = absolute(export["path"]).with_suffix(".srt")
assert sidecar.exists() and "-->" in sidecar.read_text(encoding="utf-8")
def probe_ms(probed) -> int:
return int(round(probed.duration * 1000))