@kevlns/art-workshop 0.5.1 → 0.6.0
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/AGENTS.md +25 -25
- package/LICENSE +21 -21
- package/README.md +65 -63
- package/art-workshop.mjs +219 -174
- package/docs/channels.md +49 -0
- package/docs/commands.md +2259 -22
- package/docs/configuration.md +90 -82
- package/docs/core-guidance.md +58 -50
- package/docs/data-formats.md +89 -85
- package/docs/deployment.md +32 -32
- package/docs/local-models/qwen-image-2.1.json +73 -73
- package/docs/local-models/qwen-image-2.1.md +33 -33
- package/docs/model-architecture.md +78 -78
- package/docs/modes.md +73 -67
- package/docs/release-0.6.0.md +9 -0
- package/lib/agent.mjs +32 -32
- package/lib/analysis-worker.mjs +36 -32
- package/lib/analyze.mjs +85 -85
- package/lib/asset-profile.mjs +23 -23
- package/lib/backends.mjs +2 -0
- package/lib/candidates.mjs +104 -103
- package/lib/channel-operation.mjs +84 -0
- package/lib/comfy-recovery.mjs +17 -17
- package/lib/conversation.mjs +77 -77
- package/lib/core.mjs +104 -75
- package/lib/deployment-guide.mjs +12 -12
- package/lib/design.mjs +29 -29
- package/lib/dimensions.mjs +22 -22
- package/lib/edit.mjs +43 -41
- package/lib/environment-operations.mjs +49 -49
- package/lib/environment-runtime.mjs +99 -99
- package/lib/environments.mjs +55 -55
- package/lib/executor.mjs +107 -107
- package/lib/generate.mjs +60 -58
- package/lib/generation-policy.mjs +1 -1
- package/lib/heartbeat.mjs +16 -16
- package/lib/images.mjs +1 -1
- package/lib/lifecycle.mjs +53 -53
- package/lib/migration.mjs +154 -154
- package/lib/object-format.mjs +1 -1
- package/lib/process-identity.mjs +22 -14
- package/lib/project.mjs +39 -39
- package/lib/prompt-structure.mjs +3 -3
- package/lib/refs.mjs +26 -26
- package/lib/runtime.mjs +25 -25
- package/lib/service.mjs +155 -126
- package/lib/settings.mjs +50 -50
- package/lib/shared.mjs +67 -67
- package/lib/style-bundle.mjs +98 -85
- package/lib/style-contract.mjs +74 -74
- package/lib/style-lifecycle.mjs +124 -125
- package/lib/style-review.mjs +14 -14
- package/lib/style-samples.mjs +92 -0
- package/lib/task-contract.mjs +94 -93
- package/lib/tasks.mjs +497 -497
- package/lib/templates.mjs +80 -80
- 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/vision.mjs +56 -56
- package/lib/worker-process.mjs +6 -6
- package/lib/workspace.mjs +305 -292
- package/package.json +8 -3
- package/rules/core-guidance.json +22 -22
- package/skills/art-workshop/SKILL.md +19 -17
- package/skills/art-workshop/references/conversation.md +11 -11
- package/skills/art-workshop/references/expression.md +27 -27
- package/skills/art-workshop/references/project.md +119 -109
- package/skills/art-workshop/references/style-discussion.md +7 -3
- package/skills/art-workshop/references/video.md +13 -0
- package/v-cli.plugin.json +7240 -2747
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
const videoModeLabels = {
|
|
2
|
+
text: "纯文本创作",
|
|
3
|
+
boundary: "固定首/尾帧",
|
|
4
|
+
reference: "参考素材创作"
|
|
5
|
+
};
|
|
6
|
+
const videoBusy = (op) => ["dispatching", "running", "unknown"].includes(op.runs.at(-1)?.status ?? "");
|
|
7
|
+
const videoComplete = (op) => op.runs.at(-1)?.status === "succeeded";
|
|
8
|
+
function defaultVideoConfig(model) {
|
|
9
|
+
return {
|
|
10
|
+
modelId: model?.id ?? "",
|
|
11
|
+
mode: Object.keys(model?.modes ?? { text: {} })[0],
|
|
12
|
+
prompt: "",
|
|
13
|
+
parameters: Object.fromEntries(
|
|
14
|
+
Object.entries(model?.fields ?? {}).map(([key, field]) => [
|
|
15
|
+
key,
|
|
16
|
+
field.default
|
|
17
|
+
])
|
|
18
|
+
),
|
|
19
|
+
inputs: []
|
|
20
|
+
};
|
|
21
|
+
}
|
|
22
|
+
function videoOutput(config, model) {
|
|
23
|
+
const fps = Number(config.parameters.fps), duration = Number(config.parameters.duration);
|
|
24
|
+
const { multiple, offset, maximum } = model.frames;
|
|
25
|
+
if (!Number.isFinite(fps) || fps <= 0 || !Number.isFinite(duration) || duration <= 0 || !Number.isInteger(multiple) || multiple < 1)
|
|
26
|
+
throw new Error("时长或帧率无效");
|
|
27
|
+
const frames = Math.max(
|
|
28
|
+
offset,
|
|
29
|
+
Math.ceil((Math.round(fps * duration) - offset) / multiple) * multiple + offset
|
|
30
|
+
);
|
|
31
|
+
if (!Number.isSafeInteger(frames) || frames < 1 || frames > maximum)
|
|
32
|
+
throw new Error("总帧数超出模型范围");
|
|
33
|
+
return {
|
|
34
|
+
fps,
|
|
35
|
+
frames,
|
|
36
|
+
duration: frames / fps,
|
|
37
|
+
width: Number(config.parameters.width),
|
|
38
|
+
height: Number(config.parameters.height)
|
|
39
|
+
};
|
|
40
|
+
}
|
|
41
|
+
function validateVideo(config, model) {
|
|
42
|
+
const errors = [];
|
|
43
|
+
if (config.modelId !== model.id) errors.push("模型身份不匹配");
|
|
44
|
+
if (!config.prompt.trim()) errors.push("请填写本次内容");
|
|
45
|
+
const mode = model.modes[config.mode];
|
|
46
|
+
if (!mode) errors.push("模型不支持此工作方式");
|
|
47
|
+
for (const key of Object.keys(config.parameters))
|
|
48
|
+
if (!model.fields[key]) errors.push(`不支持的参数:${key}`);
|
|
49
|
+
for (const [key, field] of Object.entries(model.fields)) {
|
|
50
|
+
const value = config.parameters[key];
|
|
51
|
+
const numeric = field.type === "number" || field.type === "integer";
|
|
52
|
+
if (typeof value !== (numeric ? "number" : field.type) || numeric && (!Number.isFinite(value) || field.type === "integer" && !Number.isSafeInteger(value))) {
|
|
53
|
+
errors.push(`${field.label}类型无效`);
|
|
54
|
+
continue;
|
|
55
|
+
}
|
|
56
|
+
if (field.enum && !field.enum.includes(value))
|
|
57
|
+
errors.push(`${field.label}不在允许值中`);
|
|
58
|
+
if (numeric) {
|
|
59
|
+
const n = value;
|
|
60
|
+
if (field.minimum !== void 0 && n < field.minimum || field.maximum !== void 0 && n > field.maximum)
|
|
61
|
+
errors.push(`${field.label}超出范围`);
|
|
62
|
+
if (field.step && Math.abs(n / field.step - Math.round(n / field.step)) > 1e-6)
|
|
63
|
+
errors.push(`${field.label}步长无效`);
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
try {
|
|
67
|
+
const output = videoOutput(config, model);
|
|
68
|
+
if (!Number.isInteger(output.width) || !Number.isInteger(output.height) || output.width <= 0 || output.height <= 0 || output.width * output.height > model.maximumPixels)
|
|
69
|
+
errors.push("分辨率超出模型范围");
|
|
70
|
+
const occupied = /* @__PURE__ */ new Set();
|
|
71
|
+
for (const input of config.inputs) {
|
|
72
|
+
if (!mode?.[input.kind === "reference" ? "references" : input.kind])
|
|
73
|
+
errors.push("当前工作方式不支持已引入的约束");
|
|
74
|
+
if (input.kind === "middle" && (!Number.isInteger(input.frame) || input.frame <= 0 || input.frame >= output.frames - 1))
|
|
75
|
+
errors.push("中间帧位置超出范围");
|
|
76
|
+
const key = input.kind === "reference" ? `reference:${input.assetId}` : input.kind === "middle" ? `middle:${input.frame}` : input.kind;
|
|
77
|
+
if (occupied.has(key)) errors.push("同一位置不能重复引入画面");
|
|
78
|
+
occupied.add(key);
|
|
79
|
+
}
|
|
80
|
+
} catch (e) {
|
|
81
|
+
errors.push(e.message);
|
|
82
|
+
}
|
|
83
|
+
if (mode?.requireBoundary && !config.inputs.some((i) => i.kind === "first" || i.kind === "last"))
|
|
84
|
+
errors.push("至少引入首帧或尾帧");
|
|
85
|
+
if (mode?.references && !config.inputs.some((i) => i.kind === "reference"))
|
|
86
|
+
errors.push("请添加参考素材");
|
|
87
|
+
return [...new Set(errors)];
|
|
88
|
+
}
|
|
89
|
+
function timelinePosition(value, unit, fps, frames) {
|
|
90
|
+
let n;
|
|
91
|
+
if (unit === "frame") {
|
|
92
|
+
if (!/^F?\d+$/i.test(value.trim())) throw new Error("请输入整数帧号");
|
|
93
|
+
n = Number(value.trim().replace(/^F/i, ""));
|
|
94
|
+
} else {
|
|
95
|
+
const raw = value.trim().replace(/(?:s|秒)$/i, "");
|
|
96
|
+
if (!/^(?:\d+:){0,2}\d+(?:\.\d+)?$/.test(raw))
|
|
97
|
+
throw new Error("请输入秒数或时分秒");
|
|
98
|
+
const parts = raw.split(":").map(Number);
|
|
99
|
+
if (parts.length > 1 && parts.slice(1).some((x) => x >= 60))
|
|
100
|
+
throw new Error("时分秒格式无效");
|
|
101
|
+
const seconds = parts.reduce((total, part) => total * 60 + part, 0);
|
|
102
|
+
if (seconds > frames / fps + 5e-4) throw new Error("时间超出范围");
|
|
103
|
+
n = Math.min(frames - 1, Math.round(seconds * fps));
|
|
104
|
+
}
|
|
105
|
+
if (!Number.isSafeInteger(n) || n < 0 || n >= frames)
|
|
106
|
+
throw new Error("位置超出范围");
|
|
107
|
+
return n;
|
|
108
|
+
}
|
|
109
|
+
export {
|
|
110
|
+
defaultVideoConfig,
|
|
111
|
+
timelinePosition,
|
|
112
|
+
validateVideo,
|
|
113
|
+
videoBusy,
|
|
114
|
+
videoComplete,
|
|
115
|
+
videoModeLabels,
|
|
116
|
+
videoOutput
|
|
117
|
+
};
|
package/lib/video/h3.mjs
ADDED
|
@@ -0,0 +1,232 @@
|
|
|
1
|
+
import { localMediaModels } from "./contracts.mjs";
|
|
2
|
+
function h3Preset() {
|
|
3
|
+
const graph = {
|
|
4
|
+
"1": {
|
|
5
|
+
class_type: "UNETLoader",
|
|
6
|
+
inputs: {
|
|
7
|
+
unet_name: localMediaModels.video[0].split("/").at(-1),
|
|
8
|
+
weight_dtype: "default"
|
|
9
|
+
}
|
|
10
|
+
},
|
|
11
|
+
"2": {
|
|
12
|
+
class_type: "CLIPLoader",
|
|
13
|
+
inputs: {
|
|
14
|
+
clip_name: localMediaModels.video[2].split("/").at(-1),
|
|
15
|
+
type: "minimax",
|
|
16
|
+
device: "default"
|
|
17
|
+
}
|
|
18
|
+
},
|
|
19
|
+
"3": {
|
|
20
|
+
class_type: "VAELoader",
|
|
21
|
+
inputs: { vae_name: localMediaModels.video[3].split("/").at(-1) }
|
|
22
|
+
},
|
|
23
|
+
"4": {
|
|
24
|
+
class_type: "VAELoader",
|
|
25
|
+
inputs: { vae_name: localMediaModels.video[4].split("/").at(-1) }
|
|
26
|
+
},
|
|
27
|
+
"5": {
|
|
28
|
+
class_type: "MiniMaxH3ImageToVideo",
|
|
29
|
+
inputs: {
|
|
30
|
+
clip: ["2", 0],
|
|
31
|
+
vae: ["3", 0],
|
|
32
|
+
prompt: "$prompt",
|
|
33
|
+
width: "$width",
|
|
34
|
+
height: "$height",
|
|
35
|
+
length: "$frames"
|
|
36
|
+
}
|
|
37
|
+
},
|
|
38
|
+
"6": {
|
|
39
|
+
class_type: "BasicGuider",
|
|
40
|
+
inputs: { model: ["1", 0], conditioning: ["5", 0] }
|
|
41
|
+
},
|
|
42
|
+
"7": { class_type: "KSamplerSelect", inputs: { sampler_name: "$sampler" } },
|
|
43
|
+
"8": {
|
|
44
|
+
class_type: "BasicScheduler",
|
|
45
|
+
inputs: {
|
|
46
|
+
model: ["1", 0],
|
|
47
|
+
scheduler: "$scheduler",
|
|
48
|
+
steps: "$steps",
|
|
49
|
+
denoise: 1
|
|
50
|
+
}
|
|
51
|
+
},
|
|
52
|
+
"9": { class_type: "RandomNoise", inputs: { noise_seed: "$seed" } },
|
|
53
|
+
"10": {
|
|
54
|
+
class_type: "SamplerCustomAdvanced",
|
|
55
|
+
inputs: {
|
|
56
|
+
noise: ["9", 0],
|
|
57
|
+
guider: ["6", 0],
|
|
58
|
+
sampler: ["7", 0],
|
|
59
|
+
sigmas: ["8", 0],
|
|
60
|
+
latent_image: ["5", 1]
|
|
61
|
+
}
|
|
62
|
+
},
|
|
63
|
+
"11": {
|
|
64
|
+
class_type: "VAEDecode",
|
|
65
|
+
inputs: { samples: ["10", 0], vae: ["3", 0] }
|
|
66
|
+
},
|
|
67
|
+
"12": {
|
|
68
|
+
class_type: "VAEDecodeAudio",
|
|
69
|
+
inputs: { samples: ["10", 0], vae: ["4", 0] }
|
|
70
|
+
},
|
|
71
|
+
"13": {
|
|
72
|
+
class_type: "CreateVideo",
|
|
73
|
+
inputs: {
|
|
74
|
+
images: ["11", 0],
|
|
75
|
+
audio: ["12", 0],
|
|
76
|
+
fps: "$fps",
|
|
77
|
+
bit_depth: 8,
|
|
78
|
+
color_space: "sRGB"
|
|
79
|
+
}
|
|
80
|
+
},
|
|
81
|
+
"14": {
|
|
82
|
+
class_type: "SaveVideo",
|
|
83
|
+
inputs: {
|
|
84
|
+
video: ["13", 0],
|
|
85
|
+
filename_prefix: "vant/video",
|
|
86
|
+
format: "mp4",
|
|
87
|
+
"format.codec": "h264"
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
};
|
|
91
|
+
const requiredModels = [
|
|
92
|
+
{ node: "1", input: "unet_name", value: graph["1"].inputs.unet_name },
|
|
93
|
+
{ node: "2", input: "clip_name", value: graph["2"].inputs.clip_name },
|
|
94
|
+
{ node: "3", input: "vae_name", value: graph["3"].inputs.vae_name },
|
|
95
|
+
{ node: "4", input: "vae_name", value: graph["4"].inputs.vae_name }
|
|
96
|
+
];
|
|
97
|
+
const reference = structuredClone(graph);
|
|
98
|
+
reference["1"].inputs.unet_name = localMediaModels.video[1].split("/").at(-1);
|
|
99
|
+
reference["5"] = {
|
|
100
|
+
class_type: "MiniMaxH3ReferenceToVideo",
|
|
101
|
+
inputs: {
|
|
102
|
+
clip: ["2", 0],
|
|
103
|
+
vae: ["3", 0],
|
|
104
|
+
audio_vae: ["4", 0],
|
|
105
|
+
prompt: "$prompt",
|
|
106
|
+
width: "$width",
|
|
107
|
+
height: "$height",
|
|
108
|
+
length: "$frames",
|
|
109
|
+
ref_image_size: "match"
|
|
110
|
+
}
|
|
111
|
+
};
|
|
112
|
+
return {
|
|
113
|
+
models: [
|
|
114
|
+
{
|
|
115
|
+
id: "minimax-h3",
|
|
116
|
+
name: "MiniMax H3 · 本地 ComfyUI",
|
|
117
|
+
adapter: "comfyui",
|
|
118
|
+
fields: {
|
|
119
|
+
duration: {
|
|
120
|
+
label: "期望时长 / 秒",
|
|
121
|
+
type: "number",
|
|
122
|
+
default: 8,
|
|
123
|
+
minimum: 0.2,
|
|
124
|
+
maximum: 30,
|
|
125
|
+
step: 0.01
|
|
126
|
+
},
|
|
127
|
+
fps: { label: "帧率", type: "integer", default: 24, enum: [24] },
|
|
128
|
+
width: {
|
|
129
|
+
label: "宽度",
|
|
130
|
+
type: "integer",
|
|
131
|
+
default: 864,
|
|
132
|
+
enum: [768, 864, 1024],
|
|
133
|
+
step: 32
|
|
134
|
+
},
|
|
135
|
+
height: {
|
|
136
|
+
label: "高度",
|
|
137
|
+
type: "integer",
|
|
138
|
+
default: 480,
|
|
139
|
+
enum: [480, 512, 576],
|
|
140
|
+
step: 32
|
|
141
|
+
},
|
|
142
|
+
seed: {
|
|
143
|
+
label: "随机种子",
|
|
144
|
+
type: "integer",
|
|
145
|
+
default: 1,
|
|
146
|
+
minimum: 0,
|
|
147
|
+
maximum: Number.MAX_SAFE_INTEGER,
|
|
148
|
+
step: 1
|
|
149
|
+
},
|
|
150
|
+
steps: {
|
|
151
|
+
label: "采样步数",
|
|
152
|
+
type: "integer",
|
|
153
|
+
default: 20,
|
|
154
|
+
minimum: 1,
|
|
155
|
+
maximum: 60,
|
|
156
|
+
step: 1
|
|
157
|
+
},
|
|
158
|
+
sampler: {
|
|
159
|
+
label: "采样器",
|
|
160
|
+
type: "string",
|
|
161
|
+
default: "res_multistep",
|
|
162
|
+
enum: ["res_multistep"]
|
|
163
|
+
},
|
|
164
|
+
scheduler: {
|
|
165
|
+
label: "调度器",
|
|
166
|
+
type: "string",
|
|
167
|
+
default: "simple",
|
|
168
|
+
enum: ["simple"]
|
|
169
|
+
}
|
|
170
|
+
},
|
|
171
|
+
modes: {
|
|
172
|
+
text: {
|
|
173
|
+
first: false,
|
|
174
|
+
last: false,
|
|
175
|
+
middle: true,
|
|
176
|
+
references: false,
|
|
177
|
+
template: "h3-fl2va"
|
|
178
|
+
},
|
|
179
|
+
boundary: {
|
|
180
|
+
first: true,
|
|
181
|
+
last: true,
|
|
182
|
+
middle: true,
|
|
183
|
+
references: false,
|
|
184
|
+
requireBoundary: true,
|
|
185
|
+
template: "h3-fl2va"
|
|
186
|
+
},
|
|
187
|
+
reference: {
|
|
188
|
+
first: false,
|
|
189
|
+
last: false,
|
|
190
|
+
middle: false,
|
|
191
|
+
references: true,
|
|
192
|
+
template: "h3-ref2va"
|
|
193
|
+
}
|
|
194
|
+
},
|
|
195
|
+
frames: { multiple: 17, offset: 5, maximum: 736 },
|
|
196
|
+
maximumPixels: 589824
|
|
197
|
+
}
|
|
198
|
+
],
|
|
199
|
+
templates: {
|
|
200
|
+
"h3-fl2va": {
|
|
201
|
+
graph,
|
|
202
|
+
requiredModels,
|
|
203
|
+
imageBindings: {
|
|
204
|
+
first: { node: "5", input: "first_frame" },
|
|
205
|
+
last: { node: "5", input: "last_frame" }
|
|
206
|
+
},
|
|
207
|
+
guides: {
|
|
208
|
+
conditioning: ["5", 0],
|
|
209
|
+
latent: ["5", 1],
|
|
210
|
+
vae: ["3", 0],
|
|
211
|
+
targets: [{ node: "6", input: "conditioning" }]
|
|
212
|
+
}
|
|
213
|
+
},
|
|
214
|
+
"h3-ref2va": {
|
|
215
|
+
referencePrompt: "minimax-tags",
|
|
216
|
+
graph: reference,
|
|
217
|
+
requiredModels: requiredModels.map(
|
|
218
|
+
(r) => r.node === "1" ? { ...r, value: String(reference["1"].inputs.unet_name) } : r
|
|
219
|
+
),
|
|
220
|
+
imageBindings: {
|
|
221
|
+
references: Array.from({ length: 9 }, (_, n) => ({
|
|
222
|
+
node: "5",
|
|
223
|
+
input: `ref_images.ref_image_${n}`
|
|
224
|
+
}))
|
|
225
|
+
}
|
|
226
|
+
}
|
|
227
|
+
}
|
|
228
|
+
};
|
|
229
|
+
}
|
|
230
|
+
export {
|
|
231
|
+
h3Preset
|
|
232
|
+
};
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import {stat} from 'node:fs/promises';
|
|
2
|
+
import {join,isAbsolute,resolve,relative} from 'node:path';
|
|
3
|
+
import {pathToFileURL} from 'node:url';
|
|
4
|
+
import {createHash} from 'node:crypto';
|
|
5
|
+
import {isDeepStrictEqual} from 'node:util';
|
|
6
|
+
import {canonicalVideoRoot} from './path.mjs';
|
|
7
|
+
export const assertLocalEndpoint=value=>{const u=new URL(value);if(u.protocol!=='http:'||!['127.0.0.1','localhost','[::1]'].includes(u.hostname)||u.pathname!=='/'||u.username||u.password||u.search||u.hash)throw new Error('视频服务仅支持已配置的本机 HTTP 根地址');return u};
|
|
8
|
+
const exists=path=>stat(path).then(value=>value.isFile()&&value.size>0,()=>false);
|
|
9
|
+
export class LocalVideoRuntime{
|
|
10
|
+
constructor({root,instanceId,sdkRoot,fetcher=fetch}={}){this.root=root?canonicalVideoRoot(root):root;this.prefix=`dsh-vant-video-${createHash('sha256').update(this.root||'').digest('hex').slice(0,16)}-`;this.instanceId=instanceId?.startsWith(this.prefix)?instanceId:this.prefix+(instanceId||'reader');this.sdkRoot=sdkRoot||null;this.fetcher=fetcher;this.liveCache=new Map();}
|
|
11
|
+
async sdk(){const service=await import(this.sdkRoot?pathToFileURL(join(this.sdkRoot,'@kevlns/art-workshop/lib/service.mjs')).href:new URL('../service.mjs',import.meta.url).href).catch(()=>null);if(!service?.ensureService||!service.closeService||!service.rememberedService||!service.processIdentity)throw new Error('受管本地视频运行时 SDK 不可用;请安装兼容的 art-workshop');return service}
|
|
12
|
+
async reachable(config,signal){const url=assertLocalEndpoint(config.localEndpoint),key=url.href,cached=this.liveCache.get(key);if(!signal&&cached&&cached.until>Date.now())return cached.promise;const entry={until:Infinity,promise:null};entry.promise=(async()=>{try{const response=await this.fetcher(new URL('system_stats',url),{signal:signal?AbortSignal.any([signal,AbortSignal.timeout(3000)]):AbortSignal.timeout(3000)});return response.ok}catch{return false}finally{entry.until=Date.now()+2000}})();this.liveCache.set(key,entry);return entry.promise}
|
|
13
|
+
async python(root){for(const file of ['python_embeded/python.exe','.venv/Scripts/python.exe','.venv/bin/python'])if(await exists(join(root,file)))return join(root,file)}
|
|
14
|
+
async fileIssues(config){const root=config.localInstallRoot;if(!root||!isAbsolute(root))return ['ComfyUI 未运行;如需受管启动,请配置本地环境绝对目录'];const errors=[];if(!await this.python(root))errors.push('环境中缺少 Python');if(!await exists(join(root,'ComfyUI/main.py')))errors.push('环境中缺少 ComfyUI/main.py');const folders={UNETLoader:'diffusion_models',CLIPLoader:'text_encoders',VAELoader:'vae'};for(const model of config.models)for(const mode of Object.values(model.modes)){const template=config.templates[mode.template];if(!template){errors.push('缺少工作流 '+mode.template);continue}if(model.id==='minimax-h3'&&!await exists(join(root,'ComfyUI/comfy_extras/nodes_minimax_h3.py')))errors.push('缺少 MiniMax H3 原生节点');for(const item of template.requiredModels){const folder=folders[template.graph[item.node]?.class_type];if(!folder){errors.push('无法核对模型加载节点 '+item.node);continue}const base=resolve(root,'ComfyUI/models',folder),target=resolve(base,item.value),delta=relative(base,target);if(isAbsolute(delta)||delta.startsWith('..')||!await exists(target))errors.push('缺少模型 '+item.value)}}try{await this.sdk()}catch(error){errors.push(error.message)}return [...new Set(errors)]}
|
|
15
|
+
async inspect(config){assertLocalEndpoint(config.localEndpoint);if(await this.reachable(config))return [];return this.fileIssues(config)}
|
|
16
|
+
backend(config,python){return {id:'vant-local-video',workflow:'vant-video',template:{},bindings:{},endpoints:[config.localEndpoint],launch:{executable:python,cwd:config.localInstallRoot,args:['-s','ComfyUI/main.py','--disable-dynamic-vram','--lowvram','--disable-auto-launch','--listen','127.0.0.1','--port',new URL(config.localEndpoint).port||'80'],startupTimeoutSeconds:120}}}
|
|
17
|
+
async ensure(config,signal){signal.throwIfAborted();assertLocalEndpoint(config.localEndpoint);if(await this.reachable(config,signal))return;const errors=await this.fileIssues(config);if(errors.length)throw new Error(errors.join(';'));const sdk=await this.sdk();const previous=process.env.VANT_SERVICE_INSTANCE;process.env.VANT_SERVICE_INSTANCE=this.instanceId;try{await sdk.ensureService(this.backend(config,await this.python(config.localInstallRoot)),config.localInstallRoot,{cancelled:()=>signal.aborted})}finally{if(previous===undefined)delete process.env.VANT_SERVICE_INSTANCE;else process.env.VANT_SERVICE_INSTANCE=previous}this.liveCache.delete(new URL(config.localEndpoint).href);signal.throwIfAborted()}
|
|
18
|
+
async status(config){const running=await this.reachable(config),startupIssues=await this.fileIssues(config);let record=null,sdkAvailable=false;try{const sdk=await this.sdk();sdkAvailable=true;record=sdk.rememberedService(config.localInstallRoot,config.localEndpoint)}catch{}const owned=!!(record?.owned&&record.owner?.instanceId?.startsWith(this.prefix));return {mode:config.localInstallRoot?'managed-local':'external-existing',running,endpoint:config.localEndpoint,autoStartConfigured:!!config.localInstallRoot,autoStart:!!config.localInstallRoot&&!startupIssues.length,sdkAvailable,ownedByProject:owned,origin:owned?'managed-sdk':running?'external':'unavailable',startupIssues,issues:running?[]:startupIssues}}
|
|
19
|
+
async stop(config){const sdk=await this.sdk(),record=sdk.rememberedService(config.localInstallRoot,config.localEndpoint);if(!record?.owned||!record.owner?.instanceId?.startsWith(this.prefix)||!record.identity)throw new Error('当前服务不是此项目的受管服务,不会停止外部进程');const identity=await sdk.processIdentity(record.identity.pid);if(!isDeepStrictEqual(identity,record.identity))throw new Error('受管视频服务进程身份已变化,不会停止');if(typeof sdk.closeOwnedService!=='function')throw Object.assign(new Error('受管 SDK 缺少所有者绑定的安全关闭能力,不能关闭服务'),{code:'video-runtime-stop-unavailable'});const expected=structuredClone({owner:record.owner,identity:record.identity});try{return await sdk.closeOwnedService(config.localInstallRoot,config.localEndpoint,this.backend(config,await this.python(config.localInstallRoot)),expected)}catch(error){throw Object.assign(error,{unknown:true})}}
|
|
20
|
+
}
|
|
@@ -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
|
+
};
|