nex_animation/backend/tests/test_subtitle_modes.py

210 lines
12 KiB
Python
Raw Normal View History

2026-11-01 08:18:07 +00:00
"""字幕交付能力:能力感知选择 ffmpeg + 字幕方式(内嵌软字幕轨 / 烧录 / 外挂)折算与落地。
覆盖三件事:
1) 二进制解析:Homebrew 精简 bottle 缺 libass 时,必须自动改用带 libass 的完整版构建;
2) 字幕方式折算:请求 > 时间线 > 老版本 burn_subtitle,且能力不足时逐级降级不静默;
3) 真实成片:默认内嵌 mov_text 软字幕轨(语言标记 chi),both 模式同时烧进画面。
"""
from __future__ import annotations
from pathlib import Path
import pytest
from app.services import compose
from app.services import ffmpeg as ff
from tests.conftest import absolute
from tests.test_delivery import _prepare
# ---------------------------------------------------------------- 二进制与能力
def test_ffmpeg_binary_resolution_prefers_libass():
"""PATH 上的构建缺 libass 时,必须挑到候选目录里带 libass 的那个(或明确降级可用)。"""
assert ff.available(), "本机应至少有一个可用的 ffmpeg/ffprobe"
assert ff.FFMPEG and Path(ff.FFMPEG).is_file()
caps = ff.capabilities()
for key in ("subtitle_burn", "subtitle_embed", "subtitle_codec", "libass", "ffmpeg_path",
"ffmpeg_version", "drawtext"):
assert key in caps, f"capabilities 缺少字段 {key}"
assert caps["ffmpeg_path"] == ff.FFMPEG
# 内嵌字幕只依赖 mov_text(几乎所有构建都有),必须可用
assert ff.can_embed_subtitles(".mp4") is True and ff.subtitle_codec(".mp4") == "mov_text"
assert ff.can_embed_subtitles(".webm") == ("webvtt" in ff.list_encoders())
if ff.can_embed_subtitles(".mkv"):
assert "mov_text" in {ff.subtitle_codec(x) for x in (".mp4", ".mov", ".mkv")}
# 选中的二进制自身能力必须与 capabilities 口径一致
assert caps["subtitle_burn"] == bool(ff.subtitle_filter())
assert (ff.subtitle_filter() in ff.list_filters()) or not ff.subtitle_filter()
def test_drawtext_available_on_resolved_binary():
"""中文字幕样式(drawtext)在解析出的二进制上必须可用,否则 ASS 烧录会缺字体渲染。"""
if not ff.can_draw_text():
pytest.skip("本机所有 ffmpeg 构建都没有 drawtext")
assert ff.find_font(), "需要系统中文字体才能烧录"
# ---------------------------------------------------------------- 烧录字体
_ASS_TMPL = """[Script Info]
ScriptType: v4.00+
PlayResX: 640
PlayResY: 200
ScaledBorderAndShadow: yes
[V4+ Styles]
Format: Name, Fontname, Fontsize, PrimaryColour, SecondaryColour, OutlineColour, BackColour, Bold, Italic, Underline, StrikeOut, ScaleX, ScaleY, Spacing, Angle, BorderStyle, Outline, Shadow, Alignment, MarginL, MarginR, MarginV, Encoding
Style: Default,{family},54,&H00FFFFFF,&H00FFFFFF,&H00101010,&H96000000,-1,0,0,0,100,100,0,0,1,3,1,2,20,20,40,1
[Events]
Format: Layer, Start, End, Style, Name, MarginL, MarginR, MarginV, Effect, Text
Dialogue: 0,0:00:00.00,0:00:05.00,Default,,0,0,0,,深夜面馆,灯给你留着
"""
def _render_text(sample: str, family: str) -> str:
"""把一句话用指定字体烧到纯色画面上,返回原始像素(用于逐像素比对)。"""
ass = Path(sample)
ass.write_text(_ASS_TMPL.format(family=family), encoding="utf-8")
out = ass.with_suffix(".rgb")
ff.run([ff.FFMPEG, "-v", "error", "-y", "-f", "lavfi", "-i",
"color=c=0x22303a:s=640x200:d=1", "-vf", f"ass={ff.escape_filter_path(ass)}",
"-frames:v", "1", "-f", "rawvideo", "-pix_fmt", "rgb24", str(out)], timeout=120)
return out.read_bytes()
def test_ass_fontname_is_family_not_path():
"""ASS 的 Fontname 必须是字体**族名**:写成文件路径时 libass 找不到字体、
静默回退成无中文字形的默认字体,成片里就是一排豆腐块(本轮真实踩过)。"""
font = ff.subtitle_font()
assert font["family"], "本机没有可用于中文烧录的字体"
assert not Path(font["family"]).suffix, f"Fontname 不能是文件路径:{font['family']}"
catalog = {f for f, _ in ff.cjk_font_catalog()}
assert not catalog or font["family"] in catalog, "选中的字体不在 fontconfig 的中文字形清单里"
# 老配置里存的是字体文件路径,也必须折算成族名
if font["path"]:
again = ff.subtitle_font(font["path"])
assert again["family"] == font["family"] or not Path(again["family"]).suffix
def test_write_ass_header_uses_family(tmp_path):
cues = [{"start_ms": 0, "end_ms": 1000, "text": "深夜面馆,灯给你留着", "speaker": "林秀云"}]
out = ff.write_ass(cues, tmp_path / "s.ass", width=1280, height=720)
style = next(line for line in out.read_text(encoding="utf-8").splitlines()
if line.startswith("Style: Default"))
family = style.split(",")[1]
assert family and not family.lower().endswith((".ttf", ".ttc", ".otf"))
def test_fontconfig_charset_query_finds_cjk(tmp_path):
""":charset=4e2d 是我们判定「这个字体能不能画中文」的唯一依据,必须真有结果。"""
rows = ff.cjk_font_catalog()
assert rows, "fontconfig 查不到含中文字形的字体,烧录一定出豆腐块"
assert all(Path(p).exists() for _, p in rows[:5])
assert all(not f.startswith(".") for f, _ in rows), "内部名(.xxx Interface)画不出字"
def test_burn_render_draws_real_glyphs(tmp_path):
"""端到端证明「画出来的是汉字」:同一句中文分别用选中的中文字体、
一个不含中文的拉丁字体、一个不存在的字体名渲染。
若中文字体的像素和后两者之一完全相同,说明 libass 根本没用到中文字形。"""
if not ff.can_burn_subtitles() or not ff.subtitle_font()["family"]:
pytest.skip("本机不支持字幕烧录或缺少中文字体")
family = ff.subtitle_font()["family"]
cjk = _render_text(tmp_path / "a.ass", family)
latin = _render_text(tmp_path / "b.ass", "Verdana")
bogus = _render_text(tmp_path / "c.ass", "NoSuchFont 99")
assert cjk != latin, f"{family} 渲染结果与拉丁字体一致 → 中文没被真正渲染"
assert cjk != bogus, f"{family} 渲染结果与未知字体一致 → 中文没被真正渲染"
# ---------------------------------------------------------------- 字幕方式折算
def test_subtitle_mode_precedence():
conf_embed = {"subtitle_mode": "embed", "burn_subtitle": True}
assert compose.resolve_subtitle_mode(conf_embed)["mode"] == "embed"
# 请求显式 subtitle_mode 覆盖时间线
assert compose.resolve_subtitle_mode(conf_embed, subtitle_mode="burn")["mode"] == "burn"
# 老版本只有 burn_subtitle 布尔时保持原语义(向后兼容)
assert compose.resolve_subtitle_mode({"burn_subtitle": True})["mode"] == "burn"
assert compose.resolve_subtitle_mode({"burn_subtitle": False})["mode"] == "sidecar"
# 请求里的 burn_subtitle 优先于时间线 subtitle_mode
assert compose.resolve_subtitle_mode(conf_embed, burn_subtitle=True)["mode"] == "burn"
# 非法值回落到时间线老字段,坏输入不会让渲染报错
assert compose.resolve_subtitle_mode({"subtitle_mode": "????", "burn_subtitle": False})["mode"] == "sidecar"
def test_subtitle_mode_downgrades_by_capability(monkeypatch):
"""缺 libass 时 burn 自动降级为内嵌;两者都缺才降到外挂(并标记可报错)。"""
monkeypatch.setattr(ff, "can_burn_subtitles", lambda: False)
monkeypatch.setattr(ff, "can_embed_subtitles", lambda container=".mp4": True)
down = compose.resolve_subtitle_mode({}, burn_subtitle=True)
assert down["mode"] == "embed" and down["downgraded"] and down["explicit"]
assert down["burn"] is False and down["embed"] is True
monkeypatch.setattr(ff, "can_embed_subtitles", lambda container=".mp4": False)
last = compose.resolve_subtitle_mode({}, burn_subtitle=True)
assert last["mode"] == "sidecar" and last["downgraded"] and last["explicit"]
assert last["burn_available"] is False and last["embed_available"] is False
monkeypatch.setattr(ff, "can_burn_subtitles", lambda: True)
monkeypatch.setattr(ff, "can_embed_subtitles", lambda container=".mp4": False)
assert compose.resolve_subtitle_mode({"subtitle_mode": "both"})["mode"] == "burn"
monkeypatch.setattr(ff, "can_burn_subtitles", lambda: False)
monkeypatch.setattr(ff, "can_embed_subtitles", lambda container=".mp4": True)
assert compose.resolve_subtitle_mode({"subtitle_mode": "both"})["mode"] == "embed"
# 没有字幕句时不产生「降级」噪音
assert compose.resolve_subtitle_mode({}, has_subtitles=False)["downgraded"] is False
# ---------------------------------------------------------------- 真实成片
def test_compose_embeds_soft_subtitle_track_by_default(api, studio, script_text, make_image_png):
"""默认字幕方式 = 内嵌软字幕轨:成片必须带一条 mov_text 字幕轨,语言标记 chi,画面不糊字。"""
pid, _ = _prepare(api, studio, script_text, make_image_png)
api.post(f"/api/projects/{pid}/timeline", json={})
report = api.get(f"/api/projects/{pid}/timeline/report")["report"]
assert report["subtitle_mode"] == "embed" and report["subtitles"] > 0
result = api.post(f"/api/projects/{pid}/compose", json={"preset": "draft_720p"})["result"]
export = result["exports"][0]
assert export["status"] == "succeeded", export["error_message"]
# 内嵌字幕轨绝不能把成片截短(-shortest 与 SRT 流长度冲突的回归点)
total_ms = report["total_duration_ms"]
assert abs(export["duration_ms"] - total_ms) / total_ms < 0.06, export["error_message"]
assert "偏差" not in (export["error_message"] or "")
assert export["subtitle_burned"] is False # 内嵌模式不烧进画面
probed = ff.probe(absolute(export["path"]))
assert [t["codec_name"] for t in probed.subtitle_tracks] == ["mov_text"]
assert probed.subtitle_tracks[0]["language"] == "chi"
sidecar = Path(absolute(export["path"])).with_suffix(".srt")
assert sidecar.exists() and "-->" in sidecar.read_text(encoding="utf-8")
body = api.get(f"/api/exports/{export['id']}")["export"]
assert body["status"] == "succeeded"
def test_compose_both_mode_burns_and_embeds(api, studio, script_text, make_image_png):
"""both:字幕既烧进画面(硬字幕)又带一条可开关的软字幕轨。"""
if not ff.can_burn_subtitles():
pytest.skip("本机 ffmpeg 缺 libass,无法验证烧录")
pid, _ = _prepare(api, studio, script_text, make_image_png)
api.post(f"/api/projects/{pid}/timeline", json={"settings": {"subtitle_mode": "both"}})
result = api.post(f"/api/projects/{pid}/compose", json={"preset": "draft_720p"})["result"]
export = result["exports"][0]
assert export["status"] == "succeeded", export["error_message"]
assert export["subtitle_burned"] is True
probed = ff.probe(absolute(export["path"]))
assert len(probed.subtitle_tracks) == 1 and probed.subtitle_tracks[0]["codec_name"] == "mov_text"
actions = [e["action"] for e in api.get(f"/api/projects/{pid}/events")["events"]]
assert "compose.subtitle_skipped" not in actions
def test_compose_off_mode_emits_no_subtitle_files(api, studio, script_text, make_image_png):
"""off:成片无字幕轨,也不再产出外挂 SRT(交付纯净版)。"""
pid, _ = _prepare(api, studio, script_text, make_image_png)
api.post(f"/api/projects/{pid}/timeline", json={"settings": {"subtitle_mode": "off"}})
result = api.post(f"/api/projects/{pid}/compose", json={"preset": "draft_720p"})["result"]
export = result["exports"][0]
assert export["status"] == "succeeded", export["error_message"]
assert export["subtitle_burned"] is False
out = Path(absolute(export["path"]))
assert ff.probe(out).subtitle_tracks == []
assert not out.with_suffix(".srt").exists()