195 lines
8.4 KiB
Python
195 lines
8.4 KiB
Python
"""已有原生镜头重混音,冻结已选画面、原声和帧表,只构造声音与合成步骤。"""
|
|
|
|
from copy import deepcopy
|
|
|
|
from app.core.errors import fail
|
|
from app.core.runtime_policy import read_runtime_policy
|
|
from app.domain.continuity import compile_story_timing
|
|
from app.domain.native_av import source_audio_matches_video
|
|
from app.domain.native_dialogue import enabled as native_enabled
|
|
from app.domain.stories import digest
|
|
from app.models import Asset
|
|
from app.services.caption_revision import caption_projection, frozen_caption_recipe, revision_plan
|
|
from app.services.editor_media import assigned_utterance_ids
|
|
from app.services.orchestration.dialogue_engine import _recipe
|
|
from app.services.orchestration.editor_engine import _asset
|
|
|
|
|
|
def _local_timing(sid, shot, timing, audio, video, gate, expected_ids):
|
|
evidence = gate.get("provenance") or {}
|
|
offset = (audio.get("provenance") or {}).get("audio_offset_samples")
|
|
frames = timing["contribution_frames"]
|
|
if (
|
|
not source_audio_matches_video(audio, video)
|
|
or evidence.get("shot_id") != sid
|
|
or evidence.get("native_av_policy") != "native_av_v1"
|
|
or evidence.get("video_sha256") != video.get("sha256")
|
|
or evidence.get("audio_sha256") != audio.get("sha256")
|
|
or evidence.get("native_context") != video.get("context_hash")
|
|
or evidence.get("audio_offset_samples") != offset
|
|
):
|
|
raise ValueError("缺少已核验的同源净音轨及该候选的原声时基")
|
|
if (
|
|
timing.get("source_start_frame")
|
|
or timing.get("dedup_frames")
|
|
or video.get("duration_frames") != frames
|
|
or abs(audio["sample_count"] + offset - frames * 2000) > 600
|
|
):
|
|
raise ValueError("当前选段与已验收的完整原片、原声帧数不一致")
|
|
if shot["performance"].get("dialogue_staging") == "two_person_turns" and (
|
|
evidence.get("turn_decision") not in {"pass", "manual"}
|
|
or not evidence.get("turn_certificate_sha256")
|
|
or not evidence.get("turn_report_sha256")
|
|
):
|
|
raise ValueError("同框原片缺少匹配的逐轮验收")
|
|
measured = evidence.get("utterances")
|
|
if (
|
|
not isinstance(measured, list)
|
|
or any(not isinstance(u, dict) for u in measured)
|
|
or [u.get("utterance_id") for u in measured] != expected_ids
|
|
):
|
|
raise ValueError("冻结的对白台词与镜头分配不符")
|
|
cursor = offset
|
|
for u in measured:
|
|
start, end = u.get("start_sample"), u.get("end_sample")
|
|
if (
|
|
type(start) is not int
|
|
or type(end) is not int
|
|
or not cursor <= start < end
|
|
or end > min(frames * 2000, audio["sample_count"] + offset)
|
|
):
|
|
raise ValueError("原声中存在无效的逐句时间")
|
|
cursor = end
|
|
return [
|
|
dict(
|
|
u,
|
|
start_sample=u["start_sample"] + timing["start_frame"] * 2000,
|
|
end_sample=u["end_sample"] + timing["start_frame"] * 2000,
|
|
)
|
|
for u in measured
|
|
]
|
|
|
|
|
|
async def plan_remix(session, draft, m, body, settings):
|
|
data = body.model_dump(exclude_unset=True) if hasattr(body, "model_dump") else body
|
|
if data.get("targets") or data.get("force"):
|
|
fail(422, "invalid_target", "重混音专用计划只接受已选原片、净音轨与当前配乐")
|
|
issues, recipes, hashes, measured = [], [], {}, []
|
|
|
|
def issue(code, unit, message):
|
|
issues.append(dict(code=code, unit_id=unit, message=message))
|
|
|
|
timing, prepared = {}, {}
|
|
if not native_enabled(m):
|
|
issue("mix_native_required", "audio", "重混音专用计划需要已选用的原生对白镜头")
|
|
else:
|
|
timing = compile_story_timing(m["spec"])
|
|
sequences = {s["id"]: s for s in m["spec"]["dialogue"]["sequences"]}
|
|
limit = min(
|
|
60, m["production"]["max_shots"], read_runtime_policy(settings)["workflow_max_shots"]
|
|
)
|
|
if len(m["shot_ids"]) > limit or timing["total_frames"] > min(
|
|
7200, m["production"]["max_total_frames"]
|
|
):
|
|
issue("mix_budget_exceeded", "production", "已选时间线超过成片预算")
|
|
for shot, t in zip(m["spec"]["shots"], timing["shots"], strict=True):
|
|
sid = shot["id"]
|
|
bindings = m["bindings"]
|
|
video, audio, gate = (
|
|
bindings.get(role + ":" + sid) or {}
|
|
for role in ("video", "native_audio", "native_alignment")
|
|
)
|
|
if not video or not await _asset(
|
|
session, draft.owner_id, video, settings, "video", t["contribution_frames"]
|
|
):
|
|
issue("mix_source_missing", sid, "缺少已选用的无字幕原片或原片校验失败")
|
|
continue
|
|
if not audio or not await _asset(session, draft.owner_id, audio, settings, "audio"):
|
|
issue("mix_source_missing", sid, "缺少已选镜头的干净原声音轨或音轨校验失败")
|
|
continue
|
|
if not gate or not await _asset(session, draft.owner_id, gate, settings, "json"):
|
|
issue("mix_timing_missing", sid, "缺少该镜头已验收的原声和字幕时基")
|
|
continue
|
|
try:
|
|
measured.extend(
|
|
_local_timing(
|
|
sid,
|
|
shot,
|
|
t,
|
|
audio,
|
|
video,
|
|
gate,
|
|
assigned_utterance_ids(shot, sequences)
|
|
if m["spec"]["dialogue"].get("enabled")
|
|
else [],
|
|
)
|
|
)
|
|
except ValueError as exc:
|
|
issue("mix_timing_invalid", sid, str(exc))
|
|
continue
|
|
hashes.update((b["asset_id"], b["sha256"]) for b in (video, audio, gate))
|
|
if m["audio"].get("mode") != "silent" or m["audio"].get("mix_mode") == "manual_mix":
|
|
issue("mix_mode_conflict", "audio", "请使用对白自动混音,并将背景音乐放在独立音层")
|
|
if len(m["shot_ids"]) + len(m["audio"].get("layers", [])) > 128:
|
|
issue("mix_limit", "audio", "镜头原声与音层合计超过128段")
|
|
if not issues:
|
|
for aid in dict.fromkeys(layer["asset_id"] for layer in m["audio"].get("layers", [])):
|
|
asset = await session.get(Asset, aid)
|
|
if not asset or not await _asset(
|
|
session, draft.owner_id, dict(asset_id=aid, sha256=asset.sha256), settings, "audio"
|
|
):
|
|
issue("mix_layer_missing", aid, "配乐或音效素材不存在、不可访问或校验失败")
|
|
continue
|
|
hashes[aid] = asset.sha256
|
|
if not issues:
|
|
prepared = dict(
|
|
production_materialized=True,
|
|
effective_manifest=deepcopy(m),
|
|
timing=timing,
|
|
utterances=measured,
|
|
issues=[],
|
|
steps=[],
|
|
native_av_policy="native_av_v1",
|
|
)
|
|
projection = await caption_projection(
|
|
session, draft.owner_id, m, settings, prepared=prepared
|
|
)
|
|
issues.extend(projection["issues"])
|
|
if not issues:
|
|
for i, aid in enumerate(
|
|
dict.fromkeys(x["asset_id"] for x in m["audio"].get("layers", []))
|
|
):
|
|
recipes.append(
|
|
_recipe(
|
|
f"native_bgm_{i:04d}", "audio.normalize", aid, inputs=dict(asset_id=aid)
|
|
)
|
|
)
|
|
recipes += [
|
|
_recipe(
|
|
"dialogue_mix",
|
|
"audio.mix",
|
|
"audio",
|
|
dependencies=[r["key"] for r in recipes],
|
|
parameters=dict(ducking=m["audio"].get("ducking", True)),
|
|
),
|
|
frozen_caption_recipe(projection),
|
|
_recipe(
|
|
"compose",
|
|
"video.compose",
|
|
"film",
|
|
dependencies=["dialogue_mix", "dialogue_captions"],
|
|
),
|
|
]
|
|
signature = digest(
|
|
dict(
|
|
operation="mix_film_v1",
|
|
manifest=m,
|
|
timing=timing,
|
|
recipes=recipes,
|
|
assets=hashes,
|
|
issues=issues,
|
|
body={k: v for k, v in data.items() if k != "expected_plan_hash"},
|
|
)
|
|
)
|
|
return revision_plan(draft, recipes, hashes, issues, prepared, signature)
|