dsh-podwave 0.0.0-stage → 0.10.19

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.
@@ -0,0 +1,1123 @@
1
+ // 转写任务管理:调用 MLX Qwen3-ASR CLI,把结果落到 ~/.podwave/transcripts/<id>.json。
2
+ // 一次只跑一个任务,其余排队,避免在 16GB 内存的机器上同时加载多个模型。
3
+ // 长音频按 chunkMinutes 切块(ffmpeg),逐块转写后合并时间戳;块结果留在 .work/<id> 里,取消/崩溃后可续跑。
4
+ // transcript 使用 v2 结构:lines[](行级时间戳 + 每行译文)和 providers{}(每个目标语言的翻译来源)。
5
+ import { spawn } from 'node:child_process'
6
+ import fs from 'node:fs'
7
+ import path from 'node:path'
8
+ import { copyFile, mkdir, readFile, readdir, rename, rm, writeFile } from 'node:fs/promises'
9
+ import { fileURLToPath } from 'node:url'
10
+ import { DATA_DIR } from './state.js'
11
+ import { loadModelConfig, modelDir } from './models.js'
12
+ import { ffmpegPath, ffprobePath, venvPython } from './platform.js'
13
+ import { getSection } from './config.js'
14
+ import { assignSpeakers, diarizeInfo, runDiarization } from './diarize.js'
15
+ import { flushJobs, getJob, jobKey, removeJob, upsertJob } from './jobs.js'
16
+
17
+ export const TRANSCRIPTS_DIR = path.join(DATA_DIR, 'transcripts')
18
+ const WORK_DIR = path.join(TRANSCRIPTS_DIR, '.work')
19
+ const RUNTIME_DIR = path.join(DATA_DIR, 'runtime')
20
+ const CLI = path.join(RUNTIME_DIR, '.venv', 'bin', 'mlx-qwen3-asr')
21
+ const SHERPA_SCRIPT = path.join(path.dirname(fileURLToPath(import.meta.url)), '..', 'scripts', 'asr_sherpa_batch.py')
22
+ const FFMPEG = process.env.PODWAVE_FFMPEG || ffmpegPath() || 'ffmpeg'
23
+ const FFPROBE = process.env.PODWAVE_FFPROBE || ffprobePath() || 'ffprobe'
24
+
25
+ // 估算进度用:转写速度约 3.2x 实时(含每次启动加载模型的固定开销)。
26
+ const SPEED = 3.2
27
+
28
+ const queue = [] // { episode, force, resume }
29
+ let active = null // { key, episodeId }
30
+ let activeChild = null
31
+ const cancelRequested = new Set()
32
+
33
+ function transcriptPath(id) {
34
+ return path.join(TRANSCRIPTS_DIR, `${id}.json`)
35
+ }
36
+
37
+ function round2(value) {
38
+ return Math.round((Number(value) || 0) * 100) / 100
39
+ }
40
+
41
+ export function currentAsrModel() {
42
+ const name = loadModelConfig().asrModel
43
+ const dir = modelDir(name)
44
+ const engine = /sensevoice|sherpa/i.test(name) ? 'sherpa' : 'mlx'
45
+ const ready =
46
+ engine === 'sherpa'
47
+ ? fs.existsSync(path.join(dir, 'model.int8.onnx'))
48
+ : fs.existsSync(path.join(dir, 'model.safetensors'))
49
+ return { name, dir, ready, engine }
50
+ }
51
+
52
+ export function transcribeConfig() {
53
+ const section = getSection('transcribe') || {}
54
+ const hotwords = Array.isArray(section.hotwords) ? section.hotwords.map((word) => String(word || '').trim()).filter(Boolean) : []
55
+ const raw = Number(section.chunkMinutes)
56
+ const chunkMinutes = Math.max(1, Math.min(60, Number.isFinite(raw) && raw > 0 ? raw : 15))
57
+ return { hotwords, chunkMinutes, diarize: section.diarize !== false }
58
+ }
59
+
60
+ export function runtimeInfo() {
61
+ const model = currentAsrModel()
62
+ const config = transcribeConfig()
63
+ const sherpa = model.engine === 'sherpa'
64
+ return {
65
+ engine: model.engine,
66
+ installed: sherpa ? fs.existsSync(SHERPA_SCRIPT) && fs.existsSync(venvPython()) : fs.existsSync(CLI),
67
+ modelReady: model.ready,
68
+ model: model.dir,
69
+ modelName: model.name,
70
+ cli: sherpa ? venvPython() : CLI,
71
+ hotwords: config.hotwords,
72
+ chunkMinutes: config.chunkMinutes,
73
+ diarize: config.diarize,
74
+ diarization: diarizeInfo(),
75
+ }
76
+ }
77
+
78
+ function jobId(episodeId) {
79
+ return jobKey({ type: 'transcribe', episodeId })
80
+ }
81
+
82
+ // 把老格式(segments + translations[target][id])升级成 v2(lines + line.translations[target])。
83
+ // 已经是 v2 的原样返回;无法识别返回 null。
84
+ export function normalizeTranscript(raw, fallbackId = '') {
85
+ if (!raw || typeof raw !== 'object') return null
86
+ if (raw.schemaVersion === 2 && Array.isArray(raw.lines)) return raw
87
+
88
+ const segments = Array.isArray(raw.segments) ? raw.segments : Array.isArray(raw.lines) ? raw.lines : []
89
+ const translations = raw.translations && typeof raw.translations === 'object' ? raw.translations : {}
90
+ const providers = {}
91
+ for (const [target, mapping] of Object.entries(translations)) {
92
+ if (target === '_provider' || !mapping || typeof mapping !== 'object') continue
93
+ if (typeof mapping._provider === 'string' && mapping._provider) providers[target] = mapping._provider
94
+ }
95
+
96
+ const lines = []
97
+ for (const [index, seg] of segments.entries()) {
98
+ const text = String(seg?.text || '').trim()
99
+ if (!text) continue
100
+ const oldId = Number.isFinite(Number(seg?.id)) ? Number(seg.id) : index + 1
101
+ const lineTranslations = {}
102
+ for (const [target, mapping] of Object.entries(translations)) {
103
+ if (target === '_provider' || !mapping || typeof mapping !== 'object') continue
104
+ const value = mapping[String(oldId)] ?? mapping[oldId]
105
+ if (value) lineTranslations[target] = String(value)
106
+ }
107
+ lines.push({
108
+ id: lines.length + 1,
109
+ start: round2(seg.start),
110
+ end: round2(seg.end),
111
+ text,
112
+ speaker: seg.speaker ?? null,
113
+ translations: lineTranslations,
114
+ })
115
+ }
116
+
117
+ const id = String(raw.id || fallbackId || '')
118
+ return {
119
+ schemaVersion: 2,
120
+ id,
121
+ title: String(raw.title || ''),
122
+ createdAt: Number(raw.createdAt) || Date.now(),
123
+ updatedAt: Date.now(),
124
+ model: String(raw.model || ''),
125
+ language: String(raw.language || ''),
126
+ duration: Number(raw.duration) || round2(lines[lines.length - 1]?.end || 0),
127
+ text: String(raw.text || lines.map((line) => line.text).join(' ')),
128
+ source: raw.source && typeof raw.source === 'object' ? raw.source : null,
129
+ lines,
130
+ providers,
131
+ speakers: Array.isArray(raw.speakers) ? raw.speakers : [],
132
+ migratedFrom: Number(raw.schemaVersion) || 1,
133
+ }
134
+ }
135
+
136
+ async function saveTranscript(transcript) {
137
+ await mkdir(TRANSCRIPTS_DIR, { recursive: true })
138
+ const target = transcriptPath(transcript.id)
139
+ const tmp = `${target}.tmp`
140
+ await writeFile(tmp, JSON.stringify({ ...transcript, schemaVersion: 2, updatedAt: Date.now() }, null, 2), 'utf8')
141
+ await rename(tmp, target)
142
+ }
143
+
144
+ export { saveTranscript }
145
+
146
+ export async function readTranscript(id) {
147
+ let raw
148
+ try {
149
+ raw = JSON.parse(await readFile(transcriptPath(id), 'utf8'))
150
+ } catch {
151
+ return null
152
+ }
153
+ const normalized = normalizeTranscript(raw, id)
154
+ if (normalized && raw.schemaVersion !== 2) {
155
+ try {
156
+ await saveTranscript(normalized)
157
+ } catch {
158
+ /* 写回失败不阻塞读取 */
159
+ }
160
+ }
161
+ return normalized
162
+ }
163
+
164
+ // 启动时把目录里所有老 transcript 迁成 v2,并留一份 <id>.json.v1.bak。
165
+ export async function migrateAllTranscripts() {
166
+ let names = []
167
+ try {
168
+ names = await readdir(TRANSCRIPTS_DIR)
169
+ } catch {
170
+ return { migrated: 0 }
171
+ }
172
+ let migrated = 0
173
+ for (const name of names) {
174
+ if (!name.endsWith('.json') || name.startsWith('.')) continue
175
+ const file = path.join(TRANSCRIPTS_DIR, name)
176
+ let raw
177
+ try {
178
+ raw = JSON.parse(await readFile(file, 'utf8'))
179
+ } catch {
180
+ continue
181
+ }
182
+ if (raw?.schemaVersion === 2) continue
183
+ const normalized = normalizeTranscript(raw, name.replace(/\.json$/, ''))
184
+ if (!normalized) continue
185
+ const backup = `${file}.v1.bak`
186
+ try {
187
+ if (!fs.existsSync(backup)) await copyFile(file, backup)
188
+ } catch {
189
+ /* ignore */
190
+ }
191
+ try {
192
+ await saveTranscript(normalized)
193
+ migrated += 1
194
+ } catch {
195
+ /* ignore */
196
+ }
197
+ }
198
+ return { migrated }
199
+ }
200
+
201
+ export async function deleteTranscript(id) {
202
+ const job = getJob(jobId(id))
203
+ if (job && (job.status === 'queued' || job.status === 'running')) return false
204
+ try {
205
+ await rm(transcriptPath(id), { force: true })
206
+ } catch {
207
+ /* ignore */
208
+ }
209
+ await rm(path.join(WORK_DIR, id), { recursive: true, force: true }).catch(() => {})
210
+ removeJob(jobId(id))
211
+ return true
212
+ }
213
+
214
+ function pump() {
215
+ if (active || !queue.length) return
216
+ const item = queue.shift()
217
+ active = { key: jobId(item.episode.id), episodeId: item.episode.id }
218
+ runJob(item)
219
+ }
220
+
221
+ function waitChild(child) {
222
+ return new Promise((resolve, reject) => {
223
+ child.on('error', reject)
224
+ child.on('close', resolve)
225
+ })
226
+ }
227
+
228
+ function runProcess(command, args) {
229
+ return new Promise((resolve) => {
230
+ let child
231
+ try {
232
+ child = spawn(command, args, { stdio: 'ignore' })
233
+ } catch {
234
+ resolve(-1)
235
+ return
236
+ }
237
+ child.on('error', () => resolve(-1))
238
+ child.on('close', (code) => resolve(code))
239
+ })
240
+ }
241
+
242
+ async function probeDuration(file) {
243
+ return new Promise((resolve) => {
244
+ let child
245
+ try {
246
+ child = spawn(FFPROBE, ['-v', 'error', '-show_entries', 'format=duration', '-of', 'default=nw=1:nk=1', file], {
247
+ stdio: ['ignore', 'pipe', 'ignore'],
248
+ })
249
+ } catch {
250
+ resolve(0)
251
+ return
252
+ }
253
+ let out = ''
254
+ child.stdout.on('data', (chunk) => {
255
+ out += chunk.toString()
256
+ })
257
+ child.on('error', () => resolve(0))
258
+ child.on('close', () => resolve(Number(out.trim()) || 0))
259
+ })
260
+ }
261
+
262
+ // 用 ffmpeg 把长音频切成若干块;先尝试不重编码(-c copy),失败再转 AAC。
263
+ async function splitAudio(file, dir, chunkSeconds) {
264
+ await mkdir(dir, { recursive: true })
265
+ const sherpa = currentAsrModel().engine === 'sherpa'
266
+ const ext = sherpa ? 'wav' : 'm4a'
267
+ const pattern = path.join(dir, `chunk-%03d.${ext}`)
268
+ const base = ['-hide_banner', '-loglevel', 'error', '-y', '-i', file, '-f', 'segment', '-segment_time', String(chunkSeconds), '-reset_timestamps', '1']
269
+ let code
270
+ if (sherpa) {
271
+ code = await runProcess(FFMPEG, [...base, '-ar', '16000', '-ac', '1', '-c:a', 'pcm_s16le', pattern])
272
+ } else {
273
+ code = await runProcess(FFMPEG, [...base, '-c', 'copy', pattern])
274
+ if (code !== 0) code = await runProcess(FFMPEG, [...base, '-c:a', 'aac', '-b:a', '96k', pattern])
275
+ }
276
+ if (code !== 0) throw new Error('音频切分失败(ffmpeg)')
277
+ const names = (await readdir(dir)).filter((name) => new RegExp(`\\.${ext}$`, 'i').test(name)).sort()
278
+ if (!names.length) throw new Error('音频切分后没有生成文件')
279
+ const parts = []
280
+ let start = 0
281
+ for (const [index, name] of names.entries()) {
282
+ const full = path.join(dir, name)
283
+ const duration = (await probeDuration(full)) || chunkSeconds
284
+ parts.push({ index, start: round2(start), end: round2(start + duration), file: full, duration: round2(duration) })
285
+ start += duration
286
+ }
287
+ return parts
288
+ }
289
+
290
+ // 生成或复用切块计划;计划存 .work/<id>/meta.json,块结果在 .work/<id>/results/<index>.json。
291
+ async function ensurePlan(episode, workDir) {
292
+ const { chunkMinutes } = transcribeConfig()
293
+ const chunkSeconds = Math.round(chunkMinutes * 60)
294
+ const duration = Number(episode.duration) || (await probeDuration(episode.file)) || 0
295
+ if (!duration) throw new Error('无法读取音频时长(ffprobe)')
296
+ const metaPath = path.join(workDir, 'meta.json')
297
+ let meta = null
298
+ try {
299
+ meta = JSON.parse(await readFile(metaPath, 'utf8'))
300
+ } catch {
301
+ meta = null
302
+ }
303
+ const canReuse =
304
+ meta &&
305
+ meta.chunkSeconds === chunkSeconds &&
306
+ Math.abs(Number(meta.duration) - duration) < 2 &&
307
+ Array.isArray(meta.parts) &&
308
+ meta.parts.length > 0 &&
309
+ meta.parts.every((part) => fs.existsSync(part.file))
310
+ if (canReuse) return meta
311
+
312
+ await rm(path.join(workDir, 'parts'), { recursive: true, force: true })
313
+ await rm(path.join(workDir, 'results'), { recursive: true, force: true })
314
+ const single = currentAsrModel().engine !== 'sherpa' && duration <= chunkSeconds * 1.2
315
+ const parts = single
316
+ ? [{ index: 0, start: 0, end: round2(duration), file: episode.file, duration: round2(duration) }]
317
+ : await splitAudio(episode.file, path.join(workDir, 'parts'), chunkSeconds)
318
+ const plan = { duration: round2(duration), chunkSeconds, chunked: !single, parts }
319
+ await writeFile(metaPath, JSON.stringify(plan, null, 2), 'utf8')
320
+ return plan
321
+ }
322
+
323
+ function resultPath(resultsDir, index) {
324
+ return path.join(resultsDir, `${String(index).padStart(3, '0')}.json`)
325
+ }
326
+
327
+ function hasResult(resultsDir, index) {
328
+ return fs.existsSync(resultPath(resultsDir, index))
329
+ }
330
+
331
+ function progressOf(plan, doneSeconds, currentSeconds) {
332
+ const total = Number(plan?.duration) || 0
333
+ const raw = total ? ((doneSeconds + Math.max(0, currentSeconds)) / total) * 100 : 0
334
+ const percent = Math.min(99.5, Math.round(raw * 10) / 10)
335
+ return { done: round2(doneSeconds), total: round2(total), percent, unit: 'seconds' }
336
+ }
337
+
338
+ function buildArgs(file, outDir) {
339
+ const { hotwords } = transcribeConfig()
340
+ const args = [
341
+ file,
342
+ '--model', currentAsrModel().dir,
343
+ '--output-dir', outDir,
344
+ '--output-format', 'json',
345
+ '--timestamps',
346
+ '--no-progress',
347
+ '--quiet',
348
+ ]
349
+ if (hotwords.length) args.push('--context', hotwords.join(' '))
350
+ return args
351
+ }
352
+
353
+ async function readPartOutput(dir) {
354
+ const names = (await readdir(dir)).filter((name) => name.toLowerCase().endsWith('.json'))
355
+ if (!names.length) throw new Error('转写没有产生 JSON 输出')
356
+ return JSON.parse(await readFile(path.join(dir, names[0]), 'utf8'))
357
+ }
358
+
359
+ function wordsOf(raw) {
360
+ const list = Array.isArray(raw.segments) ? raw.segments : Array.isArray(raw.tokens) ? raw.tokens : []
361
+ return list
362
+ .map((seg) => ({
363
+ start: Number(seg.start) || 0,
364
+ end: Number(seg.end) || 0,
365
+ text: String(seg.text || '').trim(),
366
+ }))
367
+ .filter((seg) => seg.text)
368
+ }
369
+
370
+
371
+ function buildBatchArgs(files, outDir) {
372
+ const model = currentAsrModel()
373
+ const { hotwords } = transcribeConfig()
374
+ if (model.engine === 'sherpa') {
375
+ const args = [SHERPA_SCRIPT, ...files, '--model-dir', model.dir, '--out-dir', outDir, '--threads', '4']
376
+ if (hotwords.length) args.push('--hotwords', hotwords.join(' '))
377
+ return args
378
+ }
379
+ const args = [
380
+ ...files,
381
+ '--model', currentAsrModel().dir,
382
+ '--output-dir', outDir,
383
+ '--output-format', 'json',
384
+ '--timestamps',
385
+ '--no-progress',
386
+ '--quiet',
387
+ ]
388
+ if (hotwords.length) args.push('--context', hotwords.join(' '))
389
+ return args
390
+ }
391
+
392
+ function countResults(resultsDir, plan) {
393
+ let count = 0
394
+ let seconds = 0
395
+ let partial = 0
396
+ for (const part of plan.parts) {
397
+ if (!hasResult(resultsDir, part.index)) {
398
+ if (part.index === -1) partial += 1
399
+ continue
400
+ }
401
+ count += 1
402
+ seconds += part.duration
403
+ }
404
+ return { count, seconds }
405
+ }
406
+
407
+ function mergedFromResults(plan, resultsDir) {
408
+ const lines = []
409
+ let language = ''
410
+ const texts = []
411
+ for (const part of [...plan.parts].sort((a, b) => a.index - b.index)) {
412
+ if (!hasResult(resultsDir, part.index)) continue
413
+ let data
414
+ try {
415
+ data = JSON.parse(fs.readFileSync(resultPath(resultsDir, part.index), 'utf8'))
416
+ } catch {
417
+ continue
418
+ }
419
+ if (!language && data.language) language = data.language
420
+ if (data.text) texts.push(data.text)
421
+ for (const line of data.lines || []) {
422
+ const text = String(line.text || '').trim()
423
+ if (!text) continue
424
+ lines.push({
425
+ id: lines.length + 1,
426
+ start: round2(Number(line.start) + part.start),
427
+ end: round2(Number(line.end) + part.start),
428
+ text,
429
+ speaker: null,
430
+ translations: {},
431
+ })
432
+ }
433
+ }
434
+ return { lines, language, texts }
435
+ }
436
+
437
+ function buildPreview(episode, plan, merged) {
438
+ return {
439
+ schemaVersion: 2,
440
+ id: episode.id,
441
+ title: episode.title || '',
442
+ createdAt: Date.now(),
443
+ updatedAt: Date.now(),
444
+ model: currentAsrModel().name,
445
+ language: merged.language,
446
+ duration: plan.duration,
447
+ text: merged.texts.join(' '),
448
+ source: { path: episode.file },
449
+ lines: merged.lines,
450
+ providers: {},
451
+ speakers: [],
452
+ partial: true,
453
+ }
454
+ }
455
+
456
+ async function runJob(item) {
457
+ const { episode, force } = item
458
+ const key = jobId(episode.id)
459
+ const work = path.join(WORK_DIR, episode.id)
460
+ const target = transcriptPath(episode.id)
461
+ const backup = `${target}.previous`
462
+ let plan = null
463
+ let backedUp = false
464
+ let partialWritten = false
465
+ try {
466
+ upsertJob({
467
+ key,
468
+ type: 'transcribe',
469
+ episodeId: episode.id,
470
+ episodeTitle: episode.title || '',
471
+ status: 'running',
472
+ stage: 'preparing',
473
+ startedAt: Date.now(),
474
+ finishedAt: null,
475
+ error: null,
476
+ progress: { done: 0, total: 0, percent: 0, unit: 'seconds' },
477
+ chunks: { total: 0, done: 0, current: null },
478
+ })
479
+ if (force) await rm(work, { recursive: true, force: true })
480
+ await mkdir(work, { recursive: true })
481
+ plan = await ensurePlan(episode, work)
482
+ const resultsDir = path.join(work, 'results')
483
+ const cliDir = path.join(work, 'cli')
484
+ await mkdir(resultsDir, { recursive: true })
485
+ await mkdir(cliDir, { recursive: true })
486
+
487
+ // 重转时先备份旧字幕,失败/取消可以还原,不让半成品覆盖已有结果。
488
+ if (fs.existsSync(target)) {
489
+ await copyFile(target, backup).catch(() => {})
490
+ backedUp = fs.existsSync(backup)
491
+ }
492
+
493
+ // CLI 一次处理所有缺失的块:只加载一次模型;每完成一块就落一个 JSON。
494
+ const ingest = async () => {
495
+ let fresh = 0
496
+ for (const part of plan.parts) {
497
+ if (hasResult(resultsDir, part.index)) continue
498
+ const base = path.basename(part.file).replace(/\.[^.]+$/, '')
499
+ let raw
500
+ try {
501
+ raw = JSON.parse(await readFile(path.join(cliDir, `${base}.json`), 'utf8'))
502
+ } catch {
503
+ continue
504
+ }
505
+ const lines = buildSegments(raw, wordsOf(raw))
506
+ await writeFile(
507
+ resultPath(resultsDir, part.index),
508
+ JSON.stringify({ index: part.index, start: part.start, lines, language: raw.language || '', text: raw.text || '' }),
509
+ 'utf8',
510
+ )
511
+ fresh += 1
512
+ }
513
+ return fresh
514
+ }
515
+
516
+ await ingest()
517
+ const missing = plan.parts.filter((part) => !hasResult(resultsDir, part.index))
518
+ let child = null
519
+ let stderrTail = ''
520
+ let lastIngestAt = Date.now()
521
+ if (missing.length) {
522
+ const model = currentAsrModel()
523
+ const command = model.engine === 'sherpa' ? venvPython() : CLI
524
+ child = spawn(command, buildBatchArgs(missing.map((part) => part.file), cliDir), {
525
+ env: { ...process.env, HF_HUB_OFFLINE: '1', TOKENIZERS_PARALLELISM: 'false' },
526
+ stdio: ['ignore', 'ignore', 'pipe'],
527
+ })
528
+ activeChild = child
529
+ child.stderr.on('data', (chunk) => {
530
+ stderrTail = (stderrTail + chunk.toString()).slice(-4000)
531
+ })
532
+ }
533
+
534
+ let lastLineCount = 0
535
+ const tick = async () => {
536
+ const fresh = await ingest()
537
+ const counts = countResults(resultsDir, plan)
538
+ if (fresh > 0) {
539
+ const merged = mergedFromResults(plan, resultsDir)
540
+ if (merged.lines.length) {
541
+ await saveTranscript(buildPreview(episode, plan, merged)).catch(() => {})
542
+ partialWritten = true
543
+ lastLineCount = merged.lines.length
544
+ }
545
+ lastIngestAt = Date.now()
546
+ }
547
+ const current = plan.parts.find((part) => !hasResult(resultsDir, part.index)) || null
548
+ const eta = current ? Math.min(current.duration * 0.96, Math.max(0, (Date.now() - lastIngestAt) / 1000) * SPEED) : 0
549
+ upsertJob({
550
+ key,
551
+ stage: 'transcribing',
552
+ chunks: { total: plan.parts.length, done: counts.count, current: current ? current.index + 1 : null },
553
+ progress: progressOf(plan, counts.seconds, eta),
554
+ lineCount: lastLineCount,
555
+ })
556
+ if (cancelRequested.has(key) && child) {
557
+ try {
558
+ child.kill('SIGTERM')
559
+ } catch {
560
+ /* ignore */
561
+ }
562
+ }
563
+ }
564
+
565
+ const ticker = setInterval(() => {
566
+ tick().catch(() => {})
567
+ }, 1000)
568
+ let code = 0
569
+ if (child) code = await waitChild(child)
570
+ clearInterval(ticker)
571
+ activeChild = null
572
+ await ingest()
573
+ const counts = countResults(resultsDir, plan)
574
+
575
+ if (cancelRequested.has(key)) {
576
+ cancelRequested.delete(key)
577
+ if (backedUp) await copyFile(backup, target).catch(() => {})
578
+ else if (partialWritten) await rm(target, { force: true }).catch(() => {})
579
+ upsertJob({ key, status: 'cancelled', stage: 'cancelled', finishedAt: Date.now(), error: '用户取消了转写' })
580
+ return
581
+ }
582
+ if (code !== 0) {
583
+ const detail = stderrTail.trim().split('\n').slice(-6).join('\n')
584
+ throw new Error(`转写进程退出码 ${code}${detail ? `\n${detail}` : ''}`)
585
+ }
586
+ if (counts.count < plan.parts.length) throw new Error('部分音频块没有转写结果')
587
+
588
+ upsertJob({ key, stage: 'merging' })
589
+ const merged = mergedFromResults(plan, resultsDir)
590
+ let speakers = []
591
+ if (transcribeConfig().diarize && diarizeInfo().modelsReady) {
592
+ const prevPercent = getJob(key)?.progress?.percent || 0
593
+ upsertJob({ key, stage: 'diarizing', progress: { done: 0, total: 0, percent: Math.max(prevPercent, 96), unit: 'seconds' } })
594
+ try {
595
+ const result = await runDiarization(episode.file)
596
+ speakers = assignSpeakers(merged.lines, result.segments || [])
597
+ } catch {
598
+ speakers = []
599
+ }
600
+ }
601
+
602
+ let source = null
603
+ try {
604
+ const info = fs.statSync(episode.file)
605
+ source = { path: episode.file, size: info.size, mtime: info.mtimeMs }
606
+ } catch {
607
+ source = { path: episode.file }
608
+ }
609
+
610
+ const transcript = {
611
+ schemaVersion: 2,
612
+ id: episode.id,
613
+ title: episode.title,
614
+ createdAt: Date.now(),
615
+ updatedAt: Date.now(),
616
+ model: currentAsrModel().name,
617
+ language: merged.language,
618
+ duration: plan.duration || episode.duration || merged.lines[merged.lines.length - 1]?.end || 0,
619
+ text: merged.texts.join(' '),
620
+ source,
621
+ lines: merged.lines,
622
+ providers: {},
623
+ speakers,
624
+ }
625
+ await saveTranscript(transcript)
626
+ await rm(backup, { force: true }).catch(() => {})
627
+ upsertJob({
628
+ key,
629
+ status: 'done',
630
+ stage: 'done',
631
+ finishedAt: Date.now(),
632
+ lineCount: merged.lines.length,
633
+ language: merged.language,
634
+ speakers: speakers.length,
635
+ error: null,
636
+ progress: { ...progressOf(plan, plan.duration, 0), percent: 100, done: plan.duration, total: plan.duration },
637
+ chunks: { total: plan.parts.length, done: plan.parts.length, current: null },
638
+ })
639
+ await rm(work, { recursive: true, force: true })
640
+ } catch (error) {
641
+ if (backedUp) await copyFile(backup, target).catch(() => {})
642
+ else if (partialWritten) await rm(target, { force: true }).catch(() => {})
643
+ if (cancelRequested.has(key)) {
644
+ cancelRequested.delete(key)
645
+ upsertJob({ key, status: 'cancelled', stage: 'cancelled', finishedAt: Date.now(), error: '用户取消了转写' })
646
+ } else {
647
+ upsertJob({
648
+ key,
649
+ status: 'error',
650
+ stage: 'error',
651
+ finishedAt: Date.now(),
652
+ error: error.message || String(error),
653
+ })
654
+ }
655
+ } finally {
656
+ cancelRequested.delete(key)
657
+ active = null
658
+ activeChild = null
659
+ flushJobs()
660
+ pump()
661
+ }
662
+ }
663
+
664
+ const CJK_RE = /[\u3040-\u30ff\u3400-\u9fff\uac00-\ud7af]/
665
+
666
+ function joinTokens(tokens) {
667
+ let output = ''
668
+ for (const token of tokens) {
669
+ if (!output) {
670
+ output = token
671
+ continue
672
+ }
673
+ const previous = output.slice(-1)
674
+ const next = token.slice(0, 1)
675
+ output += CJK_RE.test(previous) && CJK_RE.test(next) ? token : ` ${token}`
676
+ }
677
+ return output
678
+ }
679
+
680
+ // 模型给出的是词级片段(中文甚至按字),这里拼成一行行适合做字幕的句子。
681
+ function groupWords(words) {
682
+ const lines = []
683
+ let current = null
684
+ const flush = () => {
685
+ if (!current) return
686
+ const text = joinTokens(current.tokens).trim()
687
+ if (text) lines.push({ start: current.start, end: current.end, text })
688
+ current = null
689
+ }
690
+ for (const word of words) {
691
+ const text = String(word.text || '').trim()
692
+ if (!text) continue
693
+ const start = Number(word.start) || 0
694
+ const end = Number(word.end) || start
695
+ if (!current) {
696
+ current = { start, end, tokens: [text] }
697
+ } else if (start - current.end > 1.2) {
698
+ flush()
699
+ current = { start, end, tokens: [text] }
700
+ } else {
701
+ current.end = end
702
+ current.tokens.push(text)
703
+ }
704
+ const joined = joinTokens(current.tokens)
705
+ const elapsed = current.end - current.start
706
+ if (
707
+ /[。!?!?…]$/.test(joined) ||
708
+ (/[,,;;::]$/.test(joined) && joined.length > 28) ||
709
+ joined.length >= 60 ||
710
+ elapsed >= 12
711
+ ) {
712
+ flush()
713
+ }
714
+ }
715
+ flush()
716
+ return lines.map((line, index) => ({
717
+ id: index + 1,
718
+ start: round2(line.start),
719
+ end: round2(line.end),
720
+ text: line.text,
721
+ }))
722
+ }
723
+
724
+ function normalizeWord(text) {
725
+ return String(text || '').toLowerCase().replace(/[^\p{L}\p{N}]/gu, '')
726
+ }
727
+
728
+ const CJK_CHAR = /[\u3040-\u30ff\u3400-\u9fff\uac00-\ud7af]/
729
+ const PUNCT_CHAR = /[。!?!?…,,;;::、.]/
730
+
731
+ function tokenizeChunk(text) {
732
+ const tokens = []
733
+ let buffer = ''
734
+ const push = (type, value) => tokens.push({ type, text: value })
735
+ for (const char of String(text || '')) {
736
+ if (CJK_CHAR.test(char)) {
737
+ if (buffer) {
738
+ push('word', buffer)
739
+ buffer = ''
740
+ }
741
+ push('word', char)
742
+ } else if (/\s/.test(char)) {
743
+ if (buffer) {
744
+ push('word', buffer)
745
+ buffer = ''
746
+ }
747
+ } else if (PUNCT_CHAR.test(char)) {
748
+ if (buffer) {
749
+ push('word', buffer)
750
+ buffer = ''
751
+ }
752
+ push('punct', char)
753
+ } else {
754
+ buffer += char
755
+ }
756
+ }
757
+ if (buffer) push('word', buffer)
758
+ return tokens
759
+ }
760
+
761
+ // 模型给出的词级片段没有标点;chunk 的 text 有标点。两者对齐后就能得到带标点的字幕行。
762
+ function buildSegments(raw, words) {
763
+ const chunks = Array.isArray(raw.chunks) ? raw.chunks.filter((chunk) => chunk && chunk.text) : []
764
+ if (!chunks.length) return groupWords(words)
765
+
766
+ const lines = []
767
+ let current = null
768
+ const flush = () => {
769
+ if (!current) return
770
+ const text = current.text.trim()
771
+ if (text) lines.push({ start: current.start, end: current.end, text })
772
+ current = null
773
+ }
774
+ const appendWord = (text, start, end) => {
775
+ if (!current) {
776
+ current = { start, end, text }
777
+ return
778
+ }
779
+ if (start - current.end > 1.2) {
780
+ flush()
781
+ current = { start, end, text }
782
+ return
783
+ }
784
+ const previous = current.text.slice(-1)
785
+ const next = text.slice(0, 1)
786
+ current.text += CJK_CHAR.test(previous) && CJK_CHAR.test(next) ? text : ` ${text}`
787
+ current.end = end
788
+ }
789
+
790
+ for (const chunk of chunks) {
791
+ const chunkStart = Number(chunk.start) || 0
792
+ const chunkEnd = Number(chunk.end) || chunkStart
793
+ const chunkWords = words.filter((word) => word.start >= chunkStart - 0.06 && word.start < chunkEnd + 0.06)
794
+ let cursor = 0
795
+ for (const token of tokenizeChunk(chunk.text)) {
796
+ if (token.type === 'punct') {
797
+ if (!current) continue
798
+ current.text += token.text
799
+ if (/[。!?!?…]/.test(token.text)) flush()
800
+ continue
801
+ }
802
+ const target = normalizeWord(token.text)
803
+ let matched = null
804
+ for (let index = cursor; index < Math.min(chunkWords.length, cursor + 3); index += 1) {
805
+ const candidate = normalizeWord(chunkWords[index]?.text)
806
+ if (candidate === target || (target && candidate.startsWith(target))) {
807
+ matched = chunkWords[index]
808
+ cursor = index + 1
809
+ break
810
+ }
811
+ }
812
+ if (!matched && chunkWords[cursor]) {
813
+ matched = chunkWords[cursor]
814
+ cursor += 1
815
+ }
816
+ appendWord(token.text, matched ? matched.start : chunkStart, matched ? matched.end : chunkEnd)
817
+ if (current && (current.text.length >= 60 || current.end - current.start >= 12)) flush()
818
+ }
819
+ flush()
820
+ }
821
+ flush()
822
+
823
+ return lines.map((line, index) => ({
824
+ id: index + 1,
825
+ start: round2(line.start),
826
+ end: round2(line.end),
827
+ text: line.text.trim(),
828
+ }))
829
+ }
830
+
831
+ export async function startTranscription(episode, { force = false, resume = false } = {}) {
832
+ const info = runtimeInfo()
833
+ if (!info.installed) throw Object.assign(new Error('转写运行环境未安装(~/.podwave/runtime)'), { status: 503 })
834
+ if (!info.modelReady) throw Object.assign(new Error('转写模型未下载(~/.podwave/models)'), { status: 503 })
835
+
836
+ const key = jobId(episode.id)
837
+ const currentJob = getJob(key)
838
+ if (currentJob && (currentJob.status === 'queued' || currentJob.status === 'running')) {
839
+ return { status: currentJob.status }
840
+ }
841
+
842
+ const existing = await readTranscript(episode.id)
843
+ if (existing && !force && !resume) return { status: 'done', lines: existing.lines.length }
844
+
845
+ const resumeWork = Boolean(resume) && fs.existsSync(path.join(WORK_DIR, episode.id, 'results'))
846
+ upsertJob({
847
+ key,
848
+ type: 'transcribe',
849
+ episodeId: episode.id,
850
+ episodeTitle: episode.title || '',
851
+ status: 'queued',
852
+ stage: 'queued',
853
+ queuedAt: Date.now(),
854
+ startedAt: null,
855
+ finishedAt: null,
856
+ error: null,
857
+ progress: { done: 0, total: 0, percent: 0, unit: 'seconds' },
858
+ chunks: { total: 0, done: 0, current: null },
859
+ })
860
+ queue.push({ episode, force: Boolean(force) || !resumeWork, resume: resumeWork })
861
+ pump()
862
+ return { status: 'queued' }
863
+ }
864
+
865
+ export async function enqueueBatch(episodes) {
866
+ const result = { queued: 0, skipped: 0, errors: [] }
867
+ for (const episode of episodes) {
868
+ try {
869
+ const state = await startTranscription(episode, {})
870
+ if (state.status === 'queued') result.queued += 1
871
+ else result.skipped += 1
872
+ } catch (error) {
873
+ result.errors.push({ id: episode.id, error: error.message || String(error) })
874
+ }
875
+ }
876
+ return result
877
+ }
878
+
879
+ export function cancelTranscription(id) {
880
+ const key = jobId(id)
881
+ const job = getJob(key)
882
+ if (!job) return { status: 'none' }
883
+ if (job.status === 'done' || job.status === 'error' || job.status === 'cancelled' || job.status === 'interrupted') {
884
+ return { status: job.status }
885
+ }
886
+ const index = queue.findIndex((item) => item.episode.id === id)
887
+ if (index >= 0) {
888
+ queue.splice(index, 1)
889
+ cancelRequested.delete(key)
890
+ upsertJob({ key, status: 'cancelled', stage: 'cancelled', finishedAt: Date.now(), error: '用户取消了排队中的转写' })
891
+ flushJobs()
892
+ return { status: 'cancelled' }
893
+ }
894
+ cancelRequested.add(key)
895
+ if (active && active.key === key && activeChild) {
896
+ const child = activeChild
897
+ try {
898
+ child.kill('SIGTERM')
899
+ } catch {
900
+ /* ignore */
901
+ }
902
+ setTimeout(() => {
903
+ try {
904
+ child.kill('SIGKILL')
905
+ } catch {
906
+ /* ignore */
907
+ }
908
+ }, 5000)
909
+ upsertJob({ key, stage: 'cancelling' })
910
+ return { status: 'cancelling' }
911
+ }
912
+ return { status: job.status }
913
+ }
914
+
915
+ export async function getTranscriptionState(id) {
916
+ const job = getJob(jobId(id))
917
+ const transcript = await readTranscript(id)
918
+ const base = job ? job.startedAt || job.queuedAt || job.updatedAt || Date.now() : 0
919
+ const elapsedMs = job ? (job.finishedAt || Date.now()) - base : 0
920
+ if (job && (job.status === 'queued' || job.status === 'running')) {
921
+ const position = queue.findIndex((item) => item.episode.id === id)
922
+ return {
923
+ status: job.status,
924
+ stage: job.stage,
925
+ progress: job.progress || null,
926
+ chunks: job.chunks || null,
927
+ queued: position >= 0 ? position + 1 : 0,
928
+ elapsedMs,
929
+ transcript: transcript && transcript.partial ? transcript : null,
930
+ episodeTitle: job.episodeTitle || '',
931
+ }
932
+ }
933
+ // 旧字幕还在时优先显示旧字幕:一次失败的重转不应该把已有结果藏起来。
934
+ if (transcript) return { status: 'done', transcript, job: job || null, elapsedMs }
935
+ if (job && (job.status === 'interrupted' || job.status === 'error' || job.status === 'cancelled')) {
936
+ return {
937
+ status: 'error',
938
+ error: job.error || (job.status === 'cancelled' ? '已取消' : '任务已中断'),
939
+ stage: job.stage,
940
+ progress: job.progress || null,
941
+ chunks: job.chunks || null,
942
+ elapsedMs,
943
+ }
944
+ }
945
+ return { status: 'none' }
946
+ }
947
+
948
+ function srtTime(seconds) {
949
+ const total = Math.max(0, Number(seconds) || 0)
950
+ const hours = Math.floor(total / 3600)
951
+ const minutes = Math.floor((total % 3600) / 60)
952
+ const secs = Math.floor(total % 60)
953
+ const ms = Math.round((total - Math.floor(total)) * 1000)
954
+ return `${String(hours).padStart(2, '0')}:${String(minutes).padStart(2, '0')}:${String(secs).padStart(2, '0')},${String(ms).padStart(3, '0')}`
955
+ }
956
+
957
+ function joinText(left, right) {
958
+ if (!left) return right
959
+ if (!right) return left
960
+ return /[A-Za-z0-9]$/.test(left) && /^[A-Za-z0-9]/.test(right) ? `${left} ${right}` : `${left}${right}`
961
+ }
962
+
963
+ function renumberLines(lines) {
964
+ return lines.map((line, index) => ({ ...line, id: index + 1 }))
965
+ }
966
+
967
+ function refreshText(transcript) {
968
+ transcript.text = transcript.lines.map((line) => line.text).join(' ')
969
+ return transcript
970
+ }
971
+
972
+ // P2:字幕行编辑。改文本会作废该行旧译文(时间戳单独改不影响译文)。
973
+ export async function editTranscriptLine(id, lineId, patch = {}) {
974
+ const transcript = await readTranscript(id)
975
+ if (!transcript) return null
976
+ const line = transcript.lines.find((item) => String(item.id) === String(lineId))
977
+ if (!line) throw new Error('找不到这一行')
978
+ let changedText = false
979
+ if (typeof patch.text === 'string') {
980
+ const text = patch.text.replace(/\s+/g, ' ').trim()
981
+ if (!text) throw new Error('字幕文本不能为空')
982
+ if (text !== line.text) {
983
+ line.text = text
984
+ line.translations = {}
985
+ changedText = true
986
+ }
987
+ }
988
+ if (Number.isFinite(Number(patch.start))) line.start = round2(Math.max(0, Number(patch.start)))
989
+ if (Number.isFinite(Number(patch.end))) line.end = round2(Math.max(line.start + 0.05, Number(patch.end)))
990
+ if (changedText && transcript.providers) {
991
+ // 译文整体失效时保留 provider 记录,方便再翻
992
+ }
993
+ transcript.lines = renumberLines(transcript.lines)
994
+ refreshText(transcript)
995
+ await saveTranscript(transcript)
996
+ return transcript
997
+ }
998
+
999
+ export async function splitTranscriptLine(id, lineId, offset) {
1000
+ const transcript = await readTranscript(id)
1001
+ if (!transcript) return null
1002
+ const index = transcript.lines.findIndex((item) => String(item.id) === String(lineId))
1003
+ if (index < 0) throw new Error('找不到这一行')
1004
+ const line = transcript.lines[index]
1005
+ const chars = Array.from(line.text)
1006
+ let at = Number.isFinite(Number(offset)) ? Math.round(Number(offset)) : Math.floor(chars.length / 2)
1007
+ at = Math.min(chars.length - 1, Math.max(1, at))
1008
+ const left = chars.slice(0, at).join('').trim()
1009
+ const right = chars.slice(at).join('').trim()
1010
+ if (!left || !right) throw new Error('拆分位置前后都需要有文字')
1011
+ const ratio = at / chars.length
1012
+ const splitAt = round2(line.start + (line.end - line.start) * ratio)
1013
+ const first = { ...line, text: left, end: splitAt, translations: {} }
1014
+ const second = { ...line, text: right, start: splitAt, translations: {} }
1015
+ transcript.lines.splice(index, 1, first, second)
1016
+ transcript.lines = renumberLines(transcript.lines)
1017
+ refreshText(transcript)
1018
+ await saveTranscript(transcript)
1019
+ return transcript
1020
+ }
1021
+
1022
+ export async function mergeTranscriptLine(id, lineId) {
1023
+ const transcript = await readTranscript(id)
1024
+ if (!transcript) return null
1025
+ const index = transcript.lines.findIndex((item) => String(item.id) === String(lineId))
1026
+ if (index < 0) throw new Error('找不到这一行')
1027
+ if (index >= transcript.lines.length - 1) throw new Error('已经是最后一行')
1028
+ const first = transcript.lines[index]
1029
+ const second = transcript.lines[index + 1]
1030
+ const translations = {}
1031
+ for (const target of new Set([...Object.keys(first.translations || {}), ...Object.keys(second.translations || {})])) {
1032
+ const left = first.translations?.[target]
1033
+ const right = second.translations?.[target]
1034
+ if (left && right) translations[target] = joinText(left, right)
1035
+ else if (left || right) translations[target] = left || right
1036
+ }
1037
+ const merged = {
1038
+ ...first,
1039
+ end: second.end,
1040
+ text: joinText(first.text, second.text),
1041
+ translations,
1042
+ speaker: first.speaker && first.speaker === second.speaker ? first.speaker : first.speaker || second.speaker || null,
1043
+ }
1044
+ transcript.lines.splice(index, 2, merged)
1045
+ transcript.lines = renumberLines(transcript.lines)
1046
+ refreshText(transcript)
1047
+ await saveTranscript(transcript)
1048
+ return transcript
1049
+ }
1050
+
1051
+ export async function validateTranscript(id) {
1052
+ const transcript = await readTranscript(id)
1053
+ if (!transcript) return { ok: false, issues: ['还没有转写'] }
1054
+ const issues = []
1055
+ transcript.lines.forEach((line, index) => {
1056
+ if (!String(line.text || '').trim()) issues.push(`第 ${index + 1} 行没有文字`)
1057
+ if (!(line.end > line.start)) issues.push(`第 ${index + 1} 行时间戳无效`)
1058
+ if (index > 0 && line.start < transcript.lines[index - 1].end - 0.05) {
1059
+ issues.push(`第 ${index} 行与第 ${index + 1} 行时间重叠`)
1060
+ }
1061
+ })
1062
+ return { ok: issues.length === 0, issues }
1063
+ }
1064
+
1065
+ export function toSRT(transcript) {
1066
+ return transcript.lines
1067
+ .map((line, index) => `${index + 1}\n${srtTime(line.start)} --> ${srtTime(line.end)}\n${line.text}\n`)
1068
+ .join('\n')
1069
+ }
1070
+
1071
+ export function toMarkdown(transcript) {
1072
+ const lines = [`# ${transcript.title || transcript.id}`, '', `> 模型 ${transcript.model}${transcript.language ? ` · 语言 ${transcript.language}` : ''}`, '']
1073
+ for (const line of transcript.lines) {
1074
+ const minutes = Math.floor(line.start / 60)
1075
+ const seconds = Math.floor(line.start % 60)
1076
+ lines.push(`**${minutes}:${String(seconds).padStart(2, '0')}** ${line.text}`)
1077
+ }
1078
+ return `${lines.join('\n')}\n`
1079
+ }
1080
+
1081
+ export async function diarizeTranscript(episode) {
1082
+ const transcript = await readTranscript(episode.id)
1083
+ if (!transcript) throw new Error('还没有转写,先转写再识别说话人')
1084
+ const info = diarizeInfo()
1085
+ if (!info.installed || !info.modelsReady) throw new Error('说话人分离模型还没准备好')
1086
+ const key = jobKey({ type: 'diarize', episodeId: episode.id })
1087
+ upsertJob({
1088
+ key,
1089
+ type: 'diarize',
1090
+ episodeId: episode.id,
1091
+ episodeTitle: episode.title || '',
1092
+ status: 'running',
1093
+ stage: 'diarizing',
1094
+ startedAt: Date.now(),
1095
+ finishedAt: null,
1096
+ error: null,
1097
+ })
1098
+ try {
1099
+ const result = await runDiarization(episode.file)
1100
+ const speakers = assignSpeakers(transcript.lines, result.segments || [])
1101
+ transcript.speakers = speakers
1102
+ transcript.updatedAt = Date.now()
1103
+ await saveTranscript(transcript)
1104
+ upsertJob({ key, status: 'done', stage: 'done', finishedAt: Date.now(), speakers: speakers.length, error: null })
1105
+ return transcript
1106
+ } catch (error) {
1107
+ upsertJob({ key, status: 'error', stage: 'error', finishedAt: Date.now(), error: error.message || String(error) })
1108
+ throw error
1109
+ }
1110
+ }
1111
+
1112
+ export function stopAll() {
1113
+ if (activeChild) {
1114
+ try {
1115
+ activeChild.kill('SIGTERM')
1116
+ } catch {
1117
+ /* ignore */
1118
+ }
1119
+ }
1120
+ cancelRequested.clear()
1121
+ queue.length = 0
1122
+ flushJobs()
1123
+ }