cmvr-visual/backend/app/adapters/provider/video_editing.py
zhanghao 88cf14d794 feat: consolidate story studio and editable automatic production
Restructure backend and frontend modules; add story, library, and editor schema migrations.

Support editable automatic production, manual confirmation modes, account and user lifecycle management, compact container scrolling, consistent forms, and per-task result previews.

Update inference and TTS scheduling, deployment guidance, and regression coverage; remove obsolete standalone pages and temporary acceptance artifacts.
2026-11-14 14:07:26 +08:00

75 lines
3.4 KiB
Python

"""可信分析绑定到真实输入字节,再送入现有监管队列。"""
import asyncio
from app.adapters.media import inspect_media, inspect_video_timeline
from app.services.artifacts import digest_file
from app.domain.errors import MediaError
from app.domain.video_editing import video_capability, video_defaults
async def prepare_video(provider, task, inputs, parameters, assets, binding):
local = video_capability(provider.settings)
remote = (await provider.upstream_capabilities()).get("video_edit", {})
p = video_defaults(task, provider.settings) | parameters
if (
remote.get("available") is not True
or remote.get("profile") != p["profile"]
or remote.get("fps") != 24
):
raise MediaError("capability_unavailable", "上游视频编辑未认证或模型版本不匹配。")
source = provider._asset(inputs["video_asset_id"], assets)
info = await inspect_media(source, provider.settings)
duration = await inspect_video_timeline(source, provider.settings)
if (
info["kind"] != "video"
or duration > min(local["max_seconds"], remote.get("max_seconds", 0)) + 0.001
or p["max_pixels"] > remote.get("max_pixels", 0)
):
raise MediaError("invalid_input", "视频类型、时长或规格超出已认证范围。")
digest = await asyncio.to_thread(digest_file, source)
paths = []
if task == "video.replace":
if (
not isinstance(binding, dict)
or binding.get("source_sha256") != digest
or binding.get("profile") != p["profile"]
):
raise MediaError("analysis_source_mismatch", "源视频字节与冻结分析不匹配。")
if len(inputs["replacements"]) > remote.get("max_people", 0):
raise MediaError("invalid_input", "人物数量超出上游范围。")
for row in inputs["replacements"]:
image = provider._asset(row["reference_asset_id"], assets)
if (await inspect_media(image, provider.settings))["kind"] != "image":
raise MediaError("invalid_input", "人物参考素材必须是有效图片。")
if await asyncio.to_thread(digest_file, image) != binding.get(
"reference_sha256", {}
).get(row["reference_asset_id"]):
raise MediaError("invalid_asset", "参考图片字节与冻结映射不一致。")
paths.append(image)
# 全部输入完成校验后才上传;原片不改写,映射保序且包含所有所选人物。
async def path(value):
return (
await provider._upload_input(value)
if provider.settings.inference_remote_files
else str(value)
)
out = {"video_path": await path(source)}
if task == "video.replace":
out.update(
analysis_id=binding["analysis_id"],
analysis_token=binding["analysis_token"],
prompt=inputs.get("prompt", ""),
negative_prompt=inputs.get("negative_prompt", ""),
replacements=[
{
"person_id": row["person_id"],
"image_path": await path(image),
"prompt": row.get("prompt", ""),
}
for row, image in zip(inputs["replacements"], paths, strict=True)
],
)
return {"task_type": task, "input": out, "parameters": p, "timeout_seconds": 86400}