cmvr-visual/backend/app/services/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

116 lines
4.5 KiB
Python

"""视频编辑结果导入前的完整性与媒体验证。"""
import math
from app.domain.video_editing import safe_analysis
from app.services.snapshots import public_value
from app.services.orchestration.executions import gpu_result
def validate_result_metadata(job, outputs, settings):
raw = gpu_result(job).get("metadata", {})
if job.task_type == "video.people.analyze":
data = safe_analysis(raw, settings, output_count=len(outputs))
source = (job.upstream_payload or {}).get("input", {}).get("video_path", "")
# 本地与远程输入均已在准备阶段校验;结果源摘要仍须对照原素材记录。
if not source:
raise ValueError("Analysis input binding missing")
indexes = [data["poster_output_index"]] + [
p["thumbnail_output_index"] for p in data["people"]
]
return public_value(data), indexes
indexes = raw.get("final_output_indices")
frozen = job.parameters.get("_video_edit_binding", {})
if (
not isinstance(indexes, list)
or len(indexes) != 1
or any(type(i) is not int or not 0 <= i < len(outputs) for i in indexes)
):
raise ValueError("Replacement final output index missing")
expected = [r["person_id"] for r in job.input_json["replacements"]]
if (
raw.get("person_ids") != expected
or raw.get("profile") != frozen.get("profile")
or raw.get("fps") != 24
):
raise ValueError("Replacement output does not cover frozen people/profile")
video = frozen["video"]
if raw.get("source_frames") != video["frames"] or raw.get("output_frames") != video["frames"]:
raise ValueError("Replacement truncated source")
for key in ("width", "height"):
if type(raw.get(key)) is not int or raw[key] != video[key]:
raise ValueError("Replacement dimensions changed")
keys = {
"person_ids",
"profile",
"source_frames",
"output_frames",
"fps",
"width",
"height",
"final_output_indices",
"warnings",
}
return public_value({k: v for k, v in raw.items() if k in keys}), indexes
async def validate_video_output(job, source, settings):
from app.adapters.media import inspect_media, inspect_video_track
if job.task_type != "video.replace":
return
metadata, _ = validate_result_metadata(job, gpu_result(job)["outputs"], settings)
info = await inspect_media(source, settings)
track = await inspect_video_track(source, settings)
video = job.parameters["_video_edit_binding"]["video"]
if (
info["kind"] != "video"
or info["mime_type"] != "video/mp4"
or track["fps"] != 24
or track["frames"] != video["frames"]
or any(track[k] != metadata[k] for k in ("width", "height"))
or abs(info["duration_seconds"] - video["duration_seconds"]) > 1 / 24 + 0.01
):
raise ValueError("Decoded replacement media differs from frozen source")
preserve = job.parameters.get("preserve_audio", True)
source_audio = job.parameters["_video_edit_binding"]["source_has_audio"]
if bool(info.get("has_audio")) != bool(preserve and source_audio):
raise ValueError("Replacement audio missing or unexpected")
if preserve and source_audio:
duration = await audio_duration(source, settings, fallback=video["duration_seconds"])
original = job.parameters["_video_edit_binding"].get(
"source_audio_duration", video["duration_seconds"]
)
if (
not math.isfinite(duration)
or abs(duration - min(original, video["duration_seconds"])) > 0.15
):
raise ValueError("Replacement audio duration differs")
async def audio_duration(source, settings, *, fallback):
import json
from app.adapters.media import run_process, input_options
data = json.loads(
await run_process(
[
settings.ffprobe_bin,
"-v",
"error",
*input_options(),
"-select_streams",
"a:0",
"-show_entries",
"stream=duration",
"-of",
"json",
str(source),
]
)
)
value = data["streams"][0].get("duration")
duration = fallback if value in {None, "N/A"} else float(value)
if not math.isfinite(duration) or duration <= 0:
raise ValueError("Invalid source audio duration")
return duration