dsh-voice 0.9.1 → 0.10.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/CHANGELOG.md +13 -0
- package/README.en.md +23 -21
- package/README.md +23 -21
- package/docs/USAGE.en.md +64 -63
- package/docs/USAGE.md +64 -63
- package/lib/client.js +720 -146
- package/lib/edge-tts.d.ts +11 -2
- package/lib/edge-tts.js +384 -124
- package/lib/stt.d.ts +4 -0
- package/lib/stt.js +19 -0
- package/lib/tools.d.ts +2 -1
- package/lib/tools.js +329 -48
- package/lib/tts-text.d.ts +77 -4
- package/lib/tts-text.js +497 -8
- package/lib/web.d.ts +5 -2
- package/lib/web.js +608 -108
- package/package.json +1 -1
- package/skills/voice-workflow/SKILL.md +81 -55
package/lib/tts-text.d.ts
CHANGED
|
@@ -32,7 +32,12 @@ export declare function inspectMp3Buffer(mp3: Buffer): Mp3AudioInfo;
|
|
|
32
32
|
*/
|
|
33
33
|
export declare function createSilenceMp3(durationMs: number, sampleRate?: number): Buffer;
|
|
34
34
|
/**
|
|
35
|
-
*
|
|
35
|
+
* 剥离单段 MP3 的首尾 ID3 标签以及首帧 Xing / Info / VBRI 元数据空帧,
|
|
36
|
+
* 避免多段 VBR MP3 拼接后播放器或时长解析器被首段的帧数头锁死。
|
|
37
|
+
*/
|
|
38
|
+
export declare function stripMp3MetadataFrame(mp3: Buffer): Buffer;
|
|
39
|
+
/**
|
|
40
|
+
* 拼接多段 MP3:单段原样返回;多段拼接时剥离各段的 Xing/Info/VBRI 元数据帧及非首段 ID3 标签。
|
|
36
41
|
*/
|
|
37
42
|
export declare function concatMp3Buffers(buffers: Buffer[]): Buffer;
|
|
38
43
|
/**
|
|
@@ -43,10 +48,24 @@ export declare function estimateMp3DurationMs(mp3: Buffer): number;
|
|
|
43
48
|
* 平移字幕时间轴并从 startIndex 起重新编号。
|
|
44
49
|
*/
|
|
45
50
|
export declare function offsetCues(cues: SubtitleCue[], offsetMs: number, startIndex?: number): SubtitleCue[];
|
|
51
|
+
/** 字幕切分粒度:整句、短视频紧凑短句或逐词。 */
|
|
52
|
+
export type SubtitleGranularity = 'sentence' | 'clause' | 'word';
|
|
53
|
+
/**
|
|
54
|
+
* 将词级时间轴按目标粒度重组:
|
|
55
|
+
* - `'word'`:逐词编号返回;
|
|
56
|
+
* - `'sentence'`:保持原条目重新编号;
|
|
57
|
+
* - `'clause'`:按显示字重上限(CJK 计 1,ASCII 计 0.5)、停顿间隙(>=350ms)与子句标点智能聚合成紧凑短句。
|
|
58
|
+
*/
|
|
59
|
+
export declare function groupWordCues(wordCues: readonly SubtitleCue[], granularity: SubtitleGranularity, maxCharsPerCue?: number): SubtitleCue[];
|
|
60
|
+
/**
|
|
61
|
+
* 将字幕格式化为 SubStation Alpha (`v4.00+` `.ass`) 文本;
|
|
62
|
+
* 若同时提供词级时间轴 `wordCues`,自动生成带 `{\kf<cs>}` 逐词高亮标签的卡拉 OK 字幕。
|
|
63
|
+
*/
|
|
64
|
+
export declare function formatAssSubtitles(cues: readonly SubtitleCue[], wordCues?: readonly SubtitleCue[]): string;
|
|
46
65
|
/**
|
|
47
|
-
* 将字幕条目格式化为 SRT 或
|
|
66
|
+
* 将字幕条目格式化为 SRT、WebVTT 或 ASS(支持卡拉 OK 词级高亮)文本。
|
|
48
67
|
*/
|
|
49
|
-
export declare function formatSubtitles(cues: SubtitleCue[], format: 'srt' | 'vtt'): string;
|
|
68
|
+
export declare function formatSubtitles(cues: readonly SubtitleCue[], format: 'srt' | 'vtt' | 'ass', wordCues?: readonly SubtitleCue[]): string;
|
|
50
69
|
/**
|
|
51
70
|
* 生成自包含、支持明暗主题与倍速播放的 HTML5 音色试听卡片页面。
|
|
52
71
|
*/
|
|
@@ -64,14 +83,42 @@ export declare function parseSubtitleFile(content: string): SubtitleCue[];
|
|
|
64
83
|
* 将基础语速百分比(如 `+10%`)乘以缩放因子并钳制在 `[0.7, 1.85]`(`-30%` 至 `+85%`),返回 `'+25%'` / `'-10%'` 格式。
|
|
65
84
|
*/
|
|
66
85
|
export declare function scaleRatePercent(baseRate: string, factor: number): string;
|
|
86
|
+
/**
|
|
87
|
+
* 构造词边界安全的发音词典全局正则:
|
|
88
|
+
* - 按词长降序去重排序,避免短词抢先截断长词;
|
|
89
|
+
* - 纯 ASCII 单词/缩写(`OS` / `UI` / `SQL` / `Nginx`)自动包裹 `(?<![A-Za-z0-9_])...(?![A-Za-z0-9_])`,
|
|
90
|
+
* 使其匹配独立单词与中英混排(如 `"Mac OS"`、`"OS系统"`),但绝不误伤 `"POST"` / `"BUILD"` 等单词内部子串。
|
|
91
|
+
*/
|
|
92
|
+
export declare function buildPronunciationPattern(words: readonly string[]): RegExp | null;
|
|
67
93
|
/**
|
|
68
94
|
* 纯文本单遍发音词典替换(用于不支持 SSML `<sub alias>` 的 OpenAI / SiliconFlow 引擎)。
|
|
69
|
-
*
|
|
95
|
+
* 按原词长度降序构造单次正则同步替换,保证替读结果不会递归触发其它规则且保护 ASCII 词边界。
|
|
70
96
|
*/
|
|
71
97
|
export declare function applyPronunciationsPlain(text: string, rules: ReadonlyArray<{
|
|
72
98
|
word: string;
|
|
73
99
|
alias: string;
|
|
74
100
|
}>): string;
|
|
101
|
+
/** 行内停顿切分片段:朗读文本块或毫秒级停顿块。 */
|
|
102
|
+
export type SpeechPiece = {
|
|
103
|
+
kind: 'text';
|
|
104
|
+
text: string;
|
|
105
|
+
} | {
|
|
106
|
+
kind: 'pause';
|
|
107
|
+
durationMs: number;
|
|
108
|
+
};
|
|
109
|
+
/**
|
|
110
|
+
* 解析并切分行内停顿标签(如 `[pause:500ms]`、`[break:600ms]`、`[停顿0.6s]`),
|
|
111
|
+
* 返回非空文本与停顿交替序列(自动合并相邻停顿并钳制在 50..10000ms)。
|
|
112
|
+
*/
|
|
113
|
+
export declare function splitInlinePauses(text: string): SpeechPiece[];
|
|
114
|
+
/**
|
|
115
|
+
* 剥离行内停顿标签并折叠周围多余水平空白,生成干净的字幕展示文本。
|
|
116
|
+
*/
|
|
117
|
+
export declare function stripInlinePauses(text: string): string;
|
|
118
|
+
/**
|
|
119
|
+
* 使用本地 ffmpeg `atempo` 滤镜对超窗 MP3 进行保调精准变速压缩,确保字幕时间窗零累积漂移。
|
|
120
|
+
*/
|
|
121
|
+
export declare function fitAudioToWindow(mp3: Buffer, targetMaxMs: number, signal?: AbortSignal): Promise<Buffer>;
|
|
75
122
|
/**
|
|
76
123
|
* 使用本地 ffmpeg 对 MP3 音频执行播客级响度均衡(-16 LUFS / TP -1.5 dBTP / LRA 11)。
|
|
77
124
|
* @throws 取消时抛出 signal.reason,ffmpeg 失败时抛中文错误。
|
|
@@ -89,3 +136,29 @@ export interface BgmMixOptions {
|
|
|
89
136
|
* 自动循环 BGM、按人声侧链压缩闪避(ducking)、在结尾 1.2s 平滑淡出(总长 > 3s),并可选联动响度均衡。
|
|
90
137
|
*/
|
|
91
138
|
export declare function mixAudioWithBgm(voiceMp3: Buffer, opts: BgmMixOptions, signal?: AbortSignal): Promise<Buffer>;
|
|
139
|
+
/** 音频章节时间轴条目(毫秒级)。 */
|
|
140
|
+
export interface AudioChapter {
|
|
141
|
+
title: string;
|
|
142
|
+
startMs: number;
|
|
143
|
+
endMs: number;
|
|
144
|
+
}
|
|
145
|
+
/** Markdown 标题分节结果。 */
|
|
146
|
+
export interface MarkdownSection {
|
|
147
|
+
title: string;
|
|
148
|
+
text: string;
|
|
149
|
+
}
|
|
150
|
+
/**
|
|
151
|
+
* 按一级至三级 Markdown 标题(`#` / `##` / `###`)提取有声书/播客章节:
|
|
152
|
+
* 先剥离围栏代码块以避免将代码注释误判为标题;首标题前若有正文则归入 `导言` 章节。
|
|
153
|
+
*/
|
|
154
|
+
export declare function extractMarkdownSections(markdown: string, cleanMarkdown?: boolean): MarkdownSection[];
|
|
155
|
+
/**
|
|
156
|
+
* 纯 TypeScript 零依赖向 MP3 写入原生 ID3v2.3 `CTOC` + `CHAP` 章节书签标签(兼容 Apple Podcasts / VLC / ffmpeg / 车载播放器)。
|
|
157
|
+
*/
|
|
158
|
+
export declare function embedMp3Chapters(mp3: Buffer, chapters: readonly AudioChapter[], meta?: {
|
|
159
|
+
title?: string;
|
|
160
|
+
}): Buffer;
|
|
161
|
+
/**
|
|
162
|
+
* 将章节时间轴格式化为播客 Show Notes / YouTube 时间戳文本(`MM:SS 标题` 或跨小时 `HH:MM:SS 标题`)。
|
|
163
|
+
*/
|
|
164
|
+
export declare function formatChaptersMarkdown(chapters: readonly AudioChapter[]): string;
|
package/lib/tts-text.js
CHANGED
|
@@ -317,13 +317,62 @@ export function createSilenceMp3(durationMs, sampleRate = 24000) {
|
|
|
317
317
|
return out;
|
|
318
318
|
}
|
|
319
319
|
/**
|
|
320
|
-
*
|
|
320
|
+
* 剥离单段 MP3 的首尾 ID3 标签以及首帧 Xing / Info / VBRI 元数据空帧,
|
|
321
|
+
* 避免多段 VBR MP3 拼接后播放器或时长解析器被首段的帧数头锁死。
|
|
322
|
+
*/
|
|
323
|
+
export function stripMp3MetadataFrame(mp3) {
|
|
324
|
+
let afterId3v2 = stripId3v2(mp3);
|
|
325
|
+
while (afterId3v2.length < mp3.length) {
|
|
326
|
+
const next = stripId3v2(afterId3v2);
|
|
327
|
+
if (next.length === afterId3v2.length)
|
|
328
|
+
break;
|
|
329
|
+
afterId3v2 = next;
|
|
330
|
+
}
|
|
331
|
+
const data = stripId3v1(afterId3v2);
|
|
332
|
+
let offset = 0;
|
|
333
|
+
while (offset + 4 <= data.length) {
|
|
334
|
+
const header = parseMp3FrameHeader(data, offset);
|
|
335
|
+
if (header === null) {
|
|
336
|
+
offset += 1;
|
|
337
|
+
continue;
|
|
338
|
+
}
|
|
339
|
+
const xingOffset = offset + 4 + header.sideInfoSize;
|
|
340
|
+
if (xingOffset + 4 <= offset + header.frameSize && xingOffset + 4 <= data.length) {
|
|
341
|
+
const tag = data.subarray(xingOffset, xingOffset + 4).toString('ascii');
|
|
342
|
+
if (tag === 'Xing' || tag === 'Info') {
|
|
343
|
+
return data.subarray(offset + header.frameSize);
|
|
344
|
+
}
|
|
345
|
+
}
|
|
346
|
+
const vbriOffset = offset + 36;
|
|
347
|
+
if (vbriOffset + 4 <= offset + header.frameSize && vbriOffset + 4 <= data.length) {
|
|
348
|
+
const vbriTag = data.subarray(vbriOffset, vbriOffset + 4).toString('ascii');
|
|
349
|
+
if (vbriTag === 'VBRI') {
|
|
350
|
+
return data.subarray(offset + header.frameSize);
|
|
351
|
+
}
|
|
352
|
+
}
|
|
353
|
+
return data;
|
|
354
|
+
}
|
|
355
|
+
return data;
|
|
356
|
+
}
|
|
357
|
+
/**
|
|
358
|
+
* 拼接多段 MP3:单段原样返回;多段拼接时剥离各段的 Xing/Info/VBRI 元数据帧及非首段 ID3 标签。
|
|
321
359
|
*/
|
|
322
360
|
export function concatMp3Buffers(buffers) {
|
|
323
361
|
const nonEmpty = buffers.filter((buf) => buf.length > 0);
|
|
324
362
|
if (nonEmpty.length === 0)
|
|
325
363
|
return Buffer.alloc(0);
|
|
326
|
-
|
|
364
|
+
if (nonEmpty.length === 1)
|
|
365
|
+
return nonEmpty[0];
|
|
366
|
+
const normalized = nonEmpty.map((buf, index) => {
|
|
367
|
+
if (index === 0) {
|
|
368
|
+
const afterId3 = stripId3v2(buf);
|
|
369
|
+
if (afterId3.length < buf.length) {
|
|
370
|
+
const id3Prefix = buf.subarray(0, buf.length - afterId3.length);
|
|
371
|
+
return Buffer.concat([id3Prefix, stripMp3MetadataFrame(afterId3)]);
|
|
372
|
+
}
|
|
373
|
+
}
|
|
374
|
+
return stripMp3MetadataFrame(buf);
|
|
375
|
+
});
|
|
327
376
|
return Buffer.concat(normalized);
|
|
328
377
|
}
|
|
329
378
|
/**
|
|
@@ -343,6 +392,91 @@ export function offsetCues(cues, offsetMs, startIndex = 1) {
|
|
|
343
392
|
text: cue.text,
|
|
344
393
|
}));
|
|
345
394
|
}
|
|
395
|
+
function measureCueTextWeight(text) {
|
|
396
|
+
let weight = 0;
|
|
397
|
+
for (const ch of text) {
|
|
398
|
+
if (/\s/.test(ch))
|
|
399
|
+
continue;
|
|
400
|
+
const cp = ch.codePointAt(0) ?? 0;
|
|
401
|
+
weight += cp <= 0x00ff ? 0.5 : 1;
|
|
402
|
+
}
|
|
403
|
+
return weight;
|
|
404
|
+
}
|
|
405
|
+
/**
|
|
406
|
+
* 将词级时间轴按目标粒度重组:
|
|
407
|
+
* - `'word'`:逐词编号返回;
|
|
408
|
+
* - `'sentence'`:保持原条目重新编号;
|
|
409
|
+
* - `'clause'`:按显示字重上限(CJK 计 1,ASCII 计 0.5)、停顿间隙(>=350ms)与子句标点智能聚合成紧凑短句。
|
|
410
|
+
*/
|
|
411
|
+
export function groupWordCues(wordCues, granularity, maxCharsPerCue = 16) {
|
|
412
|
+
if (!Array.isArray(wordCues) || wordCues.length === 0)
|
|
413
|
+
return [];
|
|
414
|
+
if (granularity === 'sentence') {
|
|
415
|
+
return wordCues.map((cue, idx) => ({
|
|
416
|
+
index: idx + 1,
|
|
417
|
+
startMs: cue.startMs,
|
|
418
|
+
endMs: cue.endMs,
|
|
419
|
+
text: cue.text,
|
|
420
|
+
}));
|
|
421
|
+
}
|
|
422
|
+
const nonEmpty = wordCues.filter((cue) => typeof cue.text === 'string' && cue.text.trim() !== '');
|
|
423
|
+
if (nonEmpty.length === 0)
|
|
424
|
+
return [];
|
|
425
|
+
if (granularity === 'word') {
|
|
426
|
+
return nonEmpty.map((cue, idx) => ({
|
|
427
|
+
index: idx + 1,
|
|
428
|
+
startMs: cue.startMs,
|
|
429
|
+
endMs: cue.endMs,
|
|
430
|
+
text: cue.text.trim(),
|
|
431
|
+
}));
|
|
432
|
+
}
|
|
433
|
+
const limit = Math.max(4, Number.isFinite(maxCharsPerCue) ? maxCharsPerCue : 16);
|
|
434
|
+
const groups = [];
|
|
435
|
+
let curStartMs = 0;
|
|
436
|
+
let curEndMs = 0;
|
|
437
|
+
let curText = '';
|
|
438
|
+
let curWeight = 0;
|
|
439
|
+
let prevCue = null;
|
|
440
|
+
for (const cue of nonEmpty) {
|
|
441
|
+
const word = cue.text.trim();
|
|
442
|
+
const wWeight = measureCueTextWeight(word);
|
|
443
|
+
const isStandalonePunct = /^[。!?;,、.!?;,::]+$/.test(word);
|
|
444
|
+
if (prevCue !== null) {
|
|
445
|
+
const exceedsWeight = !isStandalonePunct && curWeight + wWeight > limit;
|
|
446
|
+
const hasPauseGap = cue.startMs - prevCue.endMs >= 350;
|
|
447
|
+
const endedClause = /[。!?;,、.!?;,::]$/.test(curText);
|
|
448
|
+
if (exceedsWeight || hasPauseGap || endedClause) {
|
|
449
|
+
groups.push({ startMs: curStartMs, endMs: curEndMs, text: curText });
|
|
450
|
+
curStartMs = cue.startMs;
|
|
451
|
+
curEndMs = Math.max(cue.startMs, cue.endMs);
|
|
452
|
+
curText = word;
|
|
453
|
+
curWeight = wWeight;
|
|
454
|
+
prevCue = cue;
|
|
455
|
+
continue;
|
|
456
|
+
}
|
|
457
|
+
curText += (needsSpaceBetween(curText, word) ? ' ' : '') + word;
|
|
458
|
+
curEndMs = Math.max(curEndMs, cue.endMs);
|
|
459
|
+
curWeight += wWeight;
|
|
460
|
+
prevCue = cue;
|
|
461
|
+
}
|
|
462
|
+
else {
|
|
463
|
+
curStartMs = cue.startMs;
|
|
464
|
+
curEndMs = Math.max(cue.startMs, cue.endMs);
|
|
465
|
+
curText = word;
|
|
466
|
+
curWeight = wWeight;
|
|
467
|
+
prevCue = cue;
|
|
468
|
+
}
|
|
469
|
+
}
|
|
470
|
+
if (prevCue !== null && curText !== '') {
|
|
471
|
+
groups.push({ startMs: curStartMs, endMs: curEndMs, text: curText });
|
|
472
|
+
}
|
|
473
|
+
return groups.map((g, idx) => ({
|
|
474
|
+
index: idx + 1,
|
|
475
|
+
startMs: g.startMs,
|
|
476
|
+
endMs: g.endMs,
|
|
477
|
+
text: g.text,
|
|
478
|
+
}));
|
|
479
|
+
}
|
|
346
480
|
function formatSubtitleTimestamp(ms, decimalSep) {
|
|
347
481
|
const safe = Math.max(0, Math.round(ms));
|
|
348
482
|
const hours = Math.floor(safe / 3600000);
|
|
@@ -354,10 +488,85 @@ function formatSubtitleTimestamp(ms, decimalSep) {
|
|
|
354
488
|
+ ':' + String(seconds).padStart(2, '0')
|
|
355
489
|
+ decimalSep + String(millis).padStart(3, '0');
|
|
356
490
|
}
|
|
491
|
+
function formatAssTimestamp(ms) {
|
|
492
|
+
const totalCs = Math.max(0, Math.round(ms / 10));
|
|
493
|
+
const cs = totalCs % 100;
|
|
494
|
+
const totalSec = Math.floor(totalCs / 100);
|
|
495
|
+
const sec = totalSec % 60;
|
|
496
|
+
const totalMin = Math.floor(totalSec / 60);
|
|
497
|
+
const min = totalMin % 60;
|
|
498
|
+
const hours = Math.floor(totalMin / 60);
|
|
499
|
+
return `${hours}:${String(min).padStart(2, '0')}:${String(sec).padStart(2, '0')}.${String(cs).padStart(2, '0')}`;
|
|
500
|
+
}
|
|
501
|
+
function escapeAssText(text) {
|
|
502
|
+
return text
|
|
503
|
+
.replace(/\r\n?/g, '\n')
|
|
504
|
+
.replace(/\{/g, '\\{')
|
|
505
|
+
.replace(/\}/g, '\\}')
|
|
506
|
+
.replace(/\n/g, '\\N');
|
|
507
|
+
}
|
|
508
|
+
/**
|
|
509
|
+
* 将字幕格式化为 SubStation Alpha (`v4.00+` `.ass`) 文本;
|
|
510
|
+
* 若同时提供词级时间轴 `wordCues`,自动生成带 `{\kf<cs>}` 逐词高亮标签的卡拉 OK 字幕。
|
|
511
|
+
*/
|
|
512
|
+
export function formatAssSubtitles(cues, wordCues) {
|
|
513
|
+
const headerLines = [
|
|
514
|
+
'[Script Info]',
|
|
515
|
+
'Title: dsh-voice Subtitles',
|
|
516
|
+
'ScriptType: v4.00+',
|
|
517
|
+
'WrapStyle: 0',
|
|
518
|
+
'ScaledBorderAndShadow: yes',
|
|
519
|
+
'PlayResX: 1080',
|
|
520
|
+
'PlayResY: 1920',
|
|
521
|
+
'',
|
|
522
|
+
'[V4+ Styles]',
|
|
523
|
+
'Format: Name, Fontname, Fontsize, PrimaryColour, SecondaryColour, OutlineColour, BackColour, Bold, Italic, Underline, StrikeOut, ScaleX, ScaleY, Spacing, Angle, BorderStyle, Outline, Shadow, Alignment, MarginL, MarginR, MarginV, Encoding',
|
|
524
|
+
'Style: Default,Microsoft YaHei,58,&H0000FFFF,&H00FFFFFF,&H00000000,&H80000000,-1,0,0,0,100,100,0,0,1,3,1,2,60,60,180,1',
|
|
525
|
+
'',
|
|
526
|
+
'[Events]',
|
|
527
|
+
'Format: Layer, Start, End, Style, Name, MarginL, MarginR, MarginV, Effect, Text',
|
|
528
|
+
];
|
|
529
|
+
const hasWords = Array.isArray(wordCues) && wordCues.length > 0;
|
|
530
|
+
const dialogues = [];
|
|
531
|
+
for (const cue of cues) {
|
|
532
|
+
const startStr = formatAssTimestamp(cue.startMs);
|
|
533
|
+
const endStr = formatAssTimestamp(Math.max(cue.startMs, cue.endMs));
|
|
534
|
+
let renderedText = '';
|
|
535
|
+
if (hasWords) {
|
|
536
|
+
const matching = wordCues.filter((w) => typeof w.text === 'string'
|
|
537
|
+
&& w.text.trim() !== ''
|
|
538
|
+
&& ((w.endMs > cue.startMs && w.startMs < cue.endMs)
|
|
539
|
+
|| (w.startMs === w.endMs && w.startMs >= cue.startMs && w.startMs <= cue.endMs)));
|
|
540
|
+
if (matching.length > 0) {
|
|
541
|
+
const speakerMatch = /^(\[[^\]]+\]\s+)/.exec(cue.text);
|
|
542
|
+
if (speakerMatch && !matching[0].text.trim().startsWith('[')) {
|
|
543
|
+
renderedText += escapeAssText(speakerMatch[1]);
|
|
544
|
+
}
|
|
545
|
+
for (let i = 0; i < matching.length; i++) {
|
|
546
|
+
const w = matching[i];
|
|
547
|
+
const rawWord = w.text.trim();
|
|
548
|
+
if (i > 0 && needsSpaceBetween(matching[i - 1].text.trim(), rawWord)) {
|
|
549
|
+
renderedText += ' ';
|
|
550
|
+
}
|
|
551
|
+
const durCs = Math.max(1, Math.round((w.endMs - w.startMs) / 10));
|
|
552
|
+
renderedText += `{\\kf${durCs}}${escapeAssText(rawWord)}`;
|
|
553
|
+
}
|
|
554
|
+
}
|
|
555
|
+
}
|
|
556
|
+
if (renderedText === '') {
|
|
557
|
+
renderedText = escapeAssText(cue.text);
|
|
558
|
+
}
|
|
559
|
+
dialogues.push(`Dialogue: 0,${startStr},${endStr},Default,,0,0,0,,${renderedText}`);
|
|
560
|
+
}
|
|
561
|
+
return [...headerLines, ...dialogues].join('\n') + '\n';
|
|
562
|
+
}
|
|
357
563
|
/**
|
|
358
|
-
* 将字幕条目格式化为 SRT 或
|
|
564
|
+
* 将字幕条目格式化为 SRT、WebVTT 或 ASS(支持卡拉 OK 词级高亮)文本。
|
|
359
565
|
*/
|
|
360
|
-
export function formatSubtitles(cues, format) {
|
|
566
|
+
export function formatSubtitles(cues, format, wordCues) {
|
|
567
|
+
if (format === 'ass') {
|
|
568
|
+
return formatAssSubtitles(cues, wordCues);
|
|
569
|
+
}
|
|
361
570
|
if (format === 'vtt') {
|
|
362
571
|
if (cues.length === 0)
|
|
363
572
|
return 'WEBVTT\n\n';
|
|
@@ -559,9 +768,35 @@ export function scaleRatePercent(baseRate, factor) {
|
|
|
559
768
|
const percent = Math.round((combined - 1) * 100);
|
|
560
769
|
return (percent >= 0 ? '+' : '') + String(percent) + '%';
|
|
561
770
|
}
|
|
771
|
+
function escapeRegExp(value) {
|
|
772
|
+
return value.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
|
|
773
|
+
}
|
|
774
|
+
/**
|
|
775
|
+
* 构造词边界安全的发音词典全局正则:
|
|
776
|
+
* - 按词长降序去重排序,避免短词抢先截断长词;
|
|
777
|
+
* - 纯 ASCII 单词/缩写(`OS` / `UI` / `SQL` / `Nginx`)自动包裹 `(?<![A-Za-z0-9_])...(?![A-Za-z0-9_])`,
|
|
778
|
+
* 使其匹配独立单词与中英混排(如 `"Mac OS"`、`"OS系统"`),但绝不误伤 `"POST"` / `"BUILD"` 等单词内部子串。
|
|
779
|
+
*/
|
|
780
|
+
export function buildPronunciationPattern(words) {
|
|
781
|
+
if (!Array.isArray(words) || words.length === 0)
|
|
782
|
+
return null;
|
|
783
|
+
const unique = Array.from(new Set(words
|
|
784
|
+
.filter((w) => typeof w === 'string')
|
|
785
|
+
.map((w) => w.trim())
|
|
786
|
+
.filter((w) => w.length > 0))).sort((a, b) => b.length - a.length);
|
|
787
|
+
if (unique.length === 0)
|
|
788
|
+
return null;
|
|
789
|
+
const alternatives = unique.map((word) => {
|
|
790
|
+
const escaped = escapeRegExp(word);
|
|
791
|
+
return /^[A-Za-z0-9_]+$/.test(word)
|
|
792
|
+
? `(?<![A-Za-z0-9_])${escaped}(?![A-Za-z0-9_])`
|
|
793
|
+
: escaped;
|
|
794
|
+
});
|
|
795
|
+
return new RegExp(alternatives.join('|'), 'g');
|
|
796
|
+
}
|
|
562
797
|
/**
|
|
563
798
|
* 纯文本单遍发音词典替换(用于不支持 SSML `<sub alias>` 的 OpenAI / SiliconFlow 引擎)。
|
|
564
|
-
*
|
|
799
|
+
* 按原词长度降序构造单次正则同步替换,保证替读结果不会递归触发其它规则且保护 ASCII 词边界。
|
|
565
800
|
*/
|
|
566
801
|
export function applyPronunciationsPlain(text, rules) {
|
|
567
802
|
if (text === '' || !Array.isArray(rules) || rules.length === 0)
|
|
@@ -576,12 +811,82 @@ export function applyPronunciationsPlain(text, rules) {
|
|
|
576
811
|
map.set(word, alias);
|
|
577
812
|
}
|
|
578
813
|
}
|
|
579
|
-
|
|
814
|
+
const regex = buildPronunciationPattern(Array.from(map.keys()));
|
|
815
|
+
if (regex === null)
|
|
580
816
|
return text;
|
|
581
|
-
const sortedWords = Array.from(map.keys()).sort((a, b) => b.length - a.length);
|
|
582
|
-
const regex = new RegExp(sortedWords.map((w) => w.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')).join('|'), 'g');
|
|
583
817
|
return text.replace(regex, (matched) => map.get(matched) ?? matched);
|
|
584
818
|
}
|
|
819
|
+
const INLINE_PAUSE_SOURCE = '\\[(?:pause|break|停顿)\\s*[::=]?\\s*(\\d+(?:\\.\\d+)?)\\s*(ms|s|秒|毫秒)?\\]';
|
|
820
|
+
function parseInlinePauseMs(rawNum, rawUnit) {
|
|
821
|
+
const value = Number(rawNum);
|
|
822
|
+
const unit = (rawUnit ?? 'ms').toLowerCase();
|
|
823
|
+
const ms = unit === 's' || unit === '秒' ? value * 1000 : value;
|
|
824
|
+
if (!Number.isFinite(ms))
|
|
825
|
+
return 500;
|
|
826
|
+
return Math.min(10000, Math.max(50, Math.round(ms)));
|
|
827
|
+
}
|
|
828
|
+
/**
|
|
829
|
+
* 解析并切分行内停顿标签(如 `[pause:500ms]`、`[break:600ms]`、`[停顿0.6s]`),
|
|
830
|
+
* 返回非空文本与停顿交替序列(自动合并相邻停顿并钳制在 50..10000ms)。
|
|
831
|
+
*/
|
|
832
|
+
export function splitInlinePauses(text) {
|
|
833
|
+
if (typeof text !== 'string' || text === '')
|
|
834
|
+
return [];
|
|
835
|
+
const re = new RegExp(INLINE_PAUSE_SOURCE, 'gi');
|
|
836
|
+
const pieces = [];
|
|
837
|
+
let lastIndex = 0;
|
|
838
|
+
let match;
|
|
839
|
+
const pushPause = (durationMs) => {
|
|
840
|
+
const prev = pieces[pieces.length - 1];
|
|
841
|
+
if (prev && prev.kind === 'pause') {
|
|
842
|
+
prev.durationMs = Math.min(10000, prev.durationMs + durationMs);
|
|
843
|
+
}
|
|
844
|
+
else {
|
|
845
|
+
pieces.push({ kind: 'pause', durationMs });
|
|
846
|
+
}
|
|
847
|
+
};
|
|
848
|
+
while ((match = re.exec(text)) !== null) {
|
|
849
|
+
if (match.index > lastIndex) {
|
|
850
|
+
const part = text.slice(lastIndex, match.index).trim();
|
|
851
|
+
if (part !== '') {
|
|
852
|
+
pieces.push({ kind: 'text', text: part });
|
|
853
|
+
}
|
|
854
|
+
}
|
|
855
|
+
pushPause(parseInlinePauseMs(match[1], match[2]));
|
|
856
|
+
lastIndex = match.index + match[0].length;
|
|
857
|
+
}
|
|
858
|
+
if (lastIndex < text.length) {
|
|
859
|
+
const tail = text.slice(lastIndex).trim();
|
|
860
|
+
if (tail !== '') {
|
|
861
|
+
pieces.push({ kind: 'text', text: tail });
|
|
862
|
+
}
|
|
863
|
+
}
|
|
864
|
+
return pieces;
|
|
865
|
+
}
|
|
866
|
+
/**
|
|
867
|
+
* 剥离行内停顿标签并折叠周围多余水平空白,生成干净的字幕展示文本。
|
|
868
|
+
*/
|
|
869
|
+
export function stripInlinePauses(text) {
|
|
870
|
+
if (typeof text !== 'string' || text === '')
|
|
871
|
+
return '';
|
|
872
|
+
const re = new RegExp(INLINE_PAUSE_SOURCE, 'gi');
|
|
873
|
+
const replaced = text.replace(re, (matched, _num, _unit, offset, full) => {
|
|
874
|
+
const before = full.slice(0, offset);
|
|
875
|
+
const after = full.slice(offset + matched.length);
|
|
876
|
+
if (before !== ''
|
|
877
|
+
&& after !== ''
|
|
878
|
+
&& !/[ \t\r\n]$/.test(before)
|
|
879
|
+
&& !/^[ \t\r\n]/.test(after)
|
|
880
|
+
&& needsSpaceBetween(before, after)) {
|
|
881
|
+
return ' ';
|
|
882
|
+
}
|
|
883
|
+
return '';
|
|
884
|
+
});
|
|
885
|
+
return replaced
|
|
886
|
+
.replace(/[ \t]{2,}/g, ' ')
|
|
887
|
+
.replace(/^[ \t]+|[ \t]+$/gm, '')
|
|
888
|
+
.trim();
|
|
889
|
+
}
|
|
585
890
|
function throwIfAborted(signal) {
|
|
586
891
|
if (signal?.aborted === true)
|
|
587
892
|
throw signal.reason;
|
|
@@ -605,6 +910,57 @@ function runFfmpegArgs(args, errorPrefix, signal) {
|
|
|
605
910
|
});
|
|
606
911
|
});
|
|
607
912
|
}
|
|
913
|
+
/**
|
|
914
|
+
* 使用本地 ffmpeg `atempo` 滤镜对超窗 MP3 进行保调精准变速压缩,确保字幕时间窗零累积漂移。
|
|
915
|
+
*/
|
|
916
|
+
export async function fitAudioToWindow(mp3, targetMaxMs, signal) {
|
|
917
|
+
throwIfAborted(signal);
|
|
918
|
+
if (mp3.length === 0 || !Number.isFinite(targetMaxMs) || targetMaxMs <= 50) {
|
|
919
|
+
return mp3;
|
|
920
|
+
}
|
|
921
|
+
const info = inspectMp3Buffer(mp3);
|
|
922
|
+
if (info.durationMs <= targetMaxMs) {
|
|
923
|
+
return mp3;
|
|
924
|
+
}
|
|
925
|
+
const ratio = info.durationMs / targetMaxMs;
|
|
926
|
+
const atempoFilter = ratio <= 2.0
|
|
927
|
+
? `atempo=${ratio.toFixed(4)}`
|
|
928
|
+
: `atempo=2.0,atempo=${Math.min(2.0, ratio / 2).toFixed(4)}`;
|
|
929
|
+
const targetRate = [8000, 11025, 12000, 16000, 22050, 24000, 32000, 44100, 48000].includes(info.sampleRate)
|
|
930
|
+
? info.sampleRate
|
|
931
|
+
: 24000;
|
|
932
|
+
const bitrateArg = String(Math.min(320, Math.max(32, info.bitrateKbps || 48))) + 'k';
|
|
933
|
+
const tempDir = mkdtempSync(join(tmpdir(), 'dsh-voice-atempo-'));
|
|
934
|
+
try {
|
|
935
|
+
const inPath = join(tempDir, 'in.mp3');
|
|
936
|
+
const outPath = join(tempDir, 'out.mp3');
|
|
937
|
+
writeFileSync(inPath, mp3);
|
|
938
|
+
await runFfmpegArgs([
|
|
939
|
+
'-y',
|
|
940
|
+
'-i', inPath,
|
|
941
|
+
'-vn',
|
|
942
|
+
'-af', atempoFilter,
|
|
943
|
+
'-ar', String(targetRate),
|
|
944
|
+
'-ac', String(info.channels),
|
|
945
|
+
'-b:a', bitrateArg,
|
|
946
|
+
outPath,
|
|
947
|
+
], 'ffmpeg 语速窗口对齐失败:', signal);
|
|
948
|
+
throwIfAborted(signal);
|
|
949
|
+
const fitted = readFileSync(outPath);
|
|
950
|
+
if (fitted.length === 0) {
|
|
951
|
+
throw new Error('ffmpeg 语速窗口对齐结果为空。');
|
|
952
|
+
}
|
|
953
|
+
return fitted;
|
|
954
|
+
}
|
|
955
|
+
finally {
|
|
956
|
+
try {
|
|
957
|
+
rmSync(tempDir, { recursive: true, force: true });
|
|
958
|
+
}
|
|
959
|
+
catch {
|
|
960
|
+
// 尽力清理临时目录
|
|
961
|
+
}
|
|
962
|
+
}
|
|
963
|
+
}
|
|
608
964
|
/**
|
|
609
965
|
* 使用本地 ffmpeg 对 MP3 音频执行播客级响度均衡(-16 LUFS / TP -1.5 dBTP / LRA 11)。
|
|
610
966
|
* @throws 取消时抛出 signal.reason,ffmpeg 失败时抛中文错误。
|
|
@@ -695,3 +1051,136 @@ export async function mixAudioWithBgm(voiceMp3, opts, signal) {
|
|
|
695
1051
|
}
|
|
696
1052
|
}
|
|
697
1053
|
}
|
|
1054
|
+
/**
|
|
1055
|
+
* 按一级至三级 Markdown 标题(`#` / `##` / `###`)提取有声书/播客章节:
|
|
1056
|
+
* 先剥离围栏代码块以避免将代码注释误判为标题;首标题前若有正文则归入 `导言` 章节。
|
|
1057
|
+
*/
|
|
1058
|
+
export function extractMarkdownSections(markdown, cleanMarkdown = true) {
|
|
1059
|
+
if (typeof markdown !== 'string' || markdown.trim() === '')
|
|
1060
|
+
return [];
|
|
1061
|
+
let src = markdown.replace(/^\uFEFF/, '').replace(/\r\n?/g, '\n');
|
|
1062
|
+
src = src.replace(/```[\s\S]*?```/g, '').replace(/~~~[\s\S]*?~~~/g, '');
|
|
1063
|
+
const headingRe = /^(#{1,3})\s+(.+)$/gm;
|
|
1064
|
+
const matches = [];
|
|
1065
|
+
let m;
|
|
1066
|
+
while ((m = headingRe.exec(src)) !== null) {
|
|
1067
|
+
matches.push({
|
|
1068
|
+
index: m.index,
|
|
1069
|
+
fullLength: m[0].length,
|
|
1070
|
+
rawTitle: m[2].replace(/\s+#+\s*$/, '').trim(),
|
|
1071
|
+
});
|
|
1072
|
+
}
|
|
1073
|
+
if (matches.length === 0)
|
|
1074
|
+
return [];
|
|
1075
|
+
const sections = [];
|
|
1076
|
+
const rawIntro = src.slice(0, matches[0].index);
|
|
1077
|
+
const cleanedIntro = cleanMarkdown ? cleanMarkdownForSpeech(rawIntro) : rawIntro.trim();
|
|
1078
|
+
if (cleanedIntro !== '') {
|
|
1079
|
+
sections.push({ title: '导言', text: cleanedIntro });
|
|
1080
|
+
}
|
|
1081
|
+
for (let i = 0; i < matches.length; i++) {
|
|
1082
|
+
const cur = matches[i];
|
|
1083
|
+
const bodyStart = cur.index + cur.fullLength;
|
|
1084
|
+
const bodyEnd = i + 1 < matches.length ? matches[i + 1].index : src.length;
|
|
1085
|
+
const rawBody = src.slice(bodyStart, bodyEnd);
|
|
1086
|
+
const cleanedTitle = (cleanMarkdown ? cleanMarkdownForSpeech(cur.rawTitle) : cur.rawTitle).trim() || cur.rawTitle;
|
|
1087
|
+
const cleanedBody = cleanMarkdown ? cleanMarkdownForSpeech(rawBody) : rawBody.trim();
|
|
1088
|
+
if (cleanedTitle === '' && cleanedBody === '')
|
|
1089
|
+
continue;
|
|
1090
|
+
const combined = cleanedBody !== ''
|
|
1091
|
+
? (/[。!?.!?]$/.test(cleanedTitle) ? `${cleanedTitle}\n${cleanedBody}` : `${cleanedTitle}。\n${cleanedBody}`)
|
|
1092
|
+
: cleanedTitle;
|
|
1093
|
+
if (combined.trim() !== '') {
|
|
1094
|
+
sections.push({ title: cleanedTitle, text: combined });
|
|
1095
|
+
}
|
|
1096
|
+
}
|
|
1097
|
+
return sections;
|
|
1098
|
+
}
|
|
1099
|
+
function encodeId3v23Frame(frameId, payload) {
|
|
1100
|
+
const header = Buffer.alloc(10);
|
|
1101
|
+
header.write(frameId, 0, 4, 'ascii');
|
|
1102
|
+
header.writeUInt32BE(payload.length, 4);
|
|
1103
|
+
header[8] = 0x00;
|
|
1104
|
+
header[9] = 0x00;
|
|
1105
|
+
return Buffer.concat([header, payload]);
|
|
1106
|
+
}
|
|
1107
|
+
function encodeTit2Frame(title) {
|
|
1108
|
+
const textBytes = Buffer.from(title, 'utf16le');
|
|
1109
|
+
const payload = Buffer.alloc(3 + textBytes.length + 2);
|
|
1110
|
+
payload[0] = 0x01; // UTF-16 with BOM
|
|
1111
|
+
payload[1] = 0xff;
|
|
1112
|
+
payload[2] = 0xfe;
|
|
1113
|
+
textBytes.copy(payload, 3);
|
|
1114
|
+
payload[3 + textBytes.length] = 0x00;
|
|
1115
|
+
payload[3 + textBytes.length + 1] = 0x00;
|
|
1116
|
+
return encodeId3v23Frame('TIT2', payload);
|
|
1117
|
+
}
|
|
1118
|
+
/**
|
|
1119
|
+
* 纯 TypeScript 零依赖向 MP3 写入原生 ID3v2.3 `CTOC` + `CHAP` 章节书签标签(兼容 Apple Podcasts / VLC / ffmpeg / 车载播放器)。
|
|
1120
|
+
*/
|
|
1121
|
+
export function embedMp3Chapters(mp3, chapters, meta) {
|
|
1122
|
+
if (mp3.length === 0 || !Array.isArray(chapters) || chapters.length === 0) {
|
|
1123
|
+
return mp3;
|
|
1124
|
+
}
|
|
1125
|
+
let body = stripId3v2(mp3);
|
|
1126
|
+
while (body.length < mp3.length) {
|
|
1127
|
+
const next = stripId3v2(body);
|
|
1128
|
+
if (next.length === body.length)
|
|
1129
|
+
break;
|
|
1130
|
+
body = next;
|
|
1131
|
+
}
|
|
1132
|
+
const capped = chapters.slice(0, 255);
|
|
1133
|
+
const frames = [];
|
|
1134
|
+
if (typeof meta?.title === 'string' && meta.title.trim() !== '') {
|
|
1135
|
+
frames.push(encodeTit2Frame(meta.title.trim()));
|
|
1136
|
+
}
|
|
1137
|
+
const childIdBuffers = capped.map((_ch, idx) => Buffer.from(`chp${idx}\0`, 'ascii'));
|
|
1138
|
+
const ctocHeader = Buffer.from([0x74, 0x6f, 0x63, 0x31, 0x00, 0x03, capped.length]); // 'toc1\0' + flags(0x03) + count
|
|
1139
|
+
frames.push(encodeId3v23Frame('CTOC', Buffer.concat([ctocHeader, ...childIdBuffers])));
|
|
1140
|
+
for (let i = 0; i < capped.length; i++) {
|
|
1141
|
+
const ch = capped[i];
|
|
1142
|
+
const elemId = childIdBuffers[i];
|
|
1143
|
+
const startMs = Math.max(0, Math.round(Number.isFinite(ch.startMs) ? ch.startMs : 0));
|
|
1144
|
+
const endMs = Math.max(startMs, Math.round(Number.isFinite(ch.endMs) ? ch.endMs : startMs));
|
|
1145
|
+
const timing = Buffer.alloc(16);
|
|
1146
|
+
timing.writeUInt32BE(startMs >>> 0, 0);
|
|
1147
|
+
timing.writeUInt32BE(endMs >>> 0, 4);
|
|
1148
|
+
timing.writeUInt32BE(0xffffffff, 8);
|
|
1149
|
+
timing.writeUInt32BE(0xffffffff, 12);
|
|
1150
|
+
const tit2 = encodeTit2Frame(ch.title);
|
|
1151
|
+
frames.push(encodeId3v23Frame('CHAP', Buffer.concat([elemId, timing, tit2])));
|
|
1152
|
+
}
|
|
1153
|
+
const tagPayload = Buffer.concat(frames);
|
|
1154
|
+
const len = tagPayload.length;
|
|
1155
|
+
const id3Header = Buffer.alloc(10);
|
|
1156
|
+
id3Header[0] = 0x49; // 'I'
|
|
1157
|
+
id3Header[1] = 0x44; // 'D'
|
|
1158
|
+
id3Header[2] = 0x33; // '3'
|
|
1159
|
+
id3Header[3] = 0x03; // v2.3
|
|
1160
|
+
id3Header[4] = 0x00; // revision 0
|
|
1161
|
+
id3Header[5] = 0x00; // flags
|
|
1162
|
+
id3Header[6] = (len >>> 21) & 0x7f;
|
|
1163
|
+
id3Header[7] = (len >>> 14) & 0x7f;
|
|
1164
|
+
id3Header[8] = (len >>> 7) & 0x7f;
|
|
1165
|
+
id3Header[9] = len & 0x7f;
|
|
1166
|
+
return Buffer.concat([id3Header, tagPayload, body]);
|
|
1167
|
+
}
|
|
1168
|
+
/**
|
|
1169
|
+
* 将章节时间轴格式化为播客 Show Notes / YouTube 时间戳文本(`MM:SS 标题` 或跨小时 `HH:MM:SS 标题`)。
|
|
1170
|
+
*/
|
|
1171
|
+
export function formatChaptersMarkdown(chapters) {
|
|
1172
|
+
if (!Array.isArray(chapters) || chapters.length === 0)
|
|
1173
|
+
return '';
|
|
1174
|
+
const useHours = chapters.some((ch) => Math.max(0, Math.round(ch.startMs)) >= 3600000 || Math.max(0, Math.round(ch.endMs)) >= 3600000);
|
|
1175
|
+
const lines = chapters.map((ch) => {
|
|
1176
|
+
const totalSec = Math.max(0, Math.floor(Math.round(ch.startMs) / 1000));
|
|
1177
|
+
const hours = Math.floor(totalSec / 3600);
|
|
1178
|
+
const minutes = Math.floor((totalSec % 3600) / 60);
|
|
1179
|
+
const seconds = totalSec % 60;
|
|
1180
|
+
const stamp = useHours
|
|
1181
|
+
? `${String(hours).padStart(2, '0')}:${String(minutes).padStart(2, '0')}:${String(seconds).padStart(2, '0')}`
|
|
1182
|
+
: `${String(minutes).padStart(2, '0')}:${String(seconds).padStart(2, '0')}`;
|
|
1183
|
+
return `${stamp} ${ch.title.trim()}`;
|
|
1184
|
+
});
|
|
1185
|
+
return lines.join('\n') + '\n';
|
|
1186
|
+
}
|