dsh-folia 4.26.0-pre.19 → 4.26.0-pre.20
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/lib/voice/record.js +217 -200
- package/package.json +1 -1
package/lib/voice/record.js
CHANGED
|
@@ -1,200 +1,217 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* voice/record.js — 宿主侧录音(v0.1 语音输入)。
|
|
3
|
-
*
|
|
4
|
-
* 为什么不在前端录:DSH 的 Electron 只放行 clipboard/notifications 权限,
|
|
5
|
-
* `getUserMedia` 一律被拒且没有弹窗 ⇒ 前端录音必然失败(工单「追加决定 3」)。
|
|
6
|
-
* 所以改成宿主用 ffmpeg 的 dshow 抓麦克风,前端只负责"点一下、等结果"。
|
|
7
|
-
*
|
|
8
|
-
* 依赖注入(判据用假 runFfmpeg,不需要真麦克风):
|
|
9
|
-
* resolveFfmpeg(job) → Promise<string> 来自 lib/media/transcode.js
|
|
10
|
-
* runFfmpeg(ff, args, timeoutMs) → Promise<{ code, stderr }>
|
|
11
|
-
*/
|
|
12
|
-
import { spawn } from 'node:child_process';
|
|
13
|
-
import { mkdtempSync, rmSync, statSync } from 'node:fs';
|
|
14
|
-
import { tmpdir } from 'node:os';
|
|
15
|
-
import { join } from 'node:path';
|
|
16
|
-
|
|
17
|
-
/** 默认参数(工单给的数)。 */
|
|
18
|
-
export const RECORD_DEFAULTS = Object.freeze({
|
|
19
|
-
maxMs: 15000,
|
|
20
|
-
silenceDb: -35,
|
|
21
|
-
silenceMs: 1200,
|
|
22
|
-
sampleRate: 16000,
|
|
23
|
-
});
|
|
24
|
-
|
|
25
|
-
/**
|
|
26
|
-
* 从 ffmpeg `-list_devices` 的 stderr 里解析音频设备名。
|
|
27
|
-
*
|
|
28
|
-
* 两种输出格式都要认(实测两代 ffmpeg 不一样):
|
|
29
|
-
* ① 老式分段:`DirectShow audio devices` … `"麦克风"` … `DirectShow video devices`
|
|
30
|
-
* ② 新式带类型:`[dshow @ …] "麦克风 (C-Media(R) Audio)" (audio)`
|
|
31
|
-
* —— 本仓自带的 ffmpeg 就是这种,**没有** "DirectShow audio devices" 字样,
|
|
32
|
-
* 只按 ① 解析会得到空列表 ⇒ /record 恒回 no-device(真机踩过)。
|
|
33
|
-
* `Alternative name` 行同样是引号包裹,但它不是设备名,必须排除。
|
|
34
|
-
*/
|
|
35
|
-
export function parseDevices(stderr) {
|
|
36
|
-
const text = String(stderr || '');
|
|
37
|
-
const devices = [];
|
|
38
|
-
const seen = new Set();
|
|
39
|
-
const push = (name) => {
|
|
40
|
-
const clean = String(name || '').trim();
|
|
41
|
-
if (!clean || seen.has(clean)) return;
|
|
42
|
-
seen.add(clean);
|
|
43
|
-
devices.push(clean);
|
|
44
|
-
};
|
|
45
|
-
|
|
46
|
-
// ② 新式:`[dshow @ …] "设备名" (audio|video)` —— 只收 audio
|
|
47
|
-
const typed = /\[dshow[^\]]*\]\s+"([^"]+)"\s+\((audio|video)\)/gi;
|
|
48
|
-
let m;
|
|
49
|
-
while ((m = typed.exec(text)) !== null) {
|
|
50
|
-
if (m[2].toLowerCase() === 'audio') push(m[1]);
|
|
51
|
-
}
|
|
52
|
-
if (devices.length) return devices;
|
|
53
|
-
|
|
54
|
-
// ① 老式:先截出 audio 段,再取引号内容(排除 Alternative name)
|
|
55
|
-
const audioStart = text.search(/DirectShow audio devices/i);
|
|
56
|
-
if (audioStart < 0) return [];
|
|
57
|
-
const section = text.slice(audioStart);
|
|
58
|
-
const end = section.search(/DirectShow video devices/i);
|
|
59
|
-
const body = end >= 0 ? section.slice(0, end) : section;
|
|
60
|
-
const pattern = /^\s*(?:\[[^\]]*\]\s*)?"([^"]+)"/gm;
|
|
61
|
-
while ((m = pattern.exec(body)) !== null) push(m[1]);
|
|
62
|
-
return devices;
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
/** 优先含 Microphone/麦克风/USB 的设备,否则第一个。 */
|
|
66
|
-
export function pickDevice(devices) {
|
|
67
|
-
const list = Array.isArray(devices) ? devices.filter(Boolean) : [];
|
|
68
|
-
if (!list.length) return null;
|
|
69
|
-
const preferred = list.find((name) => /microphone|麦克风|usb/i.test(name));
|
|
70
|
-
return preferred || list[0];
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
/**
|
|
74
|
-
* 从 silencedetect 的 stderr 判断"说完了":
|
|
75
|
-
* 先出现 silence_end(用户开口),之后又出现 silence_start(停下来了)⇒ 结束。
|
|
76
|
-
*/
|
|
77
|
-
export function detectSpeechEnd(stderr) {
|
|
78
|
-
const text = String(stderr || '');
|
|
79
|
-
const ends = [...text.matchAll(/silence_end:\s*([0-9.]+)/g)];
|
|
80
|
-
const starts = [...text.matchAll(/silence_start:\s*([0-9.]+)/g)];
|
|
81
|
-
if (!ends.length || !starts.length) return null;
|
|
82
|
-
const lastEnd = Number(ends[ends.length - 1][1]);
|
|
83
|
-
const startAfter = starts.map((m) => Number(m[1])).find((value) => value >= lastEnd);
|
|
84
|
-
return startAfter == null ? null : { speechEnd: lastEnd, silenceStart: startAfter };
|
|
85
|
-
}
|
|
86
|
-
|
|
87
|
-
/** 生成 dshow 输入参数(Windows);其它平台没有 dshow,直接报 no-device。 */
|
|
88
|
-
export function dshowArgs(device, options = {}) {
|
|
89
|
-
const opts = { ...RECORD_DEFAULTS, ...options };
|
|
90
|
-
return [
|
|
91
|
-
'-hide_banner', '-y',
|
|
92
|
-
'-f', 'dshow', '-i', `audio=${device}`,
|
|
93
|
-
'-ar', String(opts.sampleRate), '-ac', '1',
|
|
94
|
-
'-t', String(Math.ceil(opts.maxMs / 1000) + 5),
|
|
95
|
-
'-af', `silencedetect=noise=${opts.silenceDb}dB:d=${opts.silenceMs / 1000}`,
|
|
96
|
-
];
|
|
97
|
-
}
|
|
98
|
-
|
|
99
|
-
export function createVoiceRecorder(deps) {
|
|
100
|
-
const { resolveFfmpeg, runFfmpeg } = deps || {};
|
|
101
|
-
const probeSize = typeof deps.probeSize === 'function' ? deps.probeSize : (path) => { try { return statSync(path).size; } catch { return 0; } };
|
|
102
|
-
|
|
103
|
-
async function listDevices() {
|
|
104
|
-
const ff = await resolveFfmpeg(null);
|
|
105
|
-
const result = await runFfmpeg(ff, ['-hide_banner', '-list_devices', 'true', '-f', 'dshow', '-i', 'dummy'], 8000);
|
|
106
|
-
return parseDevices(result && result.stderr);
|
|
107
|
-
}
|
|
108
|
-
|
|
109
|
-
/**
|
|
110
|
-
* 录一段:返回 { audioPath, ms, speech }。
|
|
111
|
-
* 失败码:no-device / ffmpeg-missing / timeout / no-speech
|
|
112
|
-
*/
|
|
113
|
-
async function record(options = {}) {
|
|
114
|
-
const opts = { ...RECORD_DEFAULTS, ...options };
|
|
115
|
-
// 非 Windows:没有 dshow,宿主录音不可用(浏览器模式请用前端 getUserMedia 通道)
|
|
116
|
-
if (process.platform !== 'win32') return { ok: false, code: 'unsupported-platform' };
|
|
117
|
-
let devices;
|
|
118
|
-
try {
|
|
119
|
-
devices = await listDevices();
|
|
120
|
-
} catch {
|
|
121
|
-
return { ok: false, code: 'ffmpeg-missing' };
|
|
122
|
-
}
|
|
123
|
-
const preferred = pickDevice(devices);
|
|
124
|
-
if (!preferred) return { ok: false, code: 'no-device' };
|
|
125
|
-
// 跨机器:不同机器的第一个"像麦克风"的设备不一定真能开(被占用/驱动不同)
|
|
126
|
-
// ⇒ 依次尝试最多 3 个音频设备,任一成功即返回。
|
|
127
|
-
const candidates = [preferred, ...devices.filter((d) => d !== preferred)].slice(0, 3);
|
|
128
|
-
let lastCode = 'no-speech';
|
|
129
|
-
for (const device of candidates) {
|
|
130
|
-
const dir = mkdtempSync(join(tmpdir(), 'folia-voice-'));
|
|
131
|
-
const audioPath = join(dir, 'input.wav');
|
|
132
|
-
const startedAt = Date.now();
|
|
133
|
-
let result = null;
|
|
134
|
-
try {
|
|
135
|
-
result = await runFfmpeg(await resolveFfmpeg(null), [...dshowArgs(device, opts), audioPath], opts.maxMs + 8000);
|
|
136
|
-
} catch {
|
|
137
|
-
rmSync(dir, { recursive: true, force: true });
|
|
138
|
-
lastCode = 'ffmpeg-missing';
|
|
139
|
-
continue;
|
|
140
|
-
}
|
|
141
|
-
const ms = Date.now() - startedAt;
|
|
142
|
-
const stderr = String((result && result.stderr) || '');
|
|
143
|
-
if (result && result.code !== 0) {
|
|
144
|
-
// 这个设备打不开(占用/驱动)⇒ 换下一个
|
|
145
|
-
rmSync(dir, { recursive: true, force: true });
|
|
146
|
-
lastCode = 'no-device';
|
|
147
|
-
continue;
|
|
148
|
-
}
|
|
149
|
-
const ended = detectSpeechEnd(stderr);
|
|
150
|
-
if (!ended) {
|
|
151
|
-
rmSync(dir, { recursive: true, force: true });
|
|
152
|
-
lastCode = 'no-speech';
|
|
153
|
-
continue;
|
|
154
|
-
}
|
|
155
|
-
const size = probeSize(audioPath);
|
|
156
|
-
if (!size) { rmSync(dir, { recursive: true, force: true }); lastCode = 'no-speech'; continue; }
|
|
157
|
-
return { ok: true, audioPath, dir, ms, device, speech: ended, audioSeconds: Number((ended.speechEnd || 0).toFixed(2)) };
|
|
158
|
-
}
|
|
159
|
-
return { ok: false, code: lastCode }; }
|
|
160
|
-
|
|
161
|
-
return { listDevices, record };
|
|
162
|
-
}
|
|
163
|
-
|
|
164
|
-
/**
|
|
165
|
-
* 默认 ffmpeg 解析:环境变量 → 本地缓存(~/.dsh-folia/ffmpeg/ffmpeg.exe,用户已有 77MB 那个)
|
|
166
|
-
* → 系统 PATH。**不触发下载**(工单要求复用现有资产)。
|
|
167
|
-
*/
|
|
168
|
-
export async function defaultResolveFfmpeg() {
|
|
169
|
-
const env = process.env.DSH_FOLIA_FFMPEG;
|
|
170
|
-
if (env && env.trim()) return env.trim();
|
|
171
|
-
const exe = process.platform === 'win32' ? 'ffmpeg.exe' : 'ffmpeg';
|
|
172
|
-
const { existsSync } = await import('node:fs');
|
|
173
|
-
const candidates = [
|
|
174
|
-
join(process.env.USERPROFILE || process.env.HOME || '', '.dsh-folia', 'ffmpeg', exe),
|
|
175
|
-
join(process.env.APPDATA || '', 'dsh-desktop', 'ffmpeg', exe),
|
|
176
|
-
];
|
|
177
|
-
for (const p of candidates) { if (p && existsSync(p)) return p; }
|
|
178
|
-
// 交给 PATH:调用方会用 spawn 试一次,ENOENT 时会明确回 ffmpeg-missing
|
|
179
|
-
return exe;
|
|
180
|
-
}
|
|
181
|
-
|
|
182
|
-
/** 默认执行器:spawn + 收集 stderr(silencedetect 的输出在这里),超时强杀。 */
|
|
183
|
-
export function defaultRunFfmpeg(ff, args, timeoutMs = 30000) {
|
|
184
|
-
/**
|
|
185
|
-
*
|
|
186
|
-
*
|
|
187
|
-
*
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
const
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
1
|
+
/**
|
|
2
|
+
* voice/record.js — 宿主侧录音(v0.1 语音输入)。
|
|
3
|
+
*
|
|
4
|
+
* 为什么不在前端录:DSH 的 Electron 只放行 clipboard/notifications 权限,
|
|
5
|
+
* `getUserMedia` 一律被拒且没有弹窗 ⇒ 前端录音必然失败(工单「追加决定 3」)。
|
|
6
|
+
* 所以改成宿主用 ffmpeg 的 dshow 抓麦克风,前端只负责"点一下、等结果"。
|
|
7
|
+
*
|
|
8
|
+
* 依赖注入(判据用假 runFfmpeg,不需要真麦克风):
|
|
9
|
+
* resolveFfmpeg(job) → Promise<string> 来自 lib/media/transcode.js
|
|
10
|
+
* runFfmpeg(ff, args, timeoutMs) → Promise<{ code, stderr }>
|
|
11
|
+
*/
|
|
12
|
+
import { spawn } from 'node:child_process';
|
|
13
|
+
import { closeSync, mkdtempSync, openSync, readFileSync, rmSync, statSync } from 'node:fs';
|
|
14
|
+
import { tmpdir } from 'node:os';
|
|
15
|
+
import { join } from 'node:path';
|
|
16
|
+
|
|
17
|
+
/** 默认参数(工单给的数)。 */
|
|
18
|
+
export const RECORD_DEFAULTS = Object.freeze({
|
|
19
|
+
maxMs: 15000,
|
|
20
|
+
silenceDb: -35,
|
|
21
|
+
silenceMs: 1200,
|
|
22
|
+
sampleRate: 16000,
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* 从 ffmpeg `-list_devices` 的 stderr 里解析音频设备名。
|
|
27
|
+
*
|
|
28
|
+
* 两种输出格式都要认(实测两代 ffmpeg 不一样):
|
|
29
|
+
* ① 老式分段:`DirectShow audio devices` … `"麦克风"` … `DirectShow video devices`
|
|
30
|
+
* ② 新式带类型:`[dshow @ …] "麦克风 (C-Media(R) Audio)" (audio)`
|
|
31
|
+
* —— 本仓自带的 ffmpeg 就是这种,**没有** "DirectShow audio devices" 字样,
|
|
32
|
+
* 只按 ① 解析会得到空列表 ⇒ /record 恒回 no-device(真机踩过)。
|
|
33
|
+
* `Alternative name` 行同样是引号包裹,但它不是设备名,必须排除。
|
|
34
|
+
*/
|
|
35
|
+
export function parseDevices(stderr) {
|
|
36
|
+
const text = String(stderr || '');
|
|
37
|
+
const devices = [];
|
|
38
|
+
const seen = new Set();
|
|
39
|
+
const push = (name) => {
|
|
40
|
+
const clean = String(name || '').trim();
|
|
41
|
+
if (!clean || seen.has(clean)) return;
|
|
42
|
+
seen.add(clean);
|
|
43
|
+
devices.push(clean);
|
|
44
|
+
};
|
|
45
|
+
|
|
46
|
+
// ② 新式:`[dshow @ …] "设备名" (audio|video)` —— 只收 audio
|
|
47
|
+
const typed = /\[dshow[^\]]*\]\s+"([^"]+)"\s+\((audio|video)\)/gi;
|
|
48
|
+
let m;
|
|
49
|
+
while ((m = typed.exec(text)) !== null) {
|
|
50
|
+
if (m[2].toLowerCase() === 'audio') push(m[1]);
|
|
51
|
+
}
|
|
52
|
+
if (devices.length) return devices;
|
|
53
|
+
|
|
54
|
+
// ① 老式:先截出 audio 段,再取引号内容(排除 Alternative name)
|
|
55
|
+
const audioStart = text.search(/DirectShow audio devices/i);
|
|
56
|
+
if (audioStart < 0) return [];
|
|
57
|
+
const section = text.slice(audioStart);
|
|
58
|
+
const end = section.search(/DirectShow video devices/i);
|
|
59
|
+
const body = end >= 0 ? section.slice(0, end) : section;
|
|
60
|
+
const pattern = /^\s*(?:\[[^\]]*\]\s*)?"([^"]+)"/gm;
|
|
61
|
+
while ((m = pattern.exec(body)) !== null) push(m[1]);
|
|
62
|
+
return devices;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/** 优先含 Microphone/麦克风/USB 的设备,否则第一个。 */
|
|
66
|
+
export function pickDevice(devices) {
|
|
67
|
+
const list = Array.isArray(devices) ? devices.filter(Boolean) : [];
|
|
68
|
+
if (!list.length) return null;
|
|
69
|
+
const preferred = list.find((name) => /microphone|麦克风|usb/i.test(name));
|
|
70
|
+
return preferred || list[0];
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/**
|
|
74
|
+
* 从 silencedetect 的 stderr 判断"说完了":
|
|
75
|
+
* 先出现 silence_end(用户开口),之后又出现 silence_start(停下来了)⇒ 结束。
|
|
76
|
+
*/
|
|
77
|
+
export function detectSpeechEnd(stderr) {
|
|
78
|
+
const text = String(stderr || '');
|
|
79
|
+
const ends = [...text.matchAll(/silence_end:\s*([0-9.]+)/g)];
|
|
80
|
+
const starts = [...text.matchAll(/silence_start:\s*([0-9.]+)/g)];
|
|
81
|
+
if (!ends.length || !starts.length) return null;
|
|
82
|
+
const lastEnd = Number(ends[ends.length - 1][1]);
|
|
83
|
+
const startAfter = starts.map((m) => Number(m[1])).find((value) => value >= lastEnd);
|
|
84
|
+
return startAfter == null ? null : { speechEnd: lastEnd, silenceStart: startAfter };
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
/** 生成 dshow 输入参数(Windows);其它平台没有 dshow,直接报 no-device。 */
|
|
88
|
+
export function dshowArgs(device, options = {}) {
|
|
89
|
+
const opts = { ...RECORD_DEFAULTS, ...options };
|
|
90
|
+
return [
|
|
91
|
+
'-hide_banner', '-y',
|
|
92
|
+
'-f', 'dshow', '-i', `audio=${device}`,
|
|
93
|
+
'-ar', String(opts.sampleRate), '-ac', '1',
|
|
94
|
+
'-t', String(Math.ceil(opts.maxMs / 1000) + 5),
|
|
95
|
+
'-af', `silencedetect=noise=${opts.silenceDb}dB:d=${opts.silenceMs / 1000}`,
|
|
96
|
+
];
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
export function createVoiceRecorder(deps) {
|
|
100
|
+
const { resolveFfmpeg, runFfmpeg } = deps || {};
|
|
101
|
+
const probeSize = typeof deps.probeSize === 'function' ? deps.probeSize : (path) => { try { return statSync(path).size; } catch { return 0; } };
|
|
102
|
+
|
|
103
|
+
async function listDevices() {
|
|
104
|
+
const ff = await resolveFfmpeg(null);
|
|
105
|
+
const result = await runFfmpeg(ff, ['-hide_banner', '-list_devices', 'true', '-f', 'dshow', '-i', 'dummy'], 8000);
|
|
106
|
+
return parseDevices(result && result.stderr);
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
/**
|
|
110
|
+
* 录一段:返回 { audioPath, ms, speech }。
|
|
111
|
+
* 失败码:no-device / ffmpeg-missing / timeout / no-speech
|
|
112
|
+
*/
|
|
113
|
+
async function record(options = {}) {
|
|
114
|
+
const opts = { ...RECORD_DEFAULTS, ...options };
|
|
115
|
+
// 非 Windows:没有 dshow,宿主录音不可用(浏览器模式请用前端 getUserMedia 通道)
|
|
116
|
+
if (process.platform !== 'win32') return { ok: false, code: 'unsupported-platform' };
|
|
117
|
+
let devices;
|
|
118
|
+
try {
|
|
119
|
+
devices = await listDevices();
|
|
120
|
+
} catch {
|
|
121
|
+
return { ok: false, code: 'ffmpeg-missing' };
|
|
122
|
+
}
|
|
123
|
+
const preferred = pickDevice(devices);
|
|
124
|
+
if (!preferred) return { ok: false, code: 'no-device' };
|
|
125
|
+
// 跨机器:不同机器的第一个"像麦克风"的设备不一定真能开(被占用/驱动不同)
|
|
126
|
+
// ⇒ 依次尝试最多 3 个音频设备,任一成功即返回。
|
|
127
|
+
const candidates = [preferred, ...devices.filter((d) => d !== preferred)].slice(0, 3);
|
|
128
|
+
let lastCode = 'no-speech';
|
|
129
|
+
for (const device of candidates) {
|
|
130
|
+
const dir = mkdtempSync(join(tmpdir(), 'folia-voice-'));
|
|
131
|
+
const audioPath = join(dir, 'input.wav');
|
|
132
|
+
const startedAt = Date.now();
|
|
133
|
+
let result = null;
|
|
134
|
+
try {
|
|
135
|
+
result = await runFfmpeg(await resolveFfmpeg(null), [...dshowArgs(device, opts), audioPath], opts.maxMs + 8000);
|
|
136
|
+
} catch {
|
|
137
|
+
rmSync(dir, { recursive: true, force: true });
|
|
138
|
+
lastCode = 'ffmpeg-missing';
|
|
139
|
+
continue;
|
|
140
|
+
}
|
|
141
|
+
const ms = Date.now() - startedAt;
|
|
142
|
+
const stderr = String((result && result.stderr) || '');
|
|
143
|
+
if (result && result.code !== 0) {
|
|
144
|
+
// 这个设备打不开(占用/驱动)⇒ 换下一个
|
|
145
|
+
rmSync(dir, { recursive: true, force: true });
|
|
146
|
+
lastCode = 'no-device';
|
|
147
|
+
continue;
|
|
148
|
+
}
|
|
149
|
+
const ended = detectSpeechEnd(stderr);
|
|
150
|
+
if (!ended) {
|
|
151
|
+
rmSync(dir, { recursive: true, force: true });
|
|
152
|
+
lastCode = 'no-speech';
|
|
153
|
+
continue;
|
|
154
|
+
}
|
|
155
|
+
const size = probeSize(audioPath);
|
|
156
|
+
if (!size) { rmSync(dir, { recursive: true, force: true }); lastCode = 'no-speech'; continue; }
|
|
157
|
+
return { ok: true, audioPath, dir, ms, device, speech: ended, audioSeconds: Number((ended.speechEnd || 0).toFixed(2)) };
|
|
158
|
+
}
|
|
159
|
+
return { ok: false, code: lastCode }; }
|
|
160
|
+
|
|
161
|
+
return { listDevices, record };
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
/**
|
|
165
|
+
* 默认 ffmpeg 解析:环境变量 → 本地缓存(~/.dsh-folia/ffmpeg/ffmpeg.exe,用户已有 77MB 那个)
|
|
166
|
+
* → 系统 PATH。**不触发下载**(工单要求复用现有资产)。
|
|
167
|
+
*/
|
|
168
|
+
export async function defaultResolveFfmpeg() {
|
|
169
|
+
const env = process.env.DSH_FOLIA_FFMPEG;
|
|
170
|
+
if (env && env.trim()) return env.trim();
|
|
171
|
+
const exe = process.platform === 'win32' ? 'ffmpeg.exe' : 'ffmpeg';
|
|
172
|
+
const { existsSync } = await import('node:fs');
|
|
173
|
+
const candidates = [
|
|
174
|
+
join(process.env.USERPROFILE || process.env.HOME || '', '.dsh-folia', 'ffmpeg', exe),
|
|
175
|
+
join(process.env.APPDATA || '', 'dsh-desktop', 'ffmpeg', exe),
|
|
176
|
+
];
|
|
177
|
+
for (const p of candidates) { if (p && existsSync(p)) return p; }
|
|
178
|
+
// 交给 PATH:调用方会用 spawn 试一次,ENOENT 时会明确回 ffmpeg-missing
|
|
179
|
+
return exe;
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
/** 默认执行器:spawn + 收集 stderr(silencedetect 的输出在这里),超时强杀。 */
|
|
183
|
+
export function defaultRunFfmpeg(ff, args, timeoutMs = 30000) {
|
|
184
|
+
/**
|
|
185
|
+
* 关键:**stderr 不能走 pipe**。
|
|
186
|
+
* dsh web 跑在 Electron 主进程里,这个受约束的 spawn 上下文里 piped stdio 会失败
|
|
187
|
+
* (本仓 transcode.js 记录过同一现象:piped stdio EPERM / 子进程启动即死),
|
|
188
|
+
* 表现为 ffmpeg 立刻退出 ⇒ record() 误判成 no-device / timeout。
|
|
189
|
+
* 因此 stderr 重定向到临时文件,等进程结束后再读回来解析 silencedetect。
|
|
190
|
+
*/
|
|
191
|
+
return new Promise((resolve) => {
|
|
192
|
+
const errFile = join(tmpdir(), 'folia-ffmpeg-' + process.pid + '-' + Date.now() + '.log');
|
|
193
|
+
let fd = null;
|
|
194
|
+
try { fd = openSync(errFile, 'w'); } catch { fd = null; }
|
|
195
|
+
const child = spawn(ff, args, {
|
|
196
|
+
windowsHide: true,
|
|
197
|
+
detached: true,
|
|
198
|
+
stdio: ['ignore', 'ignore', fd === null ? 'ignore' : fd],
|
|
199
|
+
});
|
|
200
|
+
let settled = false;
|
|
201
|
+
const finish = (code) => {
|
|
202
|
+
if (settled) return;
|
|
203
|
+
settled = true;
|
|
204
|
+
clearTimeout(timer);
|
|
205
|
+
let stderr = '';
|
|
206
|
+
try { stderr = readFileSync(errFile, 'utf8'); } catch { /* ignore */ }
|
|
207
|
+
try { if (fd !== null) closeSync(fd); } catch { /* ignore */ }
|
|
208
|
+
try { rmSync(errFile, { force: true }); } catch { /* ignore */ }
|
|
209
|
+
resolve({ code, stderr });
|
|
210
|
+
};
|
|
211
|
+
const timer = setTimeout(() => {
|
|
212
|
+
if (child.pid !== undefined) { try { child.kill(); } catch { /* ignore */ } }
|
|
213
|
+
}, timeoutMs);
|
|
214
|
+
child.on('error', () => finish(-1));
|
|
215
|
+
child.on('close', (code) => finish(code));
|
|
216
|
+
});
|
|
217
|
+
}
|