@kevlns/art-workshop 0.5.2 → 0.6.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +13 -11
- package/art-workshop.mjs +50 -10
- package/docs/channels.md +49 -0
- package/docs/commands.md +2158 -91
- package/docs/configuration.md +8 -0
- package/docs/core-guidance.md +12 -4
- package/docs/data-formats.md +10 -6
- package/docs/local-models/qwen-image-2.1.json +1 -1
- package/docs/modes.md +12 -6
- package/docs/release-0.6.0.md +9 -0
- package/lib/analysis-worker.mjs +9 -5
- package/lib/backends.mjs +2 -0
- package/lib/candidates.mjs +2 -1
- package/lib/channel-operation.mjs +84 -0
- package/lib/core.mjs +33 -2
- package/lib/edit.mjs +5 -3
- package/lib/generate.mjs +5 -3
- package/lib/generation-policy.mjs +1 -1
- package/lib/object-format.mjs +2 -2
- package/lib/prompt-structure.mjs +3 -3
- package/lib/service.mjs +21 -0
- package/lib/style-bundle.mjs +19 -6
- package/lib/style-contract.mjs +3 -3
- package/lib/style-lifecycle.mjs +7 -73
- package/lib/style-report.mjs +1 -1
- package/lib/style-samples.mjs +94 -0
- package/lib/task-contract.mjs +2 -1
- package/lib/templates.mjs +1 -1
- package/lib/video/PROVENANCE.json +63 -0
- package/lib/video/atomic.mjs +32 -0
- package/lib/video/channel.mjs +70 -0
- package/lib/video/contract-local-media.mjs +42 -0
- package/lib/video/contract-story.mjs +213 -0
- package/lib/video/contract-video.mjs +117 -0
- package/lib/video/contracts.mjs +3 -0
- package/lib/video/h3.mjs +232 -0
- package/lib/video/local-runtime.mjs +20 -0
- package/lib/video/media.mjs +223 -0
- package/lib/video/models.mjs +336 -0
- package/lib/video/path.mjs +18 -0
- package/lib/video/process.mjs +15 -0
- package/lib/video/resource.mjs +15 -0
- package/lib/video/service.mjs +731 -0
- package/lib/video/workflow.mjs +571 -0
- package/lib/video.mjs +12 -0
- package/lib/workspace.mjs +35 -21
- package/package.json +8 -3
- package/rules/core-guidance.json +1 -1
- package/skills/art-workshop/SKILL.md +4 -2
- package/skills/art-workshop/references/project.md +18 -9
- package/skills/art-workshop/references/style-discussion.md +8 -6
- package/skills/art-workshop/references/video.md +13 -0
- package/v-cli.plugin.json +4380 -276
|
@@ -0,0 +1,223 @@
|
|
|
1
|
+
import {execute as executeProcess} from './process.mjs';
|
|
2
|
+
import {stat} from 'node:fs/promises';
|
|
3
|
+
import { execFile } from "node:child_process";
|
|
4
|
+
import { promisify } from "node:util";
|
|
5
|
+
import { dirname, join, isAbsolute } from "node:path";
|
|
6
|
+
import { mkdir, readFile, rm } from "node:fs/promises";
|
|
7
|
+
import { randomUUID } from "node:crypto";
|
|
8
|
+
const exec = promisify(execFile);
|
|
9
|
+
class VideoMedia {
|
|
10
|
+
constructor(settings, signal) {
|
|
11
|
+
this.settings = settings;
|
|
12
|
+
this.signal = signal;
|
|
13
|
+
}
|
|
14
|
+
settings;
|
|
15
|
+
signal;
|
|
16
|
+
async binary(probe = false) {
|
|
17
|
+
const configured = (await this.settings()).ffmpegPath?.trim() || process.env.VANT_FFMPEG?.trim();
|
|
18
|
+
if (!configured) return probe ? "ffprobe" : "ffmpeg";
|
|
19
|
+
if (!isAbsolute(configured))
|
|
20
|
+
throw new Error("FFmpeg 路径必须是可执行文件的绝对路径");
|
|
21
|
+
return probe ? join(
|
|
22
|
+
dirname(configured),
|
|
23
|
+
process.platform === "win32" ? "ffprobe.exe" : "ffprobe"
|
|
24
|
+
) : configured;
|
|
25
|
+
}
|
|
26
|
+
async run(args, probe = false) {
|
|
27
|
+
try {
|
|
28
|
+
return await executeProcess(await this.binary(probe),args,{signal:this.signal,timeoutMs:600000,maxOutputChars:4*1024*1024});
|
|
29
|
+
} catch (e) {
|
|
30
|
+
const error = e;
|
|
31
|
+
if (this.signal.aborted) throw new Error("视频处理已中断");
|
|
32
|
+
throw new Error(
|
|
33
|
+
error.code === "ENOENT" ? "未找到 FFmpeg / ffprobe,请在视频设置中配置剪辑工具路径" : `视频处理失败:${(error.stderr || error.message).slice(-2e3)}`
|
|
34
|
+
);
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
async status() {
|
|
38
|
+
try {
|
|
39
|
+
await Promise.all([this.run(["-version"]), this.run(["-version"], true)]);
|
|
40
|
+
return { available: true, reason: "" };
|
|
41
|
+
} catch (e) {
|
|
42
|
+
return { available: false, reason: e.message };
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
async probe(path) {
|
|
46
|
+
const { stdout } = await this.run(
|
|
47
|
+
["-v", "error", "-show_streams", "-show_format", "-of", "json", path],
|
|
48
|
+
true
|
|
49
|
+
);
|
|
50
|
+
const data = JSON.parse(stdout), v = data.streams?.find((s) => s.codec_type === "video");
|
|
51
|
+
const fraction = String(v?.avg_frame_rate || "0/1").split("/").map(Number);
|
|
52
|
+
const info = {
|
|
53
|
+
duration: Number(data.format?.duration ?? v?.duration),
|
|
54
|
+
fps: fraction[0] / fraction[1],
|
|
55
|
+
width: Number(v?.width),
|
|
56
|
+
height: Number(v?.height),
|
|
57
|
+
audio: !!data.streams?.some((s) => s.codec_type === "audio"),
|
|
58
|
+
videoDuration: Number(v?.duration ?? data.format?.duration),
|
|
59
|
+
frames: Number(v?.nb_frames) || undefined
|
|
60
|
+
};
|
|
61
|
+
if (!v || !Number.isFinite(info.duration) || info.duration <= 0 || info.duration > 7200 || !Number.isFinite(info.fps) || info.fps <= 0 || info.fps > 240 || !info.width || !info.height || info.width > 8192 || info.height > 8192)
|
|
62
|
+
throw new Error("视频时长、帧率或尺寸无效");
|
|
63
|
+
return info;
|
|
64
|
+
}
|
|
65
|
+
async frame(path, time, directory) {
|
|
66
|
+
const info = await this.probe(path);
|
|
67
|
+
if (!Number.isFinite(time) || time < 0 || time >= info.duration)
|
|
68
|
+
throw new Error("取帧位置超出视频范围");
|
|
69
|
+
const finalFrame = Number.isInteger(info.frames) && info.frames > 0 ? (info.frames - 1) / info.fps : Math.max(0, info.videoDuration - 1 / info.fps);
|
|
70
|
+
const safeTime = Math.min(time, finalFrame);
|
|
71
|
+
const output = join(directory, `${randomUUID()}.png`);
|
|
72
|
+
await mkdir(directory, { recursive: true });
|
|
73
|
+
try {
|
|
74
|
+
await this.run([
|
|
75
|
+
"-hide_banner",
|
|
76
|
+
"-loglevel",
|
|
77
|
+
"error",
|
|
78
|
+
"-nostdin",
|
|
79
|
+
"-y",
|
|
80
|
+
"-ss",
|
|
81
|
+
String(safeTime),
|
|
82
|
+
"-i",
|
|
83
|
+
path,
|
|
84
|
+
"-frames:v",
|
|
85
|
+
"1",
|
|
86
|
+
"-threads",
|
|
87
|
+
"1",
|
|
88
|
+
output
|
|
89
|
+
]);
|
|
90
|
+
return await readFile(output);
|
|
91
|
+
} finally {
|
|
92
|
+
await rm(output, { force: true });
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
async render(parts, output, profile, keepAudioFrom) {
|
|
96
|
+
if (!parts.length || parts.length > 128)
|
|
97
|
+
throw new Error("拼接片段数量应为 1–128");
|
|
98
|
+
const fps = profile.fps, width = profile.width + profile.width % 2, height = profile.height + profile.height % 2;
|
|
99
|
+
const args = [
|
|
100
|
+
"-hide_banner",
|
|
101
|
+
"-loglevel",
|
|
102
|
+
"error",
|
|
103
|
+
"-nostdin",
|
|
104
|
+
"-y",
|
|
105
|
+
"-filter_complex_threads",
|
|
106
|
+
"1"
|
|
107
|
+
];
|
|
108
|
+
const filters = [];
|
|
109
|
+
let duration = 0;
|
|
110
|
+
for (let i = 0; i < parts.length; i++) {
|
|
111
|
+
const p = parts[i], info = await this.probe(p.path);
|
|
112
|
+
const sourceDuration = p.sourceDuration ?? p.duration, speed = sourceDuration / p.duration;
|
|
113
|
+
if (!Number.isFinite(p.start) || !Number.isFinite(p.duration) || !Number.isFinite(sourceDuration) || p.start < 0 || p.duration <= 0 || sourceDuration <= 0 || p.start + sourceDuration > info.duration + 1 / info.fps)
|
|
114
|
+
throw new Error("片段区间或调整速度超出范围");
|
|
115
|
+
args.push("-i", p.path);
|
|
116
|
+
let remaining = speed;
|
|
117
|
+
const tempo = [];
|
|
118
|
+
while (remaining > 2) {
|
|
119
|
+
tempo.push("atempo=2");
|
|
120
|
+
remaining /= 2;
|
|
121
|
+
}
|
|
122
|
+
while (remaining < 0.5) {
|
|
123
|
+
tempo.push("atempo=0.5");
|
|
124
|
+
remaining *= 2;
|
|
125
|
+
}
|
|
126
|
+
tempo.push(`atempo=${remaining}`);
|
|
127
|
+
filters.push(
|
|
128
|
+
`[${i}:v:0]trim=start=${p.start}:duration=${sourceDuration},setpts=(PTS-STARTPTS)/${speed},scale=${width}:${height}:force_original_aspect_ratio=decrease,pad=${width}:${height}:(ow-iw)/2:(oh-ih)/2,setsar=1,fps=${fps},format=yuv420p,settb=AVTB[v${i}]`
|
|
129
|
+
);
|
|
130
|
+
if (info.audio)
|
|
131
|
+
filters.push(
|
|
132
|
+
`[${i}:a:0]atrim=start=${p.start}:duration=${sourceDuration},asetpts=PTS-STARTPTS,${tempo.join(",")},aresample=48000,aformat=sample_fmts=fltp:channel_layouts=stereo,apad,atrim=duration=${p.duration}[a${i}]`
|
|
133
|
+
);
|
|
134
|
+
else
|
|
135
|
+
filters.push(
|
|
136
|
+
`anullsrc=r=48000:cl=stereo,atrim=duration=${p.duration},asetpts=PTS-STARTPTS[a${i}]`
|
|
137
|
+
);
|
|
138
|
+
}
|
|
139
|
+
let v = "v0", a = "a0";
|
|
140
|
+
duration = parts[0].duration;
|
|
141
|
+
for (let i = 1; i < parts.length; i++) {
|
|
142
|
+
const transition = parts[i - 1].transition;
|
|
143
|
+
if (transition?.type === "dissolve") {
|
|
144
|
+
const overlap = transition.duration;
|
|
145
|
+
if (overlap <= 0 || overlap >= Math.min(parts[i - 1].duration, parts[i].duration))
|
|
146
|
+
throw new Error("叠化时长超出相邻片段范围");
|
|
147
|
+
filters.push(
|
|
148
|
+
`[${v}]fps=${fps},settb=AVTB[fixed${i}]`,
|
|
149
|
+
`[fixed${i}][v${i}]xfade=transition=fade:duration=${overlap}:offset=${duration - overlap}[cv${i}]`,
|
|
150
|
+
`[${a}][a${i}]acrossfade=d=${overlap}[ca${i}]`
|
|
151
|
+
);
|
|
152
|
+
duration += parts[i].duration - overlap;
|
|
153
|
+
} else {
|
|
154
|
+
filters.push(
|
|
155
|
+
`[${v}][${a}][v${i}][a${i}]concat=n=2:v=1:a=1[cv${i}][ca${i}]`
|
|
156
|
+
);
|
|
157
|
+
duration += parts[i].duration;
|
|
158
|
+
}
|
|
159
|
+
v = `cv${i}`;
|
|
160
|
+
a = `ca${i}`;
|
|
161
|
+
}
|
|
162
|
+
if (keepAudioFrom) {
|
|
163
|
+
const audio = await this.probe(keepAudioFrom);
|
|
164
|
+
filters.push(`[${a}]anullsink`);
|
|
165
|
+
if (audio.audio) {
|
|
166
|
+
args.push("-i", keepAudioFrom);
|
|
167
|
+
filters.push(
|
|
168
|
+
`[${parts.length}:a:0]atrim=duration=${duration},asetpts=PTS-STARTPTS,aresample=48000,apad,atrim=duration=${duration}[originalAudio]`
|
|
169
|
+
);
|
|
170
|
+
} else
|
|
171
|
+
filters.push(
|
|
172
|
+
`anullsrc=r=48000:cl=stereo,atrim=duration=${duration}[originalAudio]`
|
|
173
|
+
);
|
|
174
|
+
a = "originalAudio";
|
|
175
|
+
}
|
|
176
|
+
args.push(
|
|
177
|
+
"-filter_complex",
|
|
178
|
+
filters.join(";"),
|
|
179
|
+
"-map",
|
|
180
|
+
`[${v}]`,
|
|
181
|
+
"-map",
|
|
182
|
+
`[${a}]`,
|
|
183
|
+
"-c:v",
|
|
184
|
+
"libx264",
|
|
185
|
+
"-preset",
|
|
186
|
+
"veryfast",
|
|
187
|
+
"-crf",
|
|
188
|
+
"18",
|
|
189
|
+
"-pix_fmt",
|
|
190
|
+
"yuv420p",
|
|
191
|
+
"-threads",
|
|
192
|
+
"2",
|
|
193
|
+
"-r",
|
|
194
|
+
String(fps),
|
|
195
|
+
"-fps_mode",
|
|
196
|
+
"cfr",
|
|
197
|
+
"-c:a",
|
|
198
|
+
"aac",
|
|
199
|
+
"-b:a",
|
|
200
|
+
"192k",
|
|
201
|
+
"-t",
|
|
202
|
+
String(duration),
|
|
203
|
+
"-movflags",
|
|
204
|
+
"+faststart",
|
|
205
|
+
output
|
|
206
|
+
);
|
|
207
|
+
await mkdir(dirname(output), { recursive: true });
|
|
208
|
+
try {
|
|
209
|
+
await this.run(args);
|
|
210
|
+
if((await stat(output)).size>512*1024*1024)throw new Error("视频超过 512 MiB 上限");
|
|
211
|
+
const result = await this.probe(output);
|
|
212
|
+
if (Math.abs(result.duration - duration) > Math.max(0.15, 2 / fps))
|
|
213
|
+
throw new Error("处理后的视频时长与剪辑计划不一致");
|
|
214
|
+
return result;
|
|
215
|
+
} catch (e) {
|
|
216
|
+
await rm(output, { force: true });
|
|
217
|
+
throw e;
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
}
|
|
221
|
+
export {
|
|
222
|
+
VideoMedia
|
|
223
|
+
};
|
|
@@ -0,0 +1,336 @@
|
|
|
1
|
+
import { z } from "zod";
|
|
2
|
+
import { readFile, mkdir, stat } from "node:fs/promises";
|
|
3
|
+
import { join } from "node:path";
|
|
4
|
+
import { atomicJson } from "./atomic.mjs";
|
|
5
|
+
import { videoOutput } from "./contracts.mjs";
|
|
6
|
+
const scalar = z.union([z.string(), z.number().finite(), z.boolean()]);
|
|
7
|
+
const field = z.object({
|
|
8
|
+
label: z.string().min(1),
|
|
9
|
+
type: z.enum(["number", "integer", "string", "boolean"]),
|
|
10
|
+
default: scalar,
|
|
11
|
+
minimum: z.number().finite().optional(),
|
|
12
|
+
maximum: z.number().finite().optional(),
|
|
13
|
+
step: z.number().positive().optional(),
|
|
14
|
+
enum: z.array(z.union([z.string(), z.number().finite()])).optional()
|
|
15
|
+
}).strict();
|
|
16
|
+
const workflow = z.object({
|
|
17
|
+
first: z.boolean(),
|
|
18
|
+
last: z.boolean(),
|
|
19
|
+
middle: z.boolean(),
|
|
20
|
+
references: z.boolean(),
|
|
21
|
+
requireBoundary: z.boolean().optional(),
|
|
22
|
+
template: z.string().min(1)
|
|
23
|
+
}).strict();
|
|
24
|
+
const model = z.object({
|
|
25
|
+
id: z.string().regex(/^[a-zA-Z0-9_-]+$/),
|
|
26
|
+
name: z.string().min(1),
|
|
27
|
+
adapter: z.literal("comfyui"),
|
|
28
|
+
fields: z.record(z.string(), field),
|
|
29
|
+
modes: z.object({
|
|
30
|
+
text: workflow.optional(),
|
|
31
|
+
boundary: workflow.optional(),
|
|
32
|
+
reference: workflow.optional()
|
|
33
|
+
}).strict(),
|
|
34
|
+
frames: z.object({
|
|
35
|
+
multiple: z.number().int().min(1),
|
|
36
|
+
offset: z.number().int().min(1),
|
|
37
|
+
maximum: z.number().int().min(1).max(1e5)
|
|
38
|
+
}).strict(),
|
|
39
|
+
maximumPixels: z.number().int().positive()
|
|
40
|
+
}).strict();
|
|
41
|
+
const node = z.object({
|
|
42
|
+
class_type: z.string().min(1),
|
|
43
|
+
inputs: z.record(z.string(), z.unknown()),
|
|
44
|
+
_meta: z.unknown().optional()
|
|
45
|
+
}).strict();
|
|
46
|
+
const binding = z.object({ node: z.string(), input: z.string() }).strict();
|
|
47
|
+
const template = z.object({
|
|
48
|
+
imageBindings: z.object({
|
|
49
|
+
first: binding.optional(),
|
|
50
|
+
last: binding.optional(),
|
|
51
|
+
references: z.array(binding).max(9).optional()
|
|
52
|
+
}).strict().optional(),
|
|
53
|
+
graph: z.record(z.string(), node),
|
|
54
|
+
requiredModels: z.array(
|
|
55
|
+
z.object({ node: z.string(), input: z.string(), value: z.string() }).strict()
|
|
56
|
+
).default([]),
|
|
57
|
+
referencePrompt: z.enum(["minimax-tags"]).optional(),
|
|
58
|
+
guides: z.object({
|
|
59
|
+
node: node.optional(),
|
|
60
|
+
conditioning: z.tuple([z.string(), z.number().int()]),
|
|
61
|
+
latent: z.tuple([z.string(), z.number().int()]),
|
|
62
|
+
vae: z.tuple([z.string(), z.number().int()]),
|
|
63
|
+
targets: z.array(z.object({ node: z.string(), input: z.string() }).strict()).min(1)
|
|
64
|
+
}).strict().optional()
|
|
65
|
+
}).strict();
|
|
66
|
+
const videoSettingsSchema = z.object({
|
|
67
|
+
localEndpoint: z.string().url().refine((value) => {
|
|
68
|
+
const u = new URL(value);
|
|
69
|
+
return u.protocol === "http:" && ["127.0.0.1", "localhost", "[::1]"].includes(u.hostname) && u.pathname === "/" && !u.username && !u.password && !u.search && !u.hash;
|
|
70
|
+
}, "使用本机 ComfyUI HTTP 根地址").default("http://127.0.0.1:8188/"),
|
|
71
|
+
localInstallRoot: z.string().default(""),
|
|
72
|
+
ffmpegPath: z.string().default(""),
|
|
73
|
+
models: z.array(model).max(100).default([]),
|
|
74
|
+
templates: z.record(z.string(), template).default({})
|
|
75
|
+
}).strict();
|
|
76
|
+
export function validateVideoSettings(input) {
|
|
77
|
+
const value = videoSettingsSchema.parse(input);
|
|
78
|
+
if (new Set(value.models.map((m) => m.id)).size !== value.models.length)
|
|
79
|
+
throw new Error("模型 ID 重复");
|
|
80
|
+
for (const model2 of value.models) {
|
|
81
|
+
for (const required of ["duration", "fps", "width", "height"])
|
|
82
|
+
if (!model2.fields[required])
|
|
83
|
+
throw new Error(`缺少输出参数:${required}`);
|
|
84
|
+
for (const mode of Object.values(model2.modes)) {
|
|
85
|
+
const template2 = value.templates[mode.template];
|
|
86
|
+
if (!template2) throw new Error(`缺少工作流模板:${mode.template}`);
|
|
87
|
+
if (mode.middle && !template2.guides)
|
|
88
|
+
throw new Error("开放中间帧需要声明 guide 接线");
|
|
89
|
+
const placeholders = Object.values(template2.graph).flatMap(
|
|
90
|
+
(n) => Object.values(n.inputs)
|
|
91
|
+
);
|
|
92
|
+
if (mode.first && !template2.imageBindings?.first && !placeholders.includes("$first"))
|
|
93
|
+
throw new Error("首帧能力没有绑定工作流输入");
|
|
94
|
+
if (mode.last && !template2.imageBindings?.last && !placeholders.includes("$last"))
|
|
95
|
+
throw new Error("尾帧能力没有绑定工作流输入");
|
|
96
|
+
if (mode.references && !template2.imageBindings?.references?.length && !placeholders.includes("$reference1"))
|
|
97
|
+
throw new Error("参考素材能力没有绑定工作流输入");
|
|
98
|
+
const bindings = Object.values(template2.imageBindings ?? {}).flat();
|
|
99
|
+
for (const binding2 of [
|
|
100
|
+
...bindings,
|
|
101
|
+
...template2.guides?.targets ?? []
|
|
102
|
+
])
|
|
103
|
+
if (!template2.graph[binding2.node])
|
|
104
|
+
throw new Error("工作流绑定目标不存在");
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
return value;
|
|
108
|
+
}
|
|
109
|
+
class VideoModels {
|
|
110
|
+
constructor(directory, fetcher = fetch, runtime, {settingsSnapshot} = {}) {
|
|
111
|
+
this.directory = directory;
|
|
112
|
+
this.settingsSnapshot = settingsSnapshot ? structuredClone(settingsSnapshot) : null;
|
|
113
|
+
this.fetcher = fetcher;
|
|
114
|
+
this.runtime = runtime;
|
|
115
|
+
}
|
|
116
|
+
directory;
|
|
117
|
+
fetcher;
|
|
118
|
+
runtime;
|
|
119
|
+
catalogCache;
|
|
120
|
+
async get() {
|
|
121
|
+
if(this.settingsSnapshot)return structuredClone(this.settingsSnapshot.settings);
|
|
122
|
+
try {
|
|
123
|
+
const info=await stat(join(this.directory,"video.json"));if(!info.isFile()||info.size>32*1024*1024)throw new Error("视频设置文件无效或过大");
|
|
124
|
+
return videoSettingsSchema.parse(
|
|
125
|
+
JSON.parse(await readFile(join(this.directory, "video.json"), "utf8"))
|
|
126
|
+
);
|
|
127
|
+
} catch (e) {
|
|
128
|
+
if (e.code === "ENOENT")
|
|
129
|
+
return videoSettingsSchema.parse({});
|
|
130
|
+
throw e;
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
async save(input) {
|
|
134
|
+
if(this.settingsSnapshot)throw Object.assign(new Error("全局视频配置只能从所有者的美术环境设置更新"),{code:"video-global-settings-required"});
|
|
135
|
+
const value=validateVideoSettings(input);
|
|
136
|
+
await mkdir(this.directory, { recursive: true });
|
|
137
|
+
await atomicJson(join(this.directory, "video.json"), value);
|
|
138
|
+
this.catalogCache = void 0;
|
|
139
|
+
return value;
|
|
140
|
+
}
|
|
141
|
+
async ensure(config, modelId, signal, mode) {
|
|
142
|
+
if (!this.runtime) return;
|
|
143
|
+
await this.runtime.ensure(config, signal);
|
|
144
|
+
const live = await this.loadCatalog(config, true);
|
|
145
|
+
const model2 = live.find((m) => m.id === modelId);
|
|
146
|
+
if (!model2?.available)
|
|
147
|
+
throw new Error(model2?.reason || "本地方案运行校验失败");
|
|
148
|
+
if (mode && !Object.hasOwn(model2.modes, mode))
|
|
149
|
+
throw new Error("当前环境不支持所选生成方式");
|
|
150
|
+
this.catalogCache = void 0;
|
|
151
|
+
}
|
|
152
|
+
async catalog(input) {
|
|
153
|
+
const config = input ?? await this.get();
|
|
154
|
+
if (!config.models.length) return [];
|
|
155
|
+
const key = JSON.stringify(config);
|
|
156
|
+
if (this.catalogCache?.key === key && this.catalogCache.until > Date.now())
|
|
157
|
+
return structuredClone(await this.catalogCache.value);
|
|
158
|
+
const value = this.loadCatalog(config);
|
|
159
|
+
const entry = { key, until: Infinity, value };
|
|
160
|
+
this.catalogCache = entry;
|
|
161
|
+
try {
|
|
162
|
+
const models = await value;
|
|
163
|
+
entry.until = Date.now() + (models.some((m) => m.available) ? 3e4 : 3e3);
|
|
164
|
+
return structuredClone(models);
|
|
165
|
+
} catch (error) {
|
|
166
|
+
if (this.catalogCache === entry) this.catalogCache = void 0;
|
|
167
|
+
throw error;
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
async loadCatalog(config, requireLive = false) {
|
|
171
|
+
if (this.runtime) {
|
|
172
|
+
const issues = await this.runtime.inspect(config);
|
|
173
|
+
if (issues.length)
|
|
174
|
+
return config.models.map((m) => ({
|
|
175
|
+
...m,
|
|
176
|
+
available: false,
|
|
177
|
+
reason: issues.join(";")
|
|
178
|
+
}));
|
|
179
|
+
}
|
|
180
|
+
let nodes;
|
|
181
|
+
try {
|
|
182
|
+
const response = await this.fetcher(
|
|
183
|
+
new URL("object_info", config.localEndpoint),
|
|
184
|
+
{ signal: AbortSignal.timeout(5e3) }
|
|
185
|
+
);
|
|
186
|
+
if (!response.ok) throw new Error(`HTTP ${response.status}`);
|
|
187
|
+
nodes = await response.json();
|
|
188
|
+
} catch {
|
|
189
|
+
if (this.runtime && !requireLive)
|
|
190
|
+
return config.models.map((m) => ({
|
|
191
|
+
...m,
|
|
192
|
+
available: true,
|
|
193
|
+
reason: "本地文件检查通过,生成时启动并校验服务"
|
|
194
|
+
}));
|
|
195
|
+
return config.models.map((m) => ({
|
|
196
|
+
...m,
|
|
197
|
+
available: false,
|
|
198
|
+
reason: "ComfyUI 不可连接"
|
|
199
|
+
}));
|
|
200
|
+
}
|
|
201
|
+
return config.models.map((m) => {
|
|
202
|
+
const reasons = [];
|
|
203
|
+
const availableModes = {};
|
|
204
|
+
for (const [modeId, mode] of Object.entries(m.modes)) {
|
|
205
|
+
const before = reasons.length;
|
|
206
|
+
const t = config.templates[mode.template];
|
|
207
|
+
if (!t) {
|
|
208
|
+
reasons.push("未配置工作流");
|
|
209
|
+
continue;
|
|
210
|
+
}
|
|
211
|
+
for (const n of Object.values(t.graph)) {
|
|
212
|
+
if (!nodes[n.class_type]) reasons.push(`缺少节点 ${n.class_type}`);
|
|
213
|
+
for (const [key, value] of Object.entries(n.inputs)) {
|
|
214
|
+
const choices = nodes[n.class_type]?.input?.required?.[key]?.[0] ?? nodes[n.class_type]?.input?.optional?.[key]?.[0];
|
|
215
|
+
if (Array.isArray(choices) && typeof value === "string" && !value.startsWith("$") && !choices.includes(value))
|
|
216
|
+
reasons.push(`节点 ${n.class_type} 缺少参数或权重 ${value}`);
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
if ((mode.first || mode.last || mode.references) && !nodes.LoadImage)
|
|
220
|
+
reasons.push("缺少图片加载节点");
|
|
221
|
+
if (mode.middle && (!nodes[t.guides?.node?.class_type ?? "MiniMaxH3AddGuide"] || !nodes.LoadImage || !t.guides))
|
|
222
|
+
reasons.push("中间帧节点或接线缺失");
|
|
223
|
+
for (const r of t.requiredModels) {
|
|
224
|
+
const n = t.graph[r.node];
|
|
225
|
+
const choices = nodes[n?.class_type]?.input?.required?.[r.input]?.[0] ?? nodes[n?.class_type]?.input?.optional?.[r.input]?.[0];
|
|
226
|
+
if (!Array.isArray(choices) || !choices.includes(r.value))
|
|
227
|
+
reasons.push(`缺少权重 ${r.value}`);
|
|
228
|
+
}
|
|
229
|
+
if (before === reasons.length) availableModes[modeId] = mode;
|
|
230
|
+
}
|
|
231
|
+
return {
|
|
232
|
+
...m,
|
|
233
|
+
modes: availableModes,
|
|
234
|
+
available: Object.keys(availableModes).length > 0,
|
|
235
|
+
reason: [...new Set(reasons)].join(";")
|
|
236
|
+
};
|
|
237
|
+
});
|
|
238
|
+
}
|
|
239
|
+
}
|
|
240
|
+
function compileVideo(config, model2, settings, images) {
|
|
241
|
+
const definition = settings.templates[model2.modes[config.mode].template];
|
|
242
|
+
if (!definition) throw new Error("工作流模板不存在");
|
|
243
|
+
const output = videoOutput(config, model2);
|
|
244
|
+
const values = {
|
|
245
|
+
...config.parameters,
|
|
246
|
+
prompt: config.prompt,
|
|
247
|
+
frames: output.frames
|
|
248
|
+
};
|
|
249
|
+
for (const kind of ["first", "last"]) {
|
|
250
|
+
const input = config.inputs.find((i) => i.kind === kind);
|
|
251
|
+
if (input) values[kind] = images.get(input.assetId);
|
|
252
|
+
}
|
|
253
|
+
config.inputs.filter((i) => i.kind === "reference").forEach((i, index) => {
|
|
254
|
+
values[`reference${index + 1}`] = images.get(i.assetId);
|
|
255
|
+
});
|
|
256
|
+
const refs = config.inputs.filter((i) => i.kind === "reference");
|
|
257
|
+
if (refs.length && definition.referencePrompt === "minimax-tags")
|
|
258
|
+
values.prompt = config.prompt + "\n" + refs.map(
|
|
259
|
+
(i, n) => `<Picture ${n + 1}>: ${{ subject: "主体造型与身份", style: "绘制风格", motion: "动作参考", camera: "镜头构图" }[i.role ?? "subject"]}。`
|
|
260
|
+
).join("\n");
|
|
261
|
+
const graph = structuredClone(definition.graph);
|
|
262
|
+
for (const [kind, targets] of Object.entries(
|
|
263
|
+
definition.imageBindings ?? {}
|
|
264
|
+
)) {
|
|
265
|
+
const bindings = Array.isArray(targets) ? targets : [targets];
|
|
266
|
+
const selected = config.inputs.filter(
|
|
267
|
+
(i) => i.kind === (kind === "references" ? "reference" : kind)
|
|
268
|
+
);
|
|
269
|
+
if (selected.length > bindings.length)
|
|
270
|
+
throw new Error("参考图片数量超过模板接线范围");
|
|
271
|
+
selected.forEach((input, index) => {
|
|
272
|
+
const target = bindings[index], nodeId = `vant_bound_${kind}_${index}`;
|
|
273
|
+
if (!graph[target.node] || graph[nodeId])
|
|
274
|
+
throw new Error("图片输入接线无效");
|
|
275
|
+
graph[nodeId] = {
|
|
276
|
+
class_type: "LoadImage",
|
|
277
|
+
inputs: { image: images.get(input.assetId) }
|
|
278
|
+
};
|
|
279
|
+
graph[target.node].inputs[target.input] = [nodeId, 0];
|
|
280
|
+
});
|
|
281
|
+
}
|
|
282
|
+
for (const n of Object.values(graph))
|
|
283
|
+
for (const [key, value] of Object.entries(n.inputs)) {
|
|
284
|
+
if (typeof value === "string" && /^\$[a-zA-Z0-9_]+$/.test(value)) {
|
|
285
|
+
if (value.slice(1) in values) n.inputs[key] = values[value.slice(1)];
|
|
286
|
+
else if (["$first", "$last"].includes(value)) delete n.inputs[key];
|
|
287
|
+
else throw new Error(`模板输入未绑定:${value}`);
|
|
288
|
+
}
|
|
289
|
+
}
|
|
290
|
+
if (definition.guides) {
|
|
291
|
+
let conditioning = definition.guides.conditioning;
|
|
292
|
+
config.inputs.filter((i) => i.kind === "middle").sort((a, b) => a.frame - b.frame).forEach((input, index) => {
|
|
293
|
+
const imageId = `vant_image_${index}`, guideId = `vant_guide_${index}`;
|
|
294
|
+
if (graph[imageId] || graph[guideId])
|
|
295
|
+
throw new Error("模板占用了关键帧节点 ID");
|
|
296
|
+
graph[imageId] = {
|
|
297
|
+
class_type: "LoadImage",
|
|
298
|
+
inputs: { image: images.get(input.assetId) }
|
|
299
|
+
};
|
|
300
|
+
const guide = structuredClone(
|
|
301
|
+
definition.guides.node ?? {
|
|
302
|
+
class_type: "MiniMaxH3AddGuide",
|
|
303
|
+
inputs: {
|
|
304
|
+
positive: "$conditioning",
|
|
305
|
+
latent: "$latent",
|
|
306
|
+
vae: "$vae",
|
|
307
|
+
image: "$image",
|
|
308
|
+
frame_idx: "$frame"
|
|
309
|
+
}
|
|
310
|
+
}
|
|
311
|
+
);
|
|
312
|
+
const bindings = {
|
|
313
|
+
$conditioning: conditioning,
|
|
314
|
+
$latent: definition.guides.latent,
|
|
315
|
+
$vae: definition.guides.vae,
|
|
316
|
+
$image: [imageId, 0],
|
|
317
|
+
$frame: input.frame
|
|
318
|
+
};
|
|
319
|
+
for (const [key, value] of Object.entries(guide.inputs))
|
|
320
|
+
if (typeof value === "string" && value in bindings)
|
|
321
|
+
guide.inputs[key] = bindings[value];
|
|
322
|
+
graph[guideId] = guide;
|
|
323
|
+
conditioning = [guideId, 0];
|
|
324
|
+
});
|
|
325
|
+
for (const target of definition.guides.targets) {
|
|
326
|
+
if (!graph[target.node]) throw new Error("关键帧目标节点不存在");
|
|
327
|
+
graph[target.node].inputs[target.input] = conditioning;
|
|
328
|
+
}
|
|
329
|
+
}
|
|
330
|
+
return graph;
|
|
331
|
+
}
|
|
332
|
+
export {
|
|
333
|
+
VideoModels,
|
|
334
|
+
compileVideo,
|
|
335
|
+
videoSettingsSchema
|
|
336
|
+
};
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import {realpath,lstat} from 'node:fs/promises';
|
|
2
|
+
import {realpathSync,statSync} from 'node:fs';
|
|
3
|
+
import {dirname,basename,join,isAbsolute,relative,resolve} from 'node:path';
|
|
4
|
+
|
|
5
|
+
const failure=message=>Object.assign(new Error(message),{code:'path-outside-workspace',status:422});
|
|
6
|
+
export function canonicalVideoRoot(root){if(typeof root!=='string'||!isAbsolute(root))throw failure('视频工程根目录无效');const actual=realpathSync.native(root);if(!statSync(actual).isDirectory())throw failure('视频工程根目录不是文件夹');return actual;}
|
|
7
|
+
|
|
8
|
+
/** Resolve existing and not-yet-created project files without following an escaping ancestor. */
|
|
9
|
+
export async function projectPath(root,path){
|
|
10
|
+
if(typeof root!=='string'||!isAbsolute(root)||typeof path!=='string'||path.includes('\0'))throw failure('视频路径或工程根目录无效');
|
|
11
|
+
const base=await realpath(root),target=resolve(base,path),delta=relative(base,target),lexicallyInside=!delta.startsWith('..')&&!isAbsolute(delta);
|
|
12
|
+
if(!isAbsolute(path)&&!lexicallyInside)throw failure('路径超出 workspace');
|
|
13
|
+
let ancestor=target;const missing=[];
|
|
14
|
+
for(;;){
|
|
15
|
+
try{const actual=join(await realpath(ancestor),...missing),inside=relative(base,actual);if(inside.startsWith('..')||isAbsolute(inside))throw failure('真实路径超出 workspace');return lexicallyInside?target:actual;}
|
|
16
|
+
catch(error){if(error.code!=='ENOENT')throw error;const existing=await lstat(ancestor).catch(cause=>{if(cause.code==='ENOENT')return null;throw cause;});if(existing?.isSymbolicLink())throw failure('视频路径包含不可解析的链接');const parent=dirname(ancestor);if(parent===ancestor)throw error;missing.unshift(basename(ancestor));ancestor=parent;}
|
|
17
|
+
}
|
|
18
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { spawn, execFile } from 'node:child_process';
|
|
2
|
+
const unknown=(message)=>Object.assign(new Error(message),{code:'video-process-unknown',unknown:true});
|
|
3
|
+
export async function execute(file,args,{cwd,signal,allowFailure=false,maxOutputChars=32*1024*1024,timeoutMs=15*60*1000}={}){
|
|
4
|
+
signal?.throwIfAborted();
|
|
5
|
+
return new Promise((resolve,reject)=>{
|
|
6
|
+
const child=spawn(file,args,{cwd,windowsHide:true,detached:process.platform!=='win32',stdio:['ignore','pipe','pipe']});let stdout='',stderr='',cancelled=false,settled=false,timer,stopTimer,outputError;
|
|
7
|
+
const stop=()=>{if(cancelled||child.exitCode!==null)return;cancelled=true;if(child.pid){if(process.platform==='win32')execFile('taskkill',['/PID',String(child.pid),'/T','/F'],{windowsHide:true},()=>{});else try{process.kill(-child.pid,'SIGKILL')}catch{}}stopTimer=setTimeout(()=>finish(unknown('视频子进程停止尚未确认,请核对原操作。')),15000);stopTimer.unref?.()};
|
|
8
|
+
const cleanup=()=>{clearTimeout(timer);clearTimeout(stopTimer);signal?.removeEventListener('abort',stop)};
|
|
9
|
+
const finish=(error,result)=>{if(settled)return;settled=true;cleanup();error?reject(error):resolve(result)};
|
|
10
|
+
const collect=key=>chunk=>{if((key==='out'?stdout.length:stderr.length)+chunk.length>maxOutputChars){outputError=unknown('视频子进程输出超过上限,请核对原回执。');stop();return}if(key==='out')stdout+=chunk;else stderr+=chunk};
|
|
11
|
+
child.stdout.setEncoding('utf8');child.stderr.setEncoding('utf8');child.stdout.on('data',collect('out'));child.stderr.on('data',collect('err'));
|
|
12
|
+
child.once('error',error=>finish(error));child.once('close',(code,processSignal)=>{if(outputError)return finish(outputError);if(cancelled||processSignal)return finish(unknown('UI 执行已停止,原操作结果需要核对。'));if(code!==0&&!allowFailure)return finish(Object.assign(new Error(stderr||`视频子进程退出 ${code}`),{code:'video-process-failed',exitCode:code,stdout,stderr}));finish(null,{code,stdout,stderr})});
|
|
13
|
+
signal?.addEventListener('abort',stop,{once:true});if(signal?.aborted)stop();timer=setTimeout(stop,timeoutMs);timer.unref?.();
|
|
14
|
+
});
|
|
15
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import {AsyncLocalStorage} from 'node:async_hooks';
|
|
2
|
+
import {mkdir,realpath} from 'node:fs/promises';
|
|
3
|
+
import {createHash} from 'node:crypto';
|
|
4
|
+
import {join,dirname,basename,resolve} from 'node:path';
|
|
5
|
+
import {projectPath} from './path.mjs';
|
|
6
|
+
const owners=new AsyncLocalStorage(),pending=new Map();
|
|
7
|
+
let sqliteModule;
|
|
8
|
+
export function assertVideoRuntime(version=process.versions.node){const [major,minor]=version.split('.').map(Number);if(!Number.isFinite(major)||!Number.isFinite(minor)||major<22||major===22&&minor<13)throw Object.assign(new Error('视频通道需要 Node.js 22.13 或更高版本;图片通道仍可使用当前运行时'),{code:'video-runtime-unsupported'});}
|
|
9
|
+
async function sqlite(){assertVideoRuntime();try{return await (sqliteModule??=import('node:sqlite'));}catch(error){throw Object.assign(new Error('视频通道需要可用的 Node.js SQLite 运行时'),{code:'video-runtime-unsupported',cause:error});}}
|
|
10
|
+
async function identityPath(path){try{return await realpath(path)}catch(error){if(error.code!=='ENOENT')throw error;return join(await identityPath(dirname(path)),basename(path))}}
|
|
11
|
+
// Same resource database identity as VantAgent, so both cooperating implementations exclude writes.
|
|
12
|
+
export async function withResource(root,resource,action){
|
|
13
|
+
const actual=await identityPath(resolve(root,resource));await projectPath(root,actual);const hash=createHash('sha256').update(process.platform==='win32'?actual.toLowerCase():actual).digest('hex'),file=await projectPath(root,`.vant/state/locks/${hash}.sqlite`),parent=owners.getStore()||new Set();if(parent.has(file))throw new Error('同一视频操作不能重复取得同一资源');const previous=pending.get(file)||Promise.resolve();
|
|
14
|
+
const work=previous.catch(()=>{}).then(async()=>{const {DatabaseSync}=await sqlite();await mkdir(dirname(file),{recursive:true});await projectPath(root,file);const database=new DatabaseSync(file);try{database.exec('PRAGMA busy_timeout=0; BEGIN IMMEDIATE')}catch(error){database.close();throw Object.assign(new Error(error.errcode===5?'另一个服务正在操作同一视频资源':'视频资源锁不可用'),{code:'video-resource-busy',cause:error})}try{return await owners.run(new Set([...parent,file]),()=>action())}finally{database.close()}});pending.set(file,work);try{return await work}finally{if(pending.get(file)===work)pending.delete(file)}
|
|
15
|
+
}
|