cmvr-visual/backend/app/adapters/provider/video_generation.py

86 lines
3.8 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

"""普通视频参数校验;模型配置与历史兼容规则保留在纯领域层。"""
from app.domain.video_generation import (
H3_PROFILES,
H3_V2_PROFILE,
H3_TP2_PROFILE,
H3_TP2_MAX_REQUESTED_FRAMES,
H3_NATIVE_AV_PROFILES,
H3_NATIVE_AV_TP2_PROFILE,
defaults,
profile_name,
)
from .contracts import _int, _float, _keys, _string, _invalid
def normalize(parameters, inputs):
if not isinstance(parameters, dict) or not isinstance(inputs, dict):
raise _invalid("视频输入和参数必须为对象")
profile = profile_name(parameters)
h3 = profile in H3_PROFILES
p = defaults(profile, explicit="profile" in parameters)
_keys(parameters, set(p) | ({"negative_prompt"} if not h3 else set()))
native = profile in H3_NATIVE_AV_PROFILES
if profile == H3_V2_PROFILE or native:
_keys(inputs, {"prompt", "first_frame_asset_id", "last_frame_asset_id", "negative_prompt"})
mode = parameters.get("conditioning_mode", "first_frame")
if mode not in ("first_frame", "first_last_frame"):
raise _invalid("Invalid conditioning mode")
if profile == H3_NATIVE_AV_TP2_PROFILE and mode != "first_frame":
raise _invalid("H3双卡原生有声暂仅支持首帧条件")
_string(inputs.get("first_frame_asset_id"), 64)
if mode == "first_last_frame":
_string(inputs.get("last_frame_asset_id"), 64)
elif "last_frame_asset_id" in inputs:
raise _invalid("Last frame requires first_last_frame mode")
else:
_keys(inputs, {"prompt", "image_asset_id", "negative_prompt"})
if profile == H3_TP2_PROFILE:
_string(inputs.get("image_asset_id"), 64)
if h3 and inputs.get("negative_prompt", "") != "":
raise _invalid("H3视频不支持负提示词;故事负提示词仅用于首帧图片")
_string(inputs.get("prompt"))
p.update(parameters)
if h3:
for key in ("width", "height"):
_int(p[key], 768, 1344, 32)
if (p["width"], p["height"]) not in {(1344, 768), (768, 1344)}:
raise _invalid("H3视频仅支持1344×768或768×1344")
else:
for key in ("width", "height"):
_int(p[key], 256, 1280, 32)
if p["width"] * p["height"] > 1280 * 704:
raise _invalid("画面像素总数超出服务限制")
p["guidance_scale"] = _float(p["guidance_scale"], 1, 20)
_string(p.get("negative_prompt", inputs.get("negative_prompt", "")), required=False)
_int(p["num_inference_steps"], 2 if h3 else 1, 100)
_int(p["seed"], 0, 2**32 - 1)
if profile == H3_V2_PROFILE or native:
_int(p["num_frames"], 124, 158 if profile == H3_NATIVE_AV_TP2_PROFILE else 345)
if (p["num_frames"] - 124) % 17:
raise _invalid("Invalid H3 native frame count")
else:
if profile == H3_TP2_PROFILE:
if (
type(p["num_frames"]) is not int
or not 25 <= p["num_frames"] <= H3_TP2_MAX_REQUESTED_FRAMES
or p["num_frames"] % 4 != 1
):
raise _invalid("H3双卡单镜头支持1–6秒、25–145请求帧且帧数须为4n+1;原时长已保留")
else:
_int(p["num_frames"], 25 if h3 else 5, 337 if h3 else 361)
if (
(profile != H3_V2_PROFILE and not native and p["num_frames"] % 4 != 1)
or type(p["fps"]) is not int
or p["fps"] != 24
):
raise _invalid("视频帧数或帧率无效")
if type(p["upscale"]) is not bool:
raise _invalid("增强开关必须为布尔值")
if (profile == H3_TP2_PROFILE or native) and p["upscale"]:
raise _invalid("H3双卡或原生有声暂不支持画质增强")
_int(p["upscale_width"], 256, 3840, 2)
_int(p["upscale_height"], 256, 2160, 2)
_int(p["tile_size"], 64, 256, 16)
return p