-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmediaEngine.js
More file actions
680 lines (624 loc) · 23.2 KB
/
Copy pathmediaEngine.js
File metadata and controls
680 lines (624 loc) · 23.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
'use strict';
const path = require('path');
const fs = require('fs');
const fsp = require('fs').promises;
const { spawn } = require('child_process');
const { readGgufMetadata, detectModelTypeFromGguf } = require('./modelDetection');
const {
archToMediaProfile,
getProfileGen,
TENSOR_5D_MSG,
is5dTensorStderr,
} = require('./mediaAssetsCatalog');
const { needs5dFix, apply5dFix, is5dCompatArch } = require('./gguf5dCompat');
const { VRAM_TIGHT_MB, VRAM_LOW_MB, WIN_DLL_NOT_FOUND, WIN_STACK_OVERRUN } = require('./mediaConstants');
const streamTrace = require('./streamTrace');
function queryGpuVramMB() {
try {
const { execSync } = require('child_process');
const out = execSync(
'nvidia-smi --query-gpu=memory.total --format=csv,noheader,nounits',
{ timeout: 5000 },
).toString().trim();
return parseInt(out.split('\n')[0], 10) || 0;
} catch {
return 0;
}
}
function resolveMediaMemoryFlags(settings = {}, vramMB = 0) {
if (settings.gpuPreference === 'cpu' || settings.mediaOffloadPolicy === 'max') {
return {
offloadToCpu: true,
vaeOnCpu: true,
clipOnCpu: true,
diffusionFa: true,
vaeConvDirect: !!settings.mediaTaePath,
vaeTiling: true,
};
}
if (settings.mediaOffloadPolicy === 'off') {
return {
offloadToCpu: false,
vaeOnCpu: false,
clipOnCpu: false,
diffusionFa: true,
vaeConvDirect: false,
vaeTiling: false,
};
}
const vram = vramMB > 0 ? vramMB : queryGpuVramMB();
const low = vram > 0 && vram <= VRAM_LOW_MB;
const tight = vram > 0 && vram <= VRAM_TIGHT_MB;
const unknownVram = vram <= 0;
return {
offloadToCpu: low || tight || unknownVram,
vaeOnCpu: low || unknownVram,
clipOnCpu: low || unknownVram,
diffusionFa: true,
vaeConvDirect: (low || unknownVram) && !!settings.mediaTaePath,
vaeTiling: low || unknownVram,
};
}
function getDefaultMediaDimensions(vramMB, isVideo, settings = {}) {
const vram = vramMB > 0 ? vramMB : queryGpuVramMB();
const conservative = vram <= 0 || vram <= VRAM_LOW_MB;
const tight = vram > 0 && vram <= VRAM_TIGHT_MB;
const preset = settings.mediaVideoResolution || 'auto';
let width = 512;
let height = 512;
let videoFrames = 33;
if (preset === 'fast' || (preset === 'auto' && conservative)) {
width = 384;
height = 384;
videoFrames = 17;
} else if (preset === 'balanced' || (preset === 'auto' && tight)) {
width = 480;
height = 480;
videoFrames = 25;
} else if (preset === 'quality') {
width = 512;
height = 512;
videoFrames = 49;
}
if (settings.mediaVideoFrames > 0) videoFrames = settings.mediaVideoFrames;
if (!isVideo) {
if (conservative && preset === 'auto') return { width: 384, height: 384, videoFrames: 1 };
return { width, height, videoFrames: 1 };
}
return { width, height, videoFrames };
}
const WAN_VIDEO_FPS = 16;
function estimateVideoDurationSec(frames) {
return Math.round((frames / WAN_VIDEO_FPS) * 10) / 10;
}
async function tryRemuxVideoToMp4(inputPath) {
if (!inputPath || !fs.existsSync(inputPath)) return inputPath;
const lower = inputPath.toLowerCase();
if (lower.endsWith('.mp4') && !lower.endsWith('.mp4.avi')) return inputPath;
const outPath = inputPath.replace(/\.(mp4\.avi|avi)$/i, '.mp4');
if (outPath === inputPath) return inputPath;
try {
const { spawn } = require('child_process');
await new Promise((resolve, reject) => {
const proc = spawn('ffmpeg', ['-y', '-i', inputPath, '-c', 'copy', outPath], { windowsHide: true });
proc.on('error', reject);
proc.on('close', (code) => (code === 0 ? resolve() : reject(new Error(`ffmpeg exit ${code}`))));
});
if (fs.existsSync(outPath) && fs.statSync(outPath).size > 0) {
console.log(`[MediaEngine] remuxed video → ${outPath}`);
return outPath;
}
} catch (e) {
console.warn(`[MediaEngine] ffmpeg remux skipped: ${e.message}`);
}
return inputPath;
}
function _applyMemoryCliArgs(args, mem) {
if (!mem) return;
if (mem.diffusionFa) args.push('--diffusion-fa');
if (mem.offloadToCpu) args.push('--offload-to-cpu');
if (mem.vaeOnCpu) args.push('--vae-on-cpu');
if (mem.clipOnCpu) args.push('--clip-on-cpu');
if (mem.vaeConvDirect) args.push('--vae-conv-direct');
if (mem.vaeTiling) {
args.push('--vae-tiling');
args.push('--vae-tile-size', '64');
}
}
function formatSdExitError(code, stderr) {
const raw = (stderr || '').trim();
const lines = raw.split(/\r?\n/).filter((line) => {
const t = line.trim();
if (!t) return false;
if (/^ggml_vulkan:/i.test(t)) return false;
if (/^\[INFO\]/i.test(t)) return false;
return true;
});
const errorLines = lines
.filter((l) => /\[ERROR\]/i.test(l))
.map((l) => l.replace(/^\[ERROR\]\s*/i, '').trim());
if (is5dTensorStderr(stderr) || errorLines.some((l) => is5dTensorStderr(l))) {
return TENSOR_5D_MSG;
}
if (errorLines.some((l) => /not in model file/i.test(l))) {
return 'This model file is missing required diffusion components (VAE and/or text encoders). '
+ 'guIDE will auto-download companions for supported profiles on first generate. '
+ 'Otherwise place files beside your GGUF or set paths in Settings → Media.';
}
if (errorLines.some((l) => /get sd version from file failed/i.test(l))) {
return 'Could not load model file. Ensure you are using a stable-diffusion.cpp compatible image/video GGUF.';
}
const excerpt = (errorLines.length ? errorLines.slice(-5) : lines.slice(-6)).join('\n');
if (code === WIN_STACK_OVERRUN || code === -1073740791) {
return 'stable-diffusion.cpp crashed (stack buffer overrun). '
+ 'Try a smaller resolution, fewer steps, or Settings → Media → Max save VRAM policy.'
+ (excerpt ? `\n\n${excerpt}` : '');
}
if (code === WIN_DLL_NOT_FOUND || code === -1073741515) {
return 'stable-diffusion.cpp could not start (missing GPU runtime DLLs). '
+ 'Reinstall guIDE or set Settings → Media → sd.cpp path to a local sd.exe with its DLLs.'
+ (excerpt ? `\n\n${excerpt}` : '');
}
if (excerpt) return excerpt;
if (code != null) return `sd exited with code ${code}`;
return 'sd generation failed';
}
function _isLaunchFailure(code) {
if (code == null) return false;
if (code === WIN_STACK_OVERRUN || code === -1073740791) return false;
return code === WIN_DLL_NOT_FOUND || code === -1073741515;
}
function mimeForOutputPath(filePath, isVideo) {
if (!isVideo) return 'image/png';
const lower = String(filePath || '').toLowerCase();
if (lower.endsWith('.avi') || lower.endsWith('.mp4.avi')) return 'video/x-msvideo';
if (lower.endsWith('.webm')) return 'video/webm';
if (lower.endsWith('.mov')) return 'video/quicktime';
return 'video/mp4';
}
class MediaEngine {
constructor(options = {}) {
this.rootDir = options.rootDir || __dirname;
this.userDataPath = options.userDataPath || require('os').tmpdir();
this.getSettings = options.getSettings || (() => ({}));
this.isPackaged = options.isPackaged || false;
this.resourcesPath = options.resourcesPath || null;
this.installVariant = options.installVariant || 'cpu';
this.auxResolver = options.auxResolver || null;
this.onAuxProgress = options.onAuxProgress || null;
this.onGenProgress = options.onGenProgress || null;
this.modelPath = null;
this._mediaReadiness = null;
this.ggufArchitecture = null;
this.modelType = null;
this._profileId = null;
this._generating = false;
this._outputDir = path.join(this.userDataPath, 'guide-media');
this._sdBinaryPath = null;
this._resolvedAux = null;
}
getStatus() {
const primary = this._resolveSdBinaryCandidates()[0] || null;
return {
loaded: !!this.modelPath,
modelPath: this.modelPath,
ggufArchitecture: this.ggufArchitecture,
modelType: this.modelType,
profileId: this._profileId,
mediaReadiness: this._mediaReadiness,
sdBinary: primary,
sdBinaryFound: !!primary,
};
}
async load(modelPath) {
const meta = await readGgufMetadata(modelPath);
const type = meta ? detectModelTypeFromGguf(meta) : 'unknown';
if (type !== 'diffusion' && type !== 'video') {
throw new Error(
`Not a media GGUF (arch=${meta?.general?.architecture || 'unknown'}, type=${type}). `
+ 'Load a diffusion/video model with proper GGUF metadata.',
);
}
const arch = (meta?.general?.architecture || '').toLowerCase();
const profileId = archToMediaProfile(arch, type, modelPath);
if (!profileId) {
throw new Error(
`Unsupported media architecture "${arch || 'unknown'}". `
+ 'Use a stable-diffusion.cpp compatible image/video GGUF.',
);
}
this.modelPath = modelPath;
this.ggufArchitecture = arch;
this.modelType = type;
this._profileId = profileId;
this._resolvedAux = null;
if (this.auxResolver) {
const settings = this.getSettings();
const vramMB = queryGpuVramMB();
this._mediaReadiness = this.auxResolver.preflight({
arch, modelType: type, modelPath, settings, vramMB,
});
} else {
this._mediaReadiness = { profileId, ready: true, missing: [] };
}
return this.getStatus();
}
async unload() {
this.modelPath = null;
this.ggufArchitecture = null;
this.modelType = null;
this._profileId = null;
this._resolvedAux = null;
}
_collectSdBinaryPaths() {
const candidates = [];
const push = (p) => {
if (!p) return;
const abs = path.resolve(p);
if (fs.existsSync(abs) && !candidates.includes(abs)) candidates.push(abs);
};
const settings = this.getSettings();
if (settings.sdCppPath) push(settings.sdCppPath);
if (process.env.GUIDE_SD_CPP_PATH) push(process.env.GUIDE_SD_CPP_PATH);
const rootDir = path.resolve(this.rootDir);
const binDir = path.join(rootDir, 'bin');
for (const name of [process.platform === 'win32' ? 'sd.exe' : 'sd', 'sd-cli.exe']) {
push(path.join(binDir, name));
}
if (this.isPackaged && this.resourcesPath) {
const resRoot = path.resolve(this.resourcesPath);
for (const sub of ['sd-cpp', 'sd-cpp-cpu']) {
for (const name of ['sd.exe', 'sd-cli.exe', 'sd']) {
push(path.join(resRoot, sub, name));
}
}
}
for (const sub of ['win-x64-cuda', 'win-x64-cpu']) {
for (const name of ['sd.exe', 'sd-cli.exe', 'sd']) {
push(path.join(rootDir, 'resources', 'sd-cpp', sub, name));
}
}
return candidates;
}
_resolveSdBinaryCandidates() {
const all = this._collectSdBinaryPaths();
if (this._sdBinaryPath && fs.existsSync(this._sdBinaryPath)) {
const rest = all.filter((p) => p !== this._sdBinaryPath);
return [this._sdBinaryPath, ...rest];
}
const vulkan = [];
const cuda = [];
const other = [];
const seen = new Set();
for (const p of all) {
const key = path.resolve(p);
if (seen.has(key)) continue;
seen.add(key);
if (/sd-cpp-cpu|win-x64-cpu|vulkan/i.test(p)) vulkan.push(p);
else if (/sd-cpp|win-x64-cuda|cuda/i.test(p)) cuda.push(p);
else other.push(p);
}
if (this.installVariant === 'cuda') {
if (cuda.length && vulkan.length) return [cuda[0], vulkan[0], ...cuda.slice(1), ...vulkan.slice(1), ...other];
return [...cuda, ...vulkan, ...other];
}
if (vulkan.length) return [...vulkan, ...cuda, ...other];
return all;
}
_collectSdPathDirs() {
const dirs = new Set();
for (const bin of this._collectSdBinaryPaths()) {
dirs.add(path.dirname(bin));
}
if (this.isPackaged && this.resourcesPath) {
const resRoot = path.resolve(this.resourcesPath);
for (const sub of ['sd-cpp', 'sd-cpp-cpu']) {
const d = path.join(resRoot, sub);
if (fs.existsSync(d)) dirs.add(d);
}
}
return [...dirs];
}
_pushOptionalAux(args, aux, mem) {
if (aux.tae) {
args.push('--tae', aux.tae);
if (mem?.vaeConvDirect) args.push('--vae-conv-direct');
} else if (aux.vae) {
args.push('--vae', aux.vae);
}
if (aux.clip_l) args.push('--clip_l', aux.clip_l);
if (aux.clip_g) args.push('--clip_g', aux.clip_g);
if (aux.t5) args.push('--t5xxl', aux.t5);
if (aux.llm) args.push('--llm', aux.llm);
else if (aux.clip && !aux.clip_l) args.push('--llm', aux.clip);
}
_buildSdArgs(opts) {
const aux = opts.aux || {};
const profileId = opts.profileId || this._profileId;
const gen = getProfileGen(profileId);
const isVideo = gen.video === true || this.modelType === 'video';
const mem = opts.memoryFlags || {};
const args = [];
if (isVideo) args.push('-M', 'vid_gen');
args.push('--diffusion-model', opts.model);
this._pushOptionalAux(args, aux, mem);
args.push('-p', opts.prompt);
args.push('-o', opts.output);
args.push('-W', String(opts.width));
args.push('-H', String(opts.height));
args.push('--steps', String(opts.steps));
args.push('-s', String(opts.seed));
if (gen.cfgScale != null) args.push('--cfg-scale', String(gen.cfgScale));
if (gen.negativePrompt) args.push('-n', gen.negativePrompt);
if (isVideo) {
args.push('--video-frames', String(opts.videoFrames || 33));
if (gen.flowShift != null) args.push('--flow-shift', String(gen.flowShift));
}
_applyMemoryCliArgs(args, mem);
return { args, isVideo };
}
async generate(prompt, options = {}) {
if (!this.modelPath) {
return { success: false, error: 'No media model loaded. Load a diffusion/video GGUF first.' };
}
if (!prompt?.trim()) {
return { success: false, error: 'No prompt provided' };
}
if (this._generating) {
return { success: false, error: 'Generation already in progress — wait for the current job to finish.' };
}
const sdCandidates = this._resolveSdBinaryCandidates();
if (sdCandidates.length === 0) {
return {
success: false,
error: 'stable-diffusion.cpp binary not found. Run: node scripts/fetch-sd-cpp.js',
architecture: this.ggufArchitecture,
};
}
const settings = this.getSettings();
const vramMB = options.vramMB || 0;
const isVideo = this.modelType === 'video';
const defaults = getDefaultMediaDimensions(vramMB, isVideo, settings);
const memoryFlags = options.memoryFlags || resolveMediaMemoryFlags(settings, vramMB);
const width = options.width || defaults.width;
const height = options.height || defaults.height;
const steps = options.steps || (settings.mediaVideoSteps > 0 ? settings.mediaVideoSteps : 20);
const seed = options.seed != null ? options.seed : Math.floor(Math.random() * 2147483647);
const videoFrames = options.videoFrames || defaults.videoFrames;
const genStartedAt = Date.now();
const emitGenProgress = (fields) => {
if (this.onGenProgress) {
this.onGenProgress({
phase: 'generating',
elapsedMs: Date.now() - genStartedAt,
width,
height,
videoFrames: isVideo ? videoFrames : undefined,
estDurationSec: isVideo ? estimateVideoDurationSec(videoFrames) : undefined,
sdCpuFallback: !!this._lastSdCpuFallback,
...fields,
});
}
};
emitGenProgress({ label: isVideo ? `Generating video (${videoFrames} frames ≈ ${estimateVideoDurationSec(videoFrames)}s)` : 'Generating image…' });
let aux = this._resolvedAux || {};
if (this.auxResolver) {
try {
aux = await this.auxResolver.ensureForGenerate({
arch: this.ggufArchitecture,
modelType: this.modelType,
modelPath: this.modelPath,
settings,
vramMB,
onProgress: this.onAuxProgress,
});
this._resolvedAux = aux;
} catch (e) {
return { success: false, error: e.message || String(e), architecture: this.ggufArchitecture };
}
}
await fsp.mkdir(this._outputDir, { recursive: true });
const ext = isVideo ? 'mp4' : 'png';
const outFile = path.join(this._outputDir, `guide-${Date.now()}.${ext}`);
const buildRun = (modelPath) => this._buildSdArgs({
model: modelPath,
profileId: this._profileId,
prompt: prompt.trim().substring(0, 2000),
width,
height,
steps,
seed,
output: outFile,
videoFrames,
memoryFlags,
aux,
});
this._generating = true;
try {
let modelForRun = this.modelPath;
let builtForRun = buildRun(modelForRun);
console.log(`[MediaEngine] sd ${builtForRun.args.join(' ')}`);
let runResult = await this._runSdWithFallback(sdCandidates, builtForRun.args, emitGenProgress);
let outputPath = runResult.outputPath || outFile;
const sd5dFailure = !runResult.ok
&& (needs5dFix(runResult.stderr) || needs5dFix(runResult.error))
&& is5dCompatArch(this.ggufArchitecture);
if (sd5dFailure) {
try {
const cacheDir = path.join(this.userDataPath, 'media-cache');
const fixed = await apply5dFix({
srcGguf: this.modelPath,
arch: this.ggufArchitecture,
modelPath: this.modelPath,
cacheDir,
onProgress: (msg) => {
console.log(`[MediaEngine] 5D compat: ${msg}`);
streamTrace.trace('stream', 'media-progress', { phase: '5d-fix', message: msg });
if (this.onAuxProgress) this.onAuxProgress({ phase: '5d-fix', message: msg });
},
});
modelForRun = fixed;
builtForRun = buildRun(modelForRun);
console.log(`[MediaEngine] retrying with 5D-patched GGUF: ${path.basename(fixed)}`);
runResult = await this._runSdWithFallback(sdCandidates, builtForRun.args, emitGenProgress);
if (runResult.outputPath) outputPath = runResult.outputPath;
} catch (fixErr) {
console.error(`[MediaEngine] 5D auto-patch failed: ${fixErr.message}`);
return {
success: false,
error: `${TENSOR_5D_MSG}\n\n${fixErr.message}`,
architecture: this.ggufArchitecture,
};
}
}
if (!runResult.ok) {
return { success: false, error: runResult.error, architecture: this.ggufArchitecture };
}
if (builtForRun.isVideo) {
outputPath = await tryRemuxVideoToMp4(outputPath);
}
const buf = await fsp.readFile(outputPath);
const mimeType = mimeForOutputPath(outputPath, builtForRun.isVideo);
console.log(`[MediaEngine] generate OK ${builtForRun.isVideo ? 'video' : 'image'} path=${outputPath} mime=${mimeType} bytes=${buf.length}`);
const b64 = buf.toString('base64');
return {
success: true,
imageBase64: b64,
videoBase64: builtForRun.isVideo ? b64 : undefined,
mimeType,
path: outputPath,
provider: 'stable-diffusion.cpp',
model: path.basename(modelForRun),
width,
height,
seed,
videoFrames: builtForRun.isVideo ? videoFrames : undefined,
sdCpuFallback: !!this._lastSdCpuFallback,
mediaType: builtForRun.isVideo ? 'video' : 'image',
};
} catch (e) {
return { success: false, error: e.message || String(e) };
} finally {
this._generating = false;
}
}
async _runSdWithFallback(sdCandidates, args, onProgress) {
let lastError = 'sd generation failed';
let lastStderr = '';
let lastCode = null;
this._lastSdCpuFallback = false;
for (let i = 0; i < sdCandidates.length; i++) {
const sdBin = sdCandidates[i];
const result = await this._runSdOnce(sdBin, args, onProgress);
if (result.ok) {
this._sdBinaryPath = sdBin;
this._lastSdCpuFallback = /sd-cpp-cpu/i.test(sdBin);
return result;
}
lastError = result.error;
lastStderr = result.stderr || lastStderr;
lastCode = result.code;
const canRetry = i < sdCandidates.length - 1 && _isLaunchFailure(result.code);
if (canRetry) {
console.warn(`[MediaEngine] sd launch failed (${result.code}) — trying fallback binary: ${sdCandidates[i + 1]}`);
this._lastSdCpuFallback = true;
if (onProgress) onProgress({ label: 'CUDA unavailable — generating on CPU (slower, lower quality)' });
continue;
}
break;
}
return { ok: false, error: lastError, stderr: lastStderr, code: lastCode, outputPath: null };
}
_resolveSdOutputPath(requestedOut) {
if (!requestedOut) return null;
if (fs.existsSync(requestedOut)) return requestedOut;
const candidates = [
`${requestedOut}.avi`,
requestedOut.replace(/\.mp4$/i, '.mp4.avi'),
requestedOut.replace(/\.(mp4|png)$/i, '.avi'),
];
for (const p of candidates) {
if (p && fs.existsSync(p)) return p;
}
return null;
}
_runSdOnce(sdBin, args, onProgress, timeoutMs = 0) {
return new Promise((resolve) => {
const binDir = path.dirname(sdBin);
const env = { ...process.env };
const pathDirs = this._collectSdPathDirs();
if (!pathDirs.includes(binDir)) pathDirs.unshift(binDir);
env.PATH = [...pathDirs, env.PATH || ''].filter(Boolean).join(path.delimiter);
const proc = spawn(sdBin, args, {
stdio: ['ignore', 'pipe', 'pipe'],
cwd: binDir,
env,
windowsHide: true,
});
let stderr = '';
let timer = null;
const startedAt = Date.now();
let lastProgressAt = 0;
if (timeoutMs > 0) {
timer = setTimeout(() => {
try { proc.kill(); } catch { /* ignore */ }
}, timeoutMs);
}
proc.stderr.on('data', (d) => {
stderr += d.toString();
if (!onProgress) return;
const now = Date.now();
if (now - lastProgressAt < 2000) return;
lastProgressAt = now;
const stepMatch = stderr.match(/step\s+(\d+)\s*\/\s*(\d+)/i);
onProgress({
elapsedMs: now - startedAt,
step: stepMatch ? parseInt(stepMatch[1], 10) : undefined,
totalSteps: stepMatch ? parseInt(stepMatch[2], 10) : undefined,
});
});
proc.on('error', (err) => {
if (timer) clearTimeout(timer);
resolve({ ok: false, error: err.message, code: null, stderr });
});
proc.on('close', (code) => {
if (timer) clearTimeout(timer);
const outIdx = args.indexOf('-o');
const requestedOut = outIdx >= 0 ? args[outIdx + 1] : null;
const actualOut = this._resolveSdOutputPath(requestedOut);
if (actualOut) {
try {
const stat = fs.statSync(actualOut);
if (stat.size > 0) {
if (code !== 0 && code != null) {
console.warn(`[MediaEngine] sd exited ${code} but output exists (${stat.size} bytes): ${actualOut}`);
}
resolve({ ok: true, outputPath: actualOut, code });
return;
}
} catch (_) { /* fall through */ }
}
if (stderr.trim()) console.error(`[MediaEngine] sd stderr: ${stderr.trim().slice(-2000)}`);
resolve({
ok: false,
code,
stderr,
error: formatSdExitError(code, stderr),
});
});
});
}
}
module.exports = {
MediaEngine,
queryGpuVramMB,
resolveMediaMemoryFlags,
getDefaultMediaDimensions,
estimateVideoDurationSec,
formatSdExitError,
WAN_VIDEO_FPS,
WIN_DLL_NOT_FOUND,
WIN_STACK_OVERRUN,
VRAM_LOW_MB,
VRAM_TIGHT_MB,
};