nex_animation/backend/tests/test_speaking_subject.py

226 lines
12 KiB
Python
Raw 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.

"""有台词镜头的「谁来出声」必须由画面自己说清楚。
现场证据(项目 11《告诉你个小秘密第1期》shot 36):首帧是一张两个人都闭着嘴
微笑的全景,H3 在首帧里找不到出声者,于是在镜头开头即兴补了 2.5 秒无源人声
(观众听到的「每段视频前面那几句莫名其妙的话」),中段还把机位切到另一张脸上,
台词的说话人对不上。对照 shot 35:首帧里紫小慧张嘴奔跑 → 开头静音、说话人正确。
因此锁三件事:
① 联合 caption 里必须把「说话人正面朝向镜头、嘴部张开」写成看得见的事实,
且这句话本身不含任何会被念出来的声音/指令词;
② 首帧(keyframe)提示词同样带上这条构图约束,页面上手填提示词的分支也不例外;
③ 人工点「重新生成」要真的换种子——提示词一字不改时也必须重新采样。
"""
from __future__ import annotations
from types import SimpleNamespace
from app.constants import AUDIO_TOPIC_MARKERS, META_INSTRUCTION_MARKERS
from app.models import Character, Project, Scene, Shot
from app.services import prompt as P
from app.services import storage
ROLES = [{"index": 1, "owner_type": "frame", "owner_id": None, "name": "画面"},
{"index": 2, "owner_type": "human", "owner_id": None, "name": "俊杰"},
{"index": 3, "owner_type": "human", "owner_id": None, "name": "紫小慧"},
{"index": 4, "owner_type": "scene", "owner_id": None, "name": "场景"}]
def _case(db, *, dialogue: str = "不要着急,恢复系统和数据一键就能完成。"):
project = Project(name="自检-出声主体", aspect_ratio="9:16", resolution="720p", fps=24,
style_guide="办公室轻松风格", default_provider="comfyui", target_duration=20)
db.add(project)
db.flush()
male = Character(project_id=project.id, name="俊杰", gender="male", appearance="高大,和蔼")
female = Character(project_id=project.id, name="紫小慧", gender="female",
appearance="年轻东亚女生,精致淡妆")
scene = Scene(project_id=project.id, name="办公室", prompt="现代简约的办公室,明亮整洁")
db.add_all([male, female, scene])
db.flush()
shot = Shot(project_id=project.id, order_index=1, code="S01", title="俊杰解答",
description="俊杰坐在工位上,从容自信地微笑",
duration=8, shot_size="close_up", camera_angle="eye_level",
camera_movement="static", mode="i2v", status="ready",
dialogue=dialogue, speaker_character_id=male.id, ambience="办公室环境底噪")
shot.characters = [male, female]
shot.scenes = [scene]
db.add(shot)
db.flush()
roles = [dict(ROLES[0]), dict(ROLES[1], owner_id=male.id),
dict(ROLES[2], owner_id=female.id), dict(ROLES[3], owner_id=scene.id)]
return project, shot, roles
def test_joint_caption_has_no_speaking_sentence_besides_the_dialogue(db):
"""台词前面一个字都不许多:shot 36 四轮乱语 vs shot 35 开头的干净对照。
H3 会把台词前那句「主语 + 谓语」的描述当台词先念出来(take2/3/4 全部命中,
0~2.1s 的音节数与那句一一对应),而结构相同的 shot 35 台词前面只有
「中景,膝盖以上,平视视角,横向移镜跟随」这种名词短语 → 开头干净。
所以联合 caption 里唯一关于「谁出声」的文字,就是官方 ``<d>`` 段落本身
(说话人靠 ``<Audio n>`` 绑参考音色,见 test_joint_caption.py)。
"""
project, shot, roles = _case(db)
text = P.compose_prompt(project, shot, db=db, joint_av=True, roles=roles, duration_seconds=8)
assert "俊杰位于前景正中" not in text, text
assert text.count("<d>") == 1, text
# 紧贴台词的那一段必须是名词短语(环境声/镜头语言),不是「谁在哪、在做什么」的描述句
# joint 专用极简镜头语言:首帧关键帧已经锁死构图,「画面稳定」「膝盖以上」
# 这种解释性尾巴只会被念出来(S03 实测开头乱语里就有「中景,膝盖」)。
assert "固定机位" in text and "画面稳定" not in text and "位于" not in text, text
prelude = [x for x in text.split("<d>")[0].split("。") if x][-1]
assert "底噪" in prelude, f"台词前多了一段可被朗读的句子:{prelude}"
assert text.endswith("</d>"), f"台词必须收尾,后面不能再有内容:{text}"
assert "嘴" not in text and "唇" not in text, text
def test_shot_without_dialogue_has_no_speaking_lock(db):
project, shot, roles = _case(db, dialogue="")
assert P.speaking_subject_note(shot) == ""
text = P.compose_prompt(project, shot, db=db, joint_av=True, roles=roles, duration_seconds=7)
assert "嘴部张开" not in text and "嘴" not in text
def test_speaking_lock_can_be_turned_off_for_voiceover(db):
"""画外音/背影镜头:说话人本来就不该露脸张嘴。"""
project, shot, _ = _case(db)
shot.params_json = {**(shot.params_json or {}), "speaking_lock": False}
assert P.speaking_subject_note(shot) == "" and P.keyframe_speech_note(shot) == ""
def test_multi_speaker_shot_locks_every_speaker(db):
project, shot, _ = _case(db)
shot.set_lines([{"text": "电脑怎么了?", "character_id": shot.characters[1].id},
{"text": "一键就能恢复。", "character_id": shot.characters[0].id}])
db.flush()
note = P.speaking_subject_note(shot)
assert "紫小慧、俊杰依次正面朝向镜头" in note, note
# 首帧定格在镜头开头:画的是第一个开口的人
assert P.keyframe_speech_note(shot).startswith("紫小慧是本帧画面的主体")
# 文字通道:两个说话人只体现在两条 <d> 段落上,不额外加描述句
roles = [dict(r) for r in ROLES]
text = P.compose_prompt(project, shot, db=db, joint_av=True, roles=roles, duration_seconds=8)
assert "<d>电脑怎么了?</d>" in text, text
assert "<d>一键就能恢复。</d>" in text, text
assert text.count("<d>") == 2, text
assert "依次正面朝向镜头" not in text, text
def test_keyframe_prompt_names_the_speaking_subject(db, project_fixture=None):
from app.jobs import handlers
project = Project(name="自检-首帧", aspect_ratio="9:16", resolution="720p", fps=24,
default_provider="comfyui", target_duration=20)
db.add(project)
db.flush()
male = Character(project_id=project.id, name="俊杰", gender="male", appearance="高大")
scene = Scene(project_id=project.id, name="办公室", prompt="现代简约的办公室")
db.add_all([male, scene])
db.flush()
shot = Shot(project_id=project.id, order_index=1, code="S01", title="解答",
description="俊杰坐在工位上", duration=8, shot_size="close_up",
mode="i2v", status="ready", dialogue="一键就能恢复。",
speaker_character_id=male.id)
shot.characters = [male]
shot.scenes = [scene]
db.add(shot)
db.flush()
for custom in ("", "俊杰坐在工位上微笑"):
text = handlers._image_asset_prompt(db, project, shot, "shot", "keyframe", custom)
assert "嘴部微张" in text and "俊杰是本帧画面的主体" in text, (custom, text)
# 尾帧/参考图不该被这条约束污染
tail = handlers._image_asset_prompt(db, project, shot, "shot", "last_frame", "俊杰点头")
assert "嘴部微张" not in tail
def test_force_regenerate_actually_rerolls_the_seed(db):
"""人工重摇时提示词常常一字未改:种子必须照样换,否则供应商命中缓存返回同一个文件。"""
from app.providers import comfyui
from app.services import dispatch
project, shot, _ = _case(db)
out = storage.project_dir(project.id, "generated") / "shot-01-old.mp4"
out.parent.mkdir(parents=True, exist_ok=True)
out.write_bytes(b"x")
shot.output_path = storage.to_relative(out)
db.flush()
before = P.shot_idempotency_key(shot)
prompt_text = shot.prompt or P.compose_prompt(project, shot, db=db)
seed_before = comfyui.derive_video_seed(
SimpleNamespace(seed=None, idempotency_key=before, prompt=prompt_text))
task = dispatch._new_generation_task(db, project, shot, force=True)
assert task.id
after = P.shot_idempotency_key(db.get(Shot, shot.id))
assert after != before and "take2" in after, after
seed_after = comfyui.derive_video_seed(
SimpleNamespace(seed=None, idempotency_key=after, prompt=prompt_text))
assert seed_after != seed_before, "重摇必须真正换种子,否则拿回的是缓存里的同一镜"
# 不重摇(自动续跑/失败重试)时轮次不变,同一提示词仍然可复现
again = dispatch._new_generation_task(db, project, db.get(Shot, shot.id), force=False)
assert again is not None
assert P.shot_idempotency_key(db.get(Shot, shot.id)) == after
def test_the_speaking_lock_never_mentions_the_mouth(db):
"""「嘴部张开/闭合」本身就是会被念出来的句子——shot 36 的最后一轮乱语。
词级 ASR:0.0~1.28s 8 个音节、1.28~1.40s 停顿、1.40~2.44s 7 个音节,
与旧约束「俊杰正面朝向镜头,嘴部张开,是画面的主体」的两个短句逐字对齐,
真台词直到 2.68s 才出口;shot 35 的 caption 一个「嘴」字都没有,开头静音。
⇒ 「谁在张嘴」只能画进首帧,写进 H3 的文字就会被念出来。
"""
project, shot, _ = _case(db)
note = P.speaking_subject_note(shot)
for word in ("嘴", "唇", "口", "闭合", "合拢"):
assert word not in note, f"约束里又出现了发声器官词「{word}」:{note}"
for word in (*AUDIO_TOPIC_MARKERS, *META_INSTRUCTION_MARKERS):
assert word not in note, f"{word} -> {note}"
# 非说话人只给位置和朝向,不给任何脸部描述,免得模型安排一问一答
assert "紫小慧位于侧后方,面朝桌面" in note, note
# 这句话只属于图像通道:首帧可以画「嘴部微张」,H3 的文字通道一个字都不加
assert "嘴部微张" in P.keyframe_speech_note(shot)
def test_conversational_actions_are_stripped_from_dialogue_shots(db):
"""动作句里写「谁在听着」「做解释的手势」=请模型自己加一句台词。"""
project, shot, roles = _case(db)
shot.description = ("俊杰坐在工位前,正对镜头,一只手轻点桌面做解释的手势,"
"紫小慧在画面侧后方的背景里听着")
db.flush()
text = P.compose_prompt(project, shot, db=db, joint_av=True, roles=roles, duration_seconds=8)
assert "听着" not in text and "解释" not in text, text
assert "坐在工位前,正对镜头" in text and "轻点桌面" not in text, text
assert "<d>不要着急" in text, text # 真台词一个字不少
# 无台词镜头不过滤:那里的动作短语不涉及「谁开口」,删了反而丢内容
other = Shot(project_id=project.id, order_index=2, code="S02", title="演示",
description="紫小慧在一旁看着屏幕,俊杰在背景里听着", duration=7,
shot_size="medium", mode="i2v", status="ready")
other.characters = list(shot.characters)
other.scenes = list(shot.scenes)
db.add(other)
db.flush()
plain = P.compose_prompt(project, other, db=db, joint_av=True, roles=roles,
duration_seconds=7)
# 名字被 <Picture n> 标签就地吸收,这里只校验短语本身保留
assert "在背景里听着" in plain, plain
def test_mouth_idioms_are_rewritten_not_dropped(db):
"""表情写法里的「嘴角」不是声音内容,改写而不是整条删除(shot 36 take2 丢了笑意)。
反过来,真的在描述发声器官的句子(「嘴唇微张」)照旧删掉——那是会被念出来的。
"""
project, shot, roles = _case(db)
shot.description = "俊杰坐在工位前,正对镜头,嘴角带笑,嘴唇微张;紫小慧在侧后方"
db.flush()
text = P.compose_prompt(project, shot, db=db, joint_av=True, roles=roles, duration_seconds=8)
assert "面带笑意" in text, text
assert "嘴角" not in text and "唇" not in text, text
assert P.sanitize_visual("俊杰嘴角上扬,嘴唇微张,神情从容") == "俊杰面带笑意,神情从容"