Add MJ image node, GIF save, and Kling/image generator updates
Ship o1key MJ图片生成 with content/style refs and prompt flags, save-GIF with preview, Kling 4.0 helpers, plus GPT/unified image node polish and docs.
This commit is contained in:
+217
-1
@@ -25,6 +25,10 @@ import aiohttp
|
||||
|
||||
from comfy_api.latest import io
|
||||
|
||||
from ..clients.kling_video_client import (
|
||||
build_kling_v4_body,
|
||||
endpoint_for as kling_v4_endpoint_for,
|
||||
)
|
||||
from ..utils.config import get_api_key_or_raise, get_base_url_by_route
|
||||
from ..utils.image_utils import tensor_to_pil
|
||||
from ..utils.r2_uploader import upload_image
|
||||
@@ -104,6 +108,11 @@ _REFERENCE_VIDEO_MIN_FPS = 24.0
|
||||
_REFERENCE_VIDEO_MAX_FPS = 60.0
|
||||
_REFERENCE_VIDEO_MIN_RATIO = 1 / 2.5
|
||||
_REFERENCE_VIDEO_MAX_RATIO = 2.5
|
||||
_V4_REFERENCE_VIDEO_MAX_BYTES = 300 * 1024 * 1024
|
||||
_V4_REFERENCE_VIDEO_MIN_DURATION = 3.0
|
||||
_V4_REFERENCE_VIDEO_MAX_DURATION = 30.0
|
||||
_V4_REFERENCE_VIDEO_MIN_RATIO = 2 / 5
|
||||
_V4_REFERENCE_VIDEO_MAX_RATIO = 5 / 2
|
||||
|
||||
|
||||
# ── 工具函数 ───────────────────────────────────────────────────────────────────
|
||||
@@ -428,6 +437,30 @@ def _validate_reference_video(video_path: str, model: str) -> None:
|
||||
)
|
||||
|
||||
|
||||
def _validate_kling_v4_reference_video(video_path: str) -> float:
|
||||
"""Validate one Kling 4.0 Omni reference clip and return its duration."""
|
||||
ext = os.path.splitext(video_path)[1].lower()
|
||||
if ext not in (".mp4", ".mov"):
|
||||
raise RuntimeError(f"Kling 4.0 参考视频仅支持 MP4/MOV,当前为 {ext or '无扩展名'}。")
|
||||
size = os.path.getsize(video_path)
|
||||
if size > _V4_REFERENCE_VIDEO_MAX_BYTES:
|
||||
raise RuntimeError(
|
||||
f"Kling 4.0 参考视频不能超过 300MB,当前为 {size / 1024 / 1024:.1f}MB。"
|
||||
)
|
||||
meta = _probe_video_with_ffprobe(video_path) or _probe_video_with_cv2(video_path)
|
||||
if not meta or not meta.get("width") or not meta.get("height") or not meta.get("duration"):
|
||||
raise RuntimeError("无法读取 Kling 4.0 参考视频的尺寸和时长。")
|
||||
duration = float(meta["duration"])
|
||||
if not _V4_REFERENCE_VIDEO_MIN_DURATION <= duration <= _V4_REFERENCE_VIDEO_MAX_DURATION:
|
||||
raise RuntimeError(
|
||||
f"Kling 4.0 单段参考视频时长需在 3~30 秒之间,当前为 {duration:.2f} 秒。"
|
||||
)
|
||||
ratio = float(meta["width"]) / float(meta["height"])
|
||||
if not _V4_REFERENCE_VIDEO_MIN_RATIO <= ratio <= _V4_REFERENCE_VIDEO_MAX_RATIO:
|
||||
raise RuntimeError("Kling 4.0 参考视频宽高比需在 2:5~5:2 之间。")
|
||||
return duration
|
||||
|
||||
|
||||
def _collect_shots(分镜模式: dict, 时长: int) -> list:
|
||||
"""从 DynamicCombo 的 dict 收集分镜参数并校验。
|
||||
|
||||
@@ -605,6 +638,38 @@ def _v3_omni_subs():
|
||||
]
|
||||
|
||||
|
||||
def _v4_subs():
|
||||
"""Kling 4.0 generation mode and its capability-limited parameters."""
|
||||
return [
|
||||
io.Combo.Input(
|
||||
"生成模式",
|
||||
options=["文生视频", "关键帧", "全能参考"],
|
||||
default="文生视频",
|
||||
tooltip="文生视频:只使用提示词;关键帧:首帧可加中间帧和尾帧;全能参考:使用参考图或参考视频。",
|
||||
),
|
||||
io.Int.Input(
|
||||
"时长",
|
||||
default=5,
|
||||
min=3,
|
||||
max=30,
|
||||
display_mode=io.NumberDisplay.slider,
|
||||
tooltip="Kling 4.0 支持 3~30 秒。",
|
||||
),
|
||||
io.Combo.Input(
|
||||
"模式",
|
||||
options=["720p", "1080p"],
|
||||
default="720p",
|
||||
tooltip="Kling 4.0 支持 720p 和 1080p。",
|
||||
),
|
||||
io.Combo.Input(
|
||||
"宽高比",
|
||||
options=["智能", "16:9", "9:16", "1:1", "21:9"],
|
||||
default="智能",
|
||||
tooltip="关键帧模式跟随首帧比例;文生和全能参考支持智能、16:9、9:16、1:1、21:9。",
|
||||
),
|
||||
]
|
||||
|
||||
|
||||
def _build_model_input():
|
||||
"""构建「模型」DynamicCombo,把「时长」「模式」作为随模型变化的子输入。
|
||||
|
||||
@@ -637,8 +702,11 @@ def _build_model_input():
|
||||
io.DynamicCombo.Option("v3-omni", _v3_omni_subs()),
|
||||
io.DynamicCombo.Option("v2-6", _v26_subs()),
|
||||
io.DynamicCombo.Option("v3", _v3_subs()),
|
||||
# Append the new branch so existing DynamicCombo option order and
|
||||
# saved v3/v2.6 workflows remain unchanged.
|
||||
io.DynamicCombo.Option("v4", _v4_subs()),
|
||||
],
|
||||
tooltip="v2-6: 5/10s; v3: 3-15s; v3-omni: 多模态参考、视频编辑、主体管理",
|
||||
tooltip="v2-6: 5/10s; v3: 3-15s; v3-omni: 多模态参考、视频编辑、主体管理;v4: 文生、关键帧、全能参考,3-30s。",
|
||||
)
|
||||
|
||||
|
||||
@@ -706,6 +774,28 @@ class K3Video(io.ComfyNode):
|
||||
tooltip="seed 仅控制节点是否重新运行,结果本身不可复现。"),
|
||||
# 负向提示词置于最下方(全局生效,两种模式都可用)
|
||||
io.String.Input("负向提示词", multiline=True, default=""),
|
||||
# Kling 4.0 sockets are appended after the released layout so
|
||||
# positional widgets in existing K3Video workflows do not move.
|
||||
*[
|
||||
io.Image.Input(f"中间帧{i}", optional=True,
|
||||
tooltip="Kling 4.0 关键帧模式使用;按连接顺序插入首帧与尾帧之间。")
|
||||
for i in range(1, 9)
|
||||
],
|
||||
*[
|
||||
io.Image.Input(f"参考图{i}", optional=True,
|
||||
tooltip="Kling 4.0 全能参考模式使用;最多 10 张参考图。")
|
||||
for i in range(6, 11)
|
||||
],
|
||||
io.Image.Input(
|
||||
"参考图1",
|
||||
optional=True,
|
||||
tooltip="Kling 4.0 全能参考模式使用;最多 10 张参考图。",
|
||||
),
|
||||
*[
|
||||
io.Video.Input(f"参考视频{i}", optional=True,
|
||||
tooltip="Kling 4.0 全能参考模式使用;最多 5 段参考视频。")
|
||||
for i in range(2, 6)
|
||||
],
|
||||
],
|
||||
outputs=[io.Video.Output(display_name="视频")],
|
||||
accept_all_inputs=True,
|
||||
@@ -730,6 +820,49 @@ class K3Video(io.ComfyNode):
|
||||
"Content-Type": "application/json",
|
||||
}
|
||||
shots = _collect_shots(分镜模式, 时长)
|
||||
if 模型代号 == "v4":
|
||||
generation_mode = 模型.get("生成模式", "文生视频")
|
||||
middle_frames = [
|
||||
_kwargs.get(f"中间帧{i}")
|
||||
for i in range(1, 9)
|
||||
if _kwargs.get(f"中间帧{i}") is not None
|
||||
]
|
||||
reference_images = [
|
||||
image for image in (
|
||||
_kwargs.get("参考图1"),
|
||||
参考图2, 参考图3, 参考图4, 参考图5,
|
||||
*[_kwargs.get(f"参考图{i}") for i in range(6, 11)],
|
||||
) if image is not None
|
||||
]
|
||||
reference_videos = [
|
||||
video for video in (
|
||||
参考视频,
|
||||
*[_kwargs.get(f"参考视频{i}") for i in range(2, 6)],
|
||||
) if video is not None
|
||||
]
|
||||
v4_prompt = (
|
||||
(分镜模式.get("正向提示词") or "")
|
||||
if not shots else shots[0]["prompt"]
|
||||
)
|
||||
save_path = await cls._build_v4(
|
||||
base_url,
|
||||
headers,
|
||||
generation_mode,
|
||||
v4_prompt,
|
||||
负向提示词,
|
||||
时长,
|
||||
模式,
|
||||
宽高比,
|
||||
起始帧,
|
||||
middle_frames,
|
||||
尾帧,
|
||||
reference_images,
|
||||
reference_videos,
|
||||
shots,
|
||||
)
|
||||
if _FOLDER_PATHS_OK:
|
||||
return io.NodeOutput(InputImpl.VideoFromFile(save_path))
|
||||
return io.NodeOutput(save_path)
|
||||
# 正向提示词仅在「禁用」(单段)时存在;选故事板后腾讯忽略主 Prompt,故隐藏。
|
||||
提示词 = (分镜模式.get("正向提示词") or "") if not shots else ""
|
||||
|
||||
@@ -1028,6 +1161,89 @@ class K3Video(io.ComfyNode):
|
||||
)
|
||||
|
||||
# ── 标准 image2video 端点(v3 / v2-6)────────────────────────
|
||||
@classmethod
|
||||
async def _build_v4(
|
||||
cls,
|
||||
base_url,
|
||||
headers,
|
||||
generation_mode,
|
||||
prompt,
|
||||
negative_prompt,
|
||||
duration,
|
||||
resolution,
|
||||
aspect_ratio,
|
||||
first_frame,
|
||||
middle_frame_inputs,
|
||||
last_frame,
|
||||
reference_image_inputs,
|
||||
reference_video_inputs,
|
||||
shots,
|
||||
):
|
||||
"""Build and run one Kling 4.0 text/keyframe/Omni task."""
|
||||
from ..utils.r2_uploader import upload_video
|
||||
|
||||
async def upload_frame(image, label):
|
||||
if image is None:
|
||||
return None
|
||||
print(f"[Kling 4.0] uploading {label}...")
|
||||
return await upload_image(_prepare_image_for_upload(image), base_url=base_url)
|
||||
|
||||
validated_video_paths = []
|
||||
reference_video_seconds = 0.0
|
||||
for video in reference_video_inputs:
|
||||
video_path = _resolve_reference_video_path(video)
|
||||
reference_video_seconds += _validate_kling_v4_reference_video(video_path)
|
||||
validated_video_paths.append(video_path)
|
||||
if reference_video_seconds > _V4_REFERENCE_VIDEO_MAX_DURATION:
|
||||
raise RuntimeError("Kling 4.0 参考视频总时长不能超过 30 秒。")
|
||||
|
||||
first_url = await upload_frame(first_frame, "first frame")
|
||||
middle_urls = [
|
||||
await upload_frame(image, f"middle frame {index}")
|
||||
for index, image in enumerate(middle_frame_inputs, start=1)
|
||||
]
|
||||
last_url = await upload_frame(last_frame, "last frame")
|
||||
|
||||
reference_image_urls = [
|
||||
await upload_frame(image, f"reference image {index}")
|
||||
for index, image in enumerate(reference_image_inputs, start=1)
|
||||
]
|
||||
reference_video_urls = []
|
||||
for index, video_path in enumerate(validated_video_paths, start=1):
|
||||
print(f"[Kling 4.0] uploading reference video {index}...")
|
||||
reference_video_urls.append(await upload_video(video_path, base_url=base_url))
|
||||
|
||||
body = build_kling_v4_body(
|
||||
generation_mode=generation_mode,
|
||||
prompt=prompt,
|
||||
negative_prompt=negative_prompt,
|
||||
duration=duration,
|
||||
resolution=resolution,
|
||||
aspect_ratio=aspect_ratio,
|
||||
first_frame=first_url,
|
||||
middle_frames=middle_urls,
|
||||
last_frame=last_url,
|
||||
reference_images=reference_image_urls,
|
||||
reference_videos=reference_video_urls,
|
||||
)
|
||||
if shots:
|
||||
body["multi_shot"] = True
|
||||
body["shot_type"] = "customize"
|
||||
body["multi_prompt"] = [
|
||||
{"index": shot["index"], "prompt": shot["prompt"], "duration": shot["duration"]}
|
||||
for shot in shots
|
||||
]
|
||||
|
||||
create_path, status_path = kling_v4_endpoint_for(generation_mode)
|
||||
return await _run_video_task(
|
||||
base_url,
|
||||
headers,
|
||||
body,
|
||||
create_path,
|
||||
status_path,
|
||||
f"Kling 4.0 {generation_mode} submit: ",
|
||||
)
|
||||
|
||||
@classmethod
|
||||
async def _build_standard(cls, 模型, base_url, headers, 起始帧, 提示词, 负向提示词, 时长, 生成音频, 模式, shots, 尾帧=None):
|
||||
"""标准图生视频接口:POST /kling/v1/videos/image2video
|
||||
|
||||
Reference in New Issue
Block a user