fix: 视频节点改用临时文件,避免与下游保存节点重复落盘

- K3_video.py / K3_video_firstlast.py / K3_motion_control.py
- K_video.py
- kling_video.py (KlingVideo / KlingFirstLastFrame / KlingMotionControlTest)
- seedance_video.py (Seedance / SeedanceMultiModal)

所有视频节点下载视频时改写到 tempfile.mkstemp() 临时路径,
最终落盘完全交给下游保存节点处理,不再重复写入 output/video/。
同步清理已无用的 _get_video_dir / _get_next_counter / import re 等辅助代码。
This commit is contained in:
Jony
2026-04-26 22:18:22 +08:00
parent 8299594646
commit c646b0d1d7
6 changed files with 28 additions and 190 deletions
+4 -27
View File
@@ -8,8 +8,8 @@ import asyncio
import io
import json
import os
import re
import struct
import tempfile
import aiohttp
@@ -36,27 +36,6 @@ _POLL_MAX = 15
# ── 工具函数 ───────────────────────────────────────────────────────────────────
def _get_video_dir() -> str:
if _FOLDER_PATHS_OK:
base = folder_paths.get_output_directory()
else:
plugin = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
base = os.path.join(os.path.dirname(os.path.dirname(plugin)), "output")
d = os.path.join(base, "video")
os.makedirs(d, exist_ok=True)
return d
def _next_counter(directory: str, prefix: str) -> int:
pattern = re.compile(rf"^{re.escape(prefix)}_(\d+)")
max_n = 0
if os.path.exists(directory):
for f in os.listdir(directory):
m = pattern.match(f)
if m:
max_n = max(max_n, int(m.group(1)))
return max_n + 1
# ── 视频时长检测(纯标准库,跨平台) ──────────────────────────────────────────
@@ -219,10 +198,8 @@ class K3MotionControl:
if prompt:
body["prompt"] = prompt
# ── 保存路径 ──────────────────────────────────────────────────
video_dir = _get_video_dir()
counter = _next_counter(video_dir, "k3_motion")
save_path = os.path.join(video_dir, f"k3_motion_{counter:05d}.mp4")
# ── 保存路径(临时文件,避免与下游保存节点重复落盘)──────────────────
tmp_fd, save_path = tempfile.mkstemp(suffix=".mp4", prefix="k3_motion_")
connector = aiohttp.TCPConnector(ssl=False, force_close=True)
async with aiohttp.ClientSession(connector=connector) as session:
@@ -311,7 +288,7 @@ class K3MotionControl:
async with session.get(video_result_url, allow_redirects=True) as resp:
if resp.status != 200:
raise RuntimeError(f"视频下载失败 ({resp.status})")
os.makedirs(os.path.dirname(save_path), exist_ok=True)
os.close(tmp_fd)
with open(save_path, "wb") as f:
async for chunk in resp.content.iter_chunked(8192):
f.write(chunk)