dsh-podwave 0.11.27 → 0.11.29

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/src/transcribe.js CHANGED
@@ -7,12 +7,16 @@ import fs from 'node:fs'
7
7
  import path from 'node:path'
8
8
  import { copyFile, mkdir, readFile, readdir, rename, rm, writeFile } from 'node:fs/promises'
9
9
  import { fileURLToPath } from 'node:url'
10
- import { DATA_DIR } from './state.js'
10
+ import { DATA_DIR, getState } from './state.js'
11
11
  import { loadModelConfig, modelDir } from './models.js'
12
12
  import { ffmpegPath, ffprobePath, killTree, venvBin, venvPython } from './platform.js'
13
13
  import { getSection } from './config.js'
14
+ import { API_CHUNK_SECONDS, REALTIME_CHUNK_SECONDS, asrApiConfig, asrProvider, buildApiLines, transcribeChunk } from './asr-api.js'
14
15
  import { artifactByDir } from './catalog.js'
15
16
  import { assignSpeakers, diarizeInfo, runDiarization } from './diarize.js'
17
+ import { resegmentLines } from './segment.js'
18
+ import { closeLiveSpeakers, createLiveSpeakers } from './live-speaker.js'
19
+ import { loadConfig as loadTranslateConfig, needsTranslation, translateWithApi } from './translate.js'
16
20
  import { flushJobs, getJob, getJobFresh, jobKey, removeJob, upsertJob } from './jobs.js'
17
21
 
18
22
  export const TRANSCRIPTS_DIR = path.join(DATA_DIR, 'transcripts')
@@ -67,24 +71,34 @@ export function transcribeConfig() {
67
71
  const hotwords = Array.isArray(section.hotwords) ? section.hotwords.map((word) => String(word || '').trim()).filter(Boolean) : []
68
72
  const raw = Number(section.chunkMinutes)
69
73
  const chunkMinutes = Math.max(1, Math.min(60, Number.isFinite(raw) && raw > 0 ? raw : 15))
70
- return { hotwords, chunkMinutes, diarize: section.diarize !== false }
74
+ return { provider: asrProvider(), hotwords, chunkMinutes, diarize: section.diarize !== false }
75
+ }
76
+
77
+ // 云端接口只吃 16 位 PCM WAV;本地 sherpa 也吃 wav,MLX 吃 m4a。
78
+ function wantWav() {
79
+ return asrProvider() === 'api' || currentAsrModel().engine === 'sherpa'
71
80
  }
72
81
 
73
82
  export function runtimeInfo() {
74
83
  const model = currentAsrModel()
75
84
  const config = transcribeConfig()
85
+ const api = asrApiConfig()
76
86
  const sherpa = model.engine === 'sherpa'
77
87
  // sherpa 走 venv 里的 python 跑脚本;MLX 走 venv 里的命令行工具。
78
88
  // 两者都必须在 Windows 的 Scripts\ 下找,否则 installed 永远 false。
79
89
  const cliPath = sherpa ? venvPython() : CLI
80
90
  const installed = sherpa ? fs.existsSync(SHERPA_SCRIPT) && fs.existsSync(cliPath) : fs.existsSync(cliPath)
81
91
  return {
92
+ provider: config.provider,
93
+ // 当前这条路到底能不能开跑:云端看有没有 key,本地看运行时 + 模型。
94
+ ready: config.provider === 'api' ? api.hasKey : installed && model.ready,
82
95
  engine: model.engine,
83
96
  installed,
84
97
  modelReady: model.ready,
85
98
  model: model.dir,
86
99
  modelName: model.name,
87
100
  cli: cliPath,
101
+ api: { base: api.base, url: api.url, hasKey: api.hasKey },
88
102
  hotwords: config.hotwords,
89
103
  chunkMinutes: config.chunkMinutes,
90
104
  diarize: config.diarize,
@@ -205,19 +219,37 @@ async function claimOrphanTranscript(id) {
205
219
  if (!Array.isArray(raw?.lines) || !raw.lines.length) continue
206
220
  if (normalizeTitle(raw.title) !== title) continue
207
221
  if (Math.abs((Number(raw.duration) || 0) - duration) > 2) continue
208
- matches.push({ file, raw })
222
+ let mtimeMs = 0
223
+ try {
224
+ mtimeMs = fs.statSync(file).mtimeMs
225
+ } catch {
226
+ mtimeMs = 0
227
+ }
228
+ matches.push({ file, raw, mtimeMs })
209
229
  }
210
230
 
211
- // 只有唯一候选才认领,避免把另一个同名同长的单集字幕错挂过来。
212
- if (matches.length !== 1) {
213
- if (matches.length > 1) {
214
- console.warn(`[dsh-podwave] 字幕认领放弃:${title} 有 ${matches.length} 个候选字幕`)
215
- }
231
+ if (!matches.length) {
216
232
  claimMisses.set(id, Date.now())
217
233
  return null
218
234
  }
219
235
 
236
+ // 多个候选时按可信度挑一个,而不是直接放弃 —— 放弃会让用户看到「字幕没了」。
237
+ // 优先级:音频路径完全一致 > 文件大小一致 > 行数更多 > 更新的文件。
238
+ const score = (match) => {
239
+ const sourcePath = String(match.raw?.source?.path || '')
240
+ const sourceSize = Number(match.raw?.source?.size || 0)
241
+ let value = 0
242
+ if (episode?.file && sourcePath === episode.file) value += 1000
243
+ if (episode?.size && sourceSize && sourceSize === Number(episode.size)) value += 100
244
+ value += Math.min(99, (match.raw?.lines?.length || 0) / 100)
245
+ return value
246
+ }
247
+ matches.sort((a, b) => score(b) - score(a) || (b.mtimeMs || 0) - (a.mtimeMs || 0))
248
+
220
249
  const [{ file, raw }] = matches
250
+ if (matches.length > 1) {
251
+ console.warn(`[dsh-podwave] 字幕认领:${title} 有 ${matches.length} 个候选,按音频路径/大小取最可能的一个`)
252
+ }
221
253
  const claimed = normalizeTranscript(
222
254
  {
223
255
  ...raw,
@@ -241,6 +273,14 @@ async function claimOrphanTranscript(id) {
241
273
  return null
242
274
  }
243
275
  console.warn(`[dsh-podwave] 字幕认领:${path.basename(file)} → ${id}.json(${title})`)
276
+ // 旧的那份已经复制成新 id 了,留着只会在下次认领时变成第二个候选;挪进备份目录,不直接删。
277
+ const backupDir = path.join(TRANSCRIPTS_DIR, '.claimed')
278
+ try {
279
+ await mkdir(backupDir, { recursive: true })
280
+ await rename(file, path.join(backupDir, path.basename(file)))
281
+ } catch {
282
+ /* 挪不动就算了,不影响读取 */
283
+ }
244
284
  return claimed
245
285
  }
246
286
 
@@ -263,6 +303,57 @@ export async function readTranscript(id) {
263
303
  }
264
304
 
265
305
  // 启动时把目录里所有老 transcript 迁成 v2,并留一份 <id>.json.v1.bak。
306
+ // 分块缓存(.work/<id>/parts、results)在转写成功后会被清掉,
307
+ // 但取消/失败/老版本留下的会一直堆着 —— 几百兆就是这么来的。启动时扫一遍。
308
+ export async function sweepWorkDirs({ maxAgeMs = 7 * 24 * 60 * 60 * 1000 } = {}) {
309
+ let entries = []
310
+ try {
311
+ entries = await readdir(WORK_DIR, { withFileTypes: true })
312
+ } catch {
313
+ return { removed: 0 }
314
+ }
315
+ let removed = 0
316
+ for (const entry of entries) {
317
+ if (!entry.isDirectory()) continue
318
+ const dir = path.join(WORK_DIR, entry.name)
319
+ // 已经有完整字幕的目录,缓存没有保留价值,直接清。
320
+ let hasTranscript = false
321
+ try {
322
+ await fs.promises.access(transcriptPath(entry.name))
323
+ hasTranscript = true
324
+ } catch {
325
+ hasTranscript = false
326
+ }
327
+ let stale = false
328
+ if (!hasTranscript) {
329
+ try {
330
+ const stat = fs.statSync(dir)
331
+ const age = Date.now() - stat.mtimeMs
332
+ // 一个结果都没产出的目录,一天以上就是纯粹的垃圾(早就没得续);
333
+ // 有结果的留满 7 天,给「续转」留余地。
334
+ let resultCount = 0
335
+ try {
336
+ resultCount = (await readdir(path.join(dir, 'results'))).length
337
+ } catch {
338
+ resultCount = 0
339
+ }
340
+ stale = resultCount === 0 ? age > 24 * 60 * 60 * 1000 : age > maxAgeMs
341
+ } catch {
342
+ stale = false
343
+ }
344
+ }
345
+ if (!hasTranscript && !stale) continue
346
+ try {
347
+ await rm(dir, { recursive: true, force: true })
348
+ removed += 1
349
+ } catch {
350
+ /* 删不掉就下次再说 */
351
+ }
352
+ }
353
+ if (removed) console.log(`[dsh-podwave] 清理了 ${removed} 个过期的分块缓存目录`)
354
+ return { removed }
355
+ }
356
+
266
357
  export async function migrateAllTranscripts() {
267
358
  let names = []
268
359
  try {
@@ -271,16 +362,39 @@ export async function migrateAllTranscripts() {
271
362
  return { migrated: 0 }
272
363
  }
273
364
  let migrated = 0
365
+ const claimed = new Set()
366
+ const raws = new Map()
274
367
  for (const name of names) {
275
368
  if (!name.endsWith('.json') || name.startsWith('.')) continue
276
- const file = path.join(TRANSCRIPTS_DIR, name)
277
- let raw
278
369
  try {
279
- raw = JSON.parse(await readFile(file, 'utf8'))
370
+ const parsed = JSON.parse(await readFile(path.join(TRANSCRIPTS_DIR, name), 'utf8'))
371
+ raws.set(name, parsed)
372
+ if (parsed?.claimedFrom) claimed.add(String(parsed.claimedFrom))
280
373
  } catch {
281
- continue
374
+ /* 坏文件交给下面的迁移分支忽略 */
375
+ }
376
+ }
377
+ // 旧逻辑认领完不删原件,同一集会留下「孤儿 + 认领后」两份,用户看到重复条目。
378
+ // 这里把已经被别人认领走的孤儿挪进 .claimed/ 留档(不删)。
379
+ for (const [name, parsed] of raws) {
380
+ const id = name.replace(/\.json$/, '')
381
+ if (!claimed.has(id)) continue
382
+ const backupDir = path.join(TRANSCRIPTS_DIR, '.claimed')
383
+ try {
384
+ await mkdir(backupDir, { recursive: true })
385
+ await rename(path.join(TRANSCRIPTS_DIR, name), path.join(backupDir, name))
386
+ migrated += 1
387
+ } catch {
388
+ /* ignore */
282
389
  }
283
- if (raw?.schemaVersion === 2) continue
390
+ raws.delete(name)
391
+ }
392
+
393
+ for (const name of raws.keys()) {
394
+ if (!name.endsWith('.json') || name.startsWith('.')) continue
395
+ const file = path.join(TRANSCRIPTS_DIR, name)
396
+ const raw = raws.get(name)
397
+ if (!raw || raw?.schemaVersion === 2) continue
284
398
  const normalized = normalizeTranscript(raw, name.replace(/\.json$/, ''))
285
399
  if (!normalized) continue
286
400
  const backup = `${file}.v1.bak`
@@ -316,7 +430,8 @@ function pump() {
316
430
  if (active || !queue.length) return
317
431
  const item = queue.shift()
318
432
  active = { key: jobId(item.episode.id), episodeId: item.episode.id }
319
- runJob(item)
433
+ if (item.provider === 'api') runApiJob(item)
434
+ else runJob(item)
320
435
  }
321
436
 
322
437
  function waitChild(child) {
@@ -403,7 +518,7 @@ async function detectSilenceMidpoints(file) {
403
518
  return mids
404
519
  }
405
520
 
406
- function planCuts(duration, chunkSeconds, silences) {
521
+ export function planCuts(duration, chunkSeconds, silences) {
407
522
  const cuts = [0]
408
523
  let target = chunkSeconds
409
524
  while (target < duration - 20) {
@@ -420,15 +535,15 @@ function planCuts(duration, chunkSeconds, silences) {
420
535
 
421
536
  async function splitAudioAt(file, dir, cuts, shouldCancel) {
422
537
  await mkdir(dir, { recursive: true })
423
- const sherpa = currentAsrModel().engine === 'sherpa'
538
+ const wav = wantWav()
424
539
  const parts = []
425
540
  for (let index = 0; index < cuts.length - 1; index += 1) {
426
541
  if (shouldCancel && shouldCancel()) throw new Error('用户取消了转写')
427
542
  const start = cuts[index]
428
543
  const end = cuts[index + 1]
429
- const ext = sherpa ? 'wav' : 'm4a'
544
+ const ext = wav ? 'wav' : 'm4a'
430
545
  const out = path.join(dir, `chunk-${String(index).padStart(3, '0')}.${ext}`)
431
- const code = sherpa
546
+ const code = wav
432
547
  ? await runProcess(FFMPEG, ['-hide_banner', '-loglevel', 'error', '-y', '-i', file, '-ss', String(start), '-to', String(end), '-ar', '16000', '-ac', '1', '-c:a', 'pcm_s16le', out])
433
548
  : await runProcess(FFMPEG, ['-hide_banner', '-loglevel', 'error', '-y', '-i', file, '-ss', String(start), '-to', String(end), '-vn', '-c:a', 'aac', '-b:a', '96k', out])
434
549
  if (code !== 0) throw new Error('音频切分失败(ffmpeg)')
@@ -440,12 +555,12 @@ async function splitAudioAt(file, dir, cuts, shouldCancel) {
440
555
 
441
556
  async function splitAudio(file, dir, chunkSeconds) {
442
557
  await mkdir(dir, { recursive: true })
443
- const sherpa = currentAsrModel().engine === 'sherpa'
444
- const ext = sherpa ? 'wav' : 'm4a'
558
+ const wav = wantWav()
559
+ const ext = wav ? 'wav' : 'm4a'
445
560
  const pattern = path.join(dir, `chunk-%03d.${ext}`)
446
561
  const base = ['-hide_banner', '-loglevel', 'error', '-y', '-i', file, '-f', 'segment', '-segment_time', String(chunkSeconds), '-reset_timestamps', '1']
447
562
  let code
448
- if (sherpa) {
563
+ if (wav) {
449
564
  code = await runProcess(FFMPEG, [...base, '-ar', '16000', '-ac', '1', '-c:a', 'pcm_s16le', pattern])
450
565
  } else {
451
566
  code = await runProcess(FFMPEG, [...base, '-c', 'copy', pattern])
@@ -466,9 +581,12 @@ async function splitAudio(file, dir, chunkSeconds) {
466
581
  }
467
582
 
468
583
  // 生成或复用切块计划;计划存 .work/<id>/meta.json,块结果在 .work/<id>/results/<index>.json。
469
- async function ensurePlan(episode, workDir, shouldCancel) {
584
+ async function ensurePlan(episode, workDir, shouldCancel, provider = asrProvider(), chunkSecondsOverride) {
585
+ const api = provider === 'api'
470
586
  const { chunkMinutes } = transcribeConfig()
471
- const chunkSeconds = Math.round(chunkMinutes * 60)
587
+ // 云端接口硬限制单次 120 秒:整段转写用 100 秒的大块,实时跟播用 12 秒的小块。
588
+ // 本地引擎沿用设置里的 chunkMinutes。
589
+ const chunkSeconds = chunkSecondsOverride || (api ? API_CHUNK_SECONDS : Math.round(chunkMinutes * 60))
472
590
  const duration = Number(episode.duration) || (await probeDuration(episode.file)) || 0
473
591
  if (!duration) throw new Error('无法读取音频时长(ffprobe)')
474
592
  const metaPath = path.join(workDir, 'meta.json')
@@ -490,7 +608,7 @@ async function ensurePlan(episode, workDir, shouldCancel) {
490
608
 
491
609
  await rm(path.join(workDir, 'parts'), { recursive: true, force: true })
492
610
  await rm(path.join(workDir, 'results'), { recursive: true, force: true })
493
- const single = currentAsrModel().engine !== 'sherpa' && duration <= chunkSeconds * 1.2
611
+ const single = !api && currentAsrModel().engine !== 'sherpa' && duration <= chunkSeconds * 1.2
494
612
  let parts
495
613
  if (single) {
496
614
  parts = [{ index: 0, start: 0, end: round2(duration), file: episode.file, duration: round2(duration) }]
@@ -591,8 +709,27 @@ function countResults(resultsDir, plan) {
591
709
  return { count, seconds }
592
710
  }
593
711
 
712
+ // 切点即使吸附到静音,块边界仍可能落在句子中间;云端接口还会给被截断的片段补个句号。
713
+ // 所以合并时把「上一块最后一行」和「下一块第一行」按时间连续性接回去。
714
+ const TERMINAL_END = /[。!?!?…][」』))】》”’"]?$/
715
+
716
+ export function stitchAcrossParts(prev, next) {
717
+ const gap = Number(next.start) - Number(prev.end)
718
+ if (!(gap >= -0.25 && gap <= 0.45)) return null
719
+ const left = String(prev.text || '')
720
+ const right = String(next.text || '')
721
+ if (!left || !right) return null
722
+ // 上一行没有句末标点,或者短得像半句(「明日。」),就认定它被切断了。
723
+ const suspicious = !TERMINAL_END.test(left) || left.replace(/\s/g, '').length <= 6
724
+ if (!suspicious) return null
725
+ const joiner = /[A-Za-z0-9]$/.test(left) && /^[A-Za-z0-9]/.test(right) ? ' ' : ''
726
+ const text = `${left}${joiner}${right}`
727
+ if (text.length > 120) return null
728
+ return { text, end: Number(next.end) }
729
+ }
730
+
594
731
  function mergedFromResults(plan, resultsDir) {
595
- const lines = []
732
+ const groups = []
596
733
  let language = ''
597
734
  const texts = []
598
735
  for (const part of [...plan.parts].sort((a, b) => a.index - b.index)) {
@@ -605,22 +742,77 @@ function mergedFromResults(plan, resultsDir) {
605
742
  }
606
743
  if (!language && data.language) language = data.language
607
744
  if (data.text) texts.push(data.text)
745
+ const group = []
608
746
  for (const line of data.lines || []) {
609
747
  const text = String(line.text || '').trim()
610
748
  if (!text) continue
611
- lines.push({
612
- id: lines.length + 1,
749
+ group.push({
613
750
  start: round2(Number(line.start) + part.start),
614
751
  end: round2(Number(line.end) + part.start),
615
752
  text,
616
- speaker: null,
617
- translations: {},
618
753
  })
619
754
  }
755
+ if (group.length) groups.push(group)
756
+ }
757
+
758
+ const joined = []
759
+ for (const group of groups) {
760
+ if (joined.length && group.length) {
761
+ const stitched = stitchAcrossParts(joined[joined.length - 1], group[0])
762
+ if (stitched) {
763
+ joined[joined.length - 1] = { ...joined[joined.length - 1], text: stitched.text, end: stitched.end }
764
+ group.shift()
765
+ }
766
+ }
767
+ joined.push(...group)
620
768
  }
769
+
770
+ // 识别出来的分段是按模型自己的声学块来的,本地引擎经常两三词一行、句尾还没标点。
771
+ // 先整理成整句再落盘:翻译按行送模型,碎片行会让小模型自己合并、译文整体错位。
772
+ const lines = resegmentLines(joined)
621
773
  return { lines, language, texts }
622
774
  }
623
775
 
776
+ // 实时跟播:每完成一块就把这一块的行立刻翻译出来,用户不用等整段转完。
777
+ const LIVE_TARGET = 'zh'
778
+
779
+ async function translateLiveLines(lines, cache) {
780
+ const config = loadTranslateConfig()
781
+ if (!config.apiKey) return 0
782
+ const batch = []
783
+ const seen = new Set()
784
+ for (const line of lines) {
785
+ const text = String(line.text || '').trim()
786
+ if (!text || cache.has(text) || seen.has(text)) continue
787
+ if (!needsTranslation(line, LIVE_TARGET)) continue
788
+ seen.add(text)
789
+ batch.push({ id: text, text })
790
+ }
791
+ if (!batch.length) return 0
792
+ const result = await translateWithApi(batch, LIVE_TARGET, config)
793
+ for (const [key, value] of result) cache.set(key, value)
794
+ return result.size
795
+ }
796
+
797
+ // 译文按「原文」缓存:断句重排会让行号变,但同一句原文的译文可以复用。
798
+ function applyLiveTranslations(lines, cache) {
799
+ if (!cache.size) return
800
+ for (const line of lines) {
801
+ const text = String(line.text || '').trim()
802
+ const hit = cache.get(text)
803
+ if (hit) line.translations = { ...(line.translations || {}), [LIVE_TARGET]: hit }
804
+ }
805
+ }
806
+
807
+ function playbackPosition(episodeId) {
808
+ try {
809
+ const position = Number(getState().progress?.[episodeId]?.position)
810
+ return Number.isFinite(position) && position > 0 ? position : 0
811
+ } catch {
812
+ return 0
813
+ }
814
+ }
815
+
624
816
  function buildPreview(episode, plan, merged) {
625
817
  return {
626
818
  schemaVersion: 2,
@@ -1044,10 +1236,271 @@ function buildSegments(raw, words) {
1044
1236
  }))
1045
1237
  }
1046
1238
 
1047
- export async function startTranscription(episode, { force = false, resume = false } = {}) {
1239
+ // 云端转写:把音频切成 30 秒小块并发打到 DeepSeek ASR,再把每块文本按字数摊进块内时间。
1240
+ // 和本地引擎共用 ensurePlan / mergedFromResults / 字幕落盘,差别只在「谁来识别」。
1241
+ // 并发 4:接口允许 50 并发,但一次任务打太多容易被同一账号的其它请求挤,4 条既快又不冒进。
1242
+ const API_CONCURRENCY = 4
1243
+
1244
+ function apiTranscript(episode, plan, merged, speakers, source) {
1245
+ return {
1246
+ schemaVersion: 2,
1247
+ id: episode.id,
1248
+ title: episode.title,
1249
+ createdAt: Date.now(),
1250
+ updatedAt: Date.now(),
1251
+ model: 'deepseek-asr',
1252
+ language: merged.language,
1253
+ duration: plan.duration || episode.duration || merged.lines[merged.lines.length - 1]?.end || 0,
1254
+ text: merged.texts.join(' '),
1255
+ source,
1256
+ lines: merged.lines,
1257
+ providers: {},
1258
+ speakers,
1259
+ }
1260
+ }
1261
+
1262
+ async function runApiJob(item) {
1263
+ const { episode, force } = item
1264
+ const realtime = Boolean(item.realtime)
1265
+ const key = jobId(episode.id)
1266
+ // 实时跟播和整段转写的块号不通用,缓存目录必须分开,否则续跑会读到别的切法。
1267
+ const work = path.join(WORK_DIR, realtime ? `${episode.id}-rt` : episode.id)
1268
+ const target = transcriptPath(episode.id)
1269
+ const backup = `${target}.previous`
1270
+ let plan = null
1271
+ let backedUp = false
1272
+ let partialWritten = false
1273
+ try {
1274
+ upsertJob({
1275
+ key,
1276
+ type: 'transcribe',
1277
+ episodeId: episode.id,
1278
+ episodeTitle: episode.title || '',
1279
+ status: 'running',
1280
+ stage: 'preparing',
1281
+ startedAt: Date.now(),
1282
+ finishedAt: null,
1283
+ error: null,
1284
+ progress: { done: 0, total: 0, percent: 0, unit: 'seconds' },
1285
+ chunks: { total: 0, done: 0, current: null },
1286
+ })
1287
+ if (force) await rm(work, { recursive: true, force: true })
1288
+ await mkdir(work, { recursive: true })
1289
+ plan = await ensurePlan(episode, work, () => cancelRequested.has(key), 'api', realtime ? REALTIME_CHUNK_SECONDS : undefined)
1290
+ const resultsDir = path.join(work, 'results')
1291
+ await mkdir(resultsDir, { recursive: true })
1292
+
1293
+ if (fs.existsSync(target)) {
1294
+ await copyFile(target, backup).catch(() => {})
1295
+ backedUp = fs.existsSync(backup)
1296
+ }
1297
+
1298
+ // 续跑:已经落地的块直接跳过,只补缺的。
1299
+ const pending = plan.parts.filter((part) => !hasResult(resultsDir, part.index))
1300
+ let doneCount = plan.parts.length - pending.length
1301
+ let doneSeconds = plan.parts
1302
+ .filter((part) => hasResult(resultsDir, part.index))
1303
+ .reduce((sum, part) => sum + part.duration, 0)
1304
+ let cursor = 0
1305
+ let lastPreview = doneCount
1306
+ // 实时模式:在线说话人 + 译文缓存,都随任务结束丢弃。
1307
+ const taken = new Array(pending.length).fill(false)
1308
+ const liveTranslations = new Map()
1309
+ const live = realtime ? await createLiveSpeakers().catch(() => null) : null
1310
+
1311
+ // 4 个 worker 会同时完成,预览必须串行落盘,否则两个 writeFile 交错会把 JSON 写坏。
1312
+ let publishChain = Promise.resolve()
1313
+ const publishLivePreview = () => {
1314
+ publishChain = publishChain.then(() => doPublishLivePreview()).catch(() => {})
1315
+ return publishChain
1316
+ }
1317
+
1318
+ const doPublishLivePreview = async () => {
1319
+ lastPreview = doneCount
1320
+ const preview = mergedFromResults(plan, resultsDir)
1321
+ if (!preview.lines.length) return
1322
+ if (live) await live.labelLines(preview.lines, plan.parts).catch(() => {})
1323
+ await translateLiveLines(preview.lines, liveTranslations).catch(() => {})
1324
+ applyLiveTranslations(preview.lines, liveTranslations)
1325
+ const built = buildPreview(episode, plan, preview)
1326
+ built.model = 'deepseek-asr'
1327
+ if (liveTranslations.size) built.providers = { [LIVE_TARGET]: 'api' }
1328
+ if (live) built.speakers = live.speakers()
1329
+ await saveTranscript(built).catch(() => {})
1330
+ partialWritten = true
1331
+ }
1332
+
1333
+ upsertJob({
1334
+ key,
1335
+ stage: 'transcribing',
1336
+ chunks: { total: plan.parts.length, done: doneCount, current: pending[0] ? pending[0].index + 1 : null },
1337
+ })
1338
+
1339
+ const takePart = () => {
1340
+ if (!realtime) {
1341
+ const part = pending[cursor]
1342
+ cursor += 1
1343
+ return part
1344
+ }
1345
+ // 实时:挑离播放头最近的一块,用户听到哪儿,字幕就跟到哪儿。
1346
+ const position = playbackPosition(episode.id)
1347
+ let bestIndex = -1
1348
+ let bestScore = Number.POSITIVE_INFINITY
1349
+ for (let index = 0; index < pending.length; index += 1) {
1350
+ if (taken[index]) continue
1351
+ const score = Math.abs(pending[index].start - position)
1352
+ if (score < bestScore) {
1353
+ bestScore = score
1354
+ bestIndex = index
1355
+ }
1356
+ }
1357
+ if (bestIndex < 0) return undefined
1358
+ taken[bestIndex] = true
1359
+ return pending[bestIndex]
1360
+ }
1361
+
1362
+ const worker = async () => {
1363
+ for (;;) {
1364
+ if (cancelRequested.has(key)) return
1365
+ const part = takePart()
1366
+ if (!part) return
1367
+ // 取消意图可能来自另一个模块实例(只写磁盘),所以这里轮询而不是只认内存标记。
1368
+ const controller = new AbortController()
1369
+ const watcher = setInterval(() => {
1370
+ if (cancelRequested.has(key)) controller.abort()
1371
+ }, 400)
1372
+ try {
1373
+ const { text } = await transcribeChunk(part.file, asrApiConfig(), { signal: controller.signal })
1374
+ // 存的是「块内相对时间」:mergedFromResults 会把 part.start 加回去。
1375
+ const lines = buildApiLines(text, 0, part.duration)
1376
+ await writeFile(
1377
+ resultPath(resultsDir, part.index),
1378
+ JSON.stringify({ index: part.index, start: part.start, lines, language: '', text }),
1379
+ 'utf8',
1380
+ )
1381
+ } finally {
1382
+ clearInterval(watcher)
1383
+ }
1384
+ doneCount += 1
1385
+ doneSeconds += part.duration
1386
+ upsertJob({
1387
+ key,
1388
+ stage: 'transcribing',
1389
+ chunks: { total: plan.parts.length, done: doneCount, current: part.index + 1 },
1390
+ progress: progressOf(plan, doneSeconds, 0),
1391
+ })
1392
+ // 实时:每块都发一次,字幕才跟得上;批量:每完成五分之一写一次预览。
1393
+ const step = Math.max(1, Math.round(plan.parts.length / 5))
1394
+ if (realtime) {
1395
+ await publishLivePreview().catch(() => {})
1396
+ } else if (doneCount - lastPreview >= step) {
1397
+ lastPreview = doneCount
1398
+ const preview = mergedFromResults(plan, resultsDir)
1399
+ if (preview.lines.length) {
1400
+ const built = buildPreview(episode, plan, preview)
1401
+ built.model = 'deepseek-asr'
1402
+ await saveTranscript(built).catch(() => {})
1403
+ partialWritten = true
1404
+ }
1405
+ }
1406
+ }
1407
+ }
1408
+
1409
+ await Promise.all(Array.from({ length: Math.min(API_CONCURRENCY, pending.length) }, () => worker()))
1410
+ if (cancelRequested.has(key)) throw new Error('用户取消了转写')
1411
+ const counts = countResults(resultsDir, plan)
1412
+ if (counts.count < plan.parts.length) throw new Error('部分音频块没有识别结果')
1413
+
1414
+ upsertJob({ key, stage: 'merging' })
1415
+ const merged = mergedFromResults(plan, resultsDir)
1416
+ let speakers = []
1417
+ // 实时期间攒下的译文要带进最终字幕;整段说话人分离不可用时,保留在线标签。
1418
+ if (realtime) {
1419
+ applyLiveTranslations(merged.lines, liveTranslations)
1420
+ if (live) speakers = live.speakers()
1421
+ }
1422
+ if (transcribeConfig().diarize && diarizeInfo().modelsReady) {
1423
+ const prevPercent = getJob(key)?.progress?.percent || 0
1424
+ upsertJob({ key, stage: 'diarizing', progress: { done: 0, total: 0, percent: Math.max(prevPercent, 96), unit: 'seconds' } })
1425
+ try {
1426
+ const result = await runDiarization(episode.file)
1427
+ speakers = assignSpeakers(merged.lines, result.segments || [])
1428
+ } catch {
1429
+ speakers = []
1430
+ }
1431
+ }
1432
+
1433
+ let source = null
1434
+ try {
1435
+ const info = fs.statSync(episode.file)
1436
+ source = { path: episode.file, size: info.size, mtime: info.mtimeMs }
1437
+ } catch {
1438
+ source = { path: episode.file }
1439
+ }
1440
+
1441
+ const final = apiTranscript(episode, plan, merged, speakers, source)
1442
+ if (realtime && liveTranslations.size) final.providers = { [LIVE_TARGET]: 'api' }
1443
+ await saveTranscript(final)
1444
+ await rm(backup, { force: true }).catch(() => {})
1445
+ upsertJob({
1446
+ key,
1447
+ status: 'done',
1448
+ stage: 'done',
1449
+ finishedAt: Date.now(),
1450
+ lineCount: merged.lines.length,
1451
+ language: merged.language,
1452
+ speakers: speakers.length,
1453
+ error: null,
1454
+ progress: { ...progressOf(plan, plan.duration, 0), percent: 100, done: plan.duration, total: plan.duration },
1455
+ chunks: { total: plan.parts.length, done: plan.parts.length, current: null },
1456
+ })
1457
+ await rm(work, { recursive: true, force: true })
1458
+ } catch (error) {
1459
+ if (backedUp) await copyFile(backup, target).catch(() => {})
1460
+ else if (partialWritten && !realtime) await rm(target, { force: true }).catch(() => {})
1461
+ const disk = getJobFresh(key)
1462
+ const cancelledOnDisk = disk?.status === 'cancelled' || disk?.stage === 'cancelling'
1463
+ if (cancelRequested.has(key) || cancelledOnDisk) {
1464
+ cancelRequested.delete(key)
1465
+ upsertJob({ key, status: 'cancelled', stage: 'cancelled', finishedAt: Date.now(), error: '用户取消了转写', pid: null })
1466
+ } else {
1467
+ upsertJob({
1468
+ key,
1469
+ status: 'error',
1470
+ stage: 'error',
1471
+ finishedAt: Date.now(),
1472
+ error: error.message || String(error),
1473
+ })
1474
+ }
1475
+ } finally {
1476
+ cancelRequested.delete(key)
1477
+ active = null
1478
+ activeChild = null
1479
+ // 声纹服务是常驻进程,任务结束就关掉,否则会一直挂在后台。
1480
+ if (realtime) closeLiveSpeakers()
1481
+ upsertJob({ key, pid: null, orphan: false })
1482
+ flushJobs()
1483
+ pump()
1484
+ }
1485
+ }
1486
+
1487
+ // realtime=true:边播边转(12 秒小块、按播放头挑块、每块立刻出字幕/译文/说话人)。
1488
+ // 只有云端 API 支持这种走法;本地引擎是整段 batch 的,忽略 realtime 走普通流程。
1489
+ export async function startTranscription(episode, { force = false, resume = false, realtime = false } = {}) {
1048
1490
  const info = runtimeInfo()
1049
- if (!info.installed) throw Object.assign(new Error('转写运行环境未安装(~/.podwave/runtime)'), { status: 503 })
1050
- if (!info.modelReady) throw Object.assign(new Error('转写模型未下载,请在设置里安装或检查模型目录'), { status: 503 })
1491
+ // 云端只要一把 key:不装 python 运行时、不下 2.4G 模型也能转写,这就是「装好就能用」。
1492
+ // 本地才检查运行时和模型,缺了就让用户去设置里一键装。
1493
+ if (info.provider === 'api') {
1494
+ if (!info.api.hasKey) {
1495
+ throw Object.assign(
1496
+ new Error('还没有填 DeepSeek API key。去「设置 → 播客 → 转写」填一个,或者切换成本地模型(免费但需要先下载模型)'),
1497
+ { status: 400 },
1498
+ )
1499
+ }
1500
+ } else {
1501
+ if (!info.installed) throw Object.assign(new Error('转写运行环境未安装(~/.podwave/runtime),在设置里点「一键安装本地模型」'), { status: 503 })
1502
+ if (!info.modelReady) throw Object.assign(new Error('转写模型未下载,请在设置里安装或检查模型目录'), { status: 503 })
1503
+ }
1051
1504
 
1052
1505
  const key = jobId(episode.id)
1053
1506
  const currentJob = getJob(key)
@@ -1055,10 +1508,15 @@ export async function startTranscription(episode, { force = false, resume = fals
1055
1508
  return { status: currentJob.status }
1056
1509
  }
1057
1510
 
1511
+ const useRealtime = Boolean(realtime) && info.provider === 'api'
1512
+ const workRoot = useRealtime ? path.join(WORK_DIR, `${episode.id}-rt`) : path.join(WORK_DIR, episode.id)
1513
+
1058
1514
  const existing = await readTranscript(episode.id)
1059
- if (existing && !force && !resume) return { status: 'done', lines: existing.lines.length }
1515
+ // 实时跟播可能只跑了一半(partial),下次播放要接着补,不能因为「已经有字幕」就当成完成。
1516
+ const halfDone = useRealtime && existing && existing.partial
1517
+ if (existing && !force && !resume && !halfDone) return { status: 'done', lines: existing.lines.length }
1060
1518
 
1061
- const resumeWork = Boolean(resume) && fs.existsSync(path.join(WORK_DIR, episode.id, 'results'))
1519
+ const resumeWork = Boolean(resume || (useRealtime && halfDone)) && fs.existsSync(path.join(workRoot, 'results'))
1062
1520
  upsertJob({
1063
1521
  key,
1064
1522
  type: 'transcribe',
@@ -1073,7 +1531,7 @@ export async function startTranscription(episode, { force = false, resume = fals
1073
1531
  progress: { done: 0, total: 0, percent: 0, unit: 'seconds' },
1074
1532
  chunks: { total: 0, done: 0, current: null },
1075
1533
  })
1076
- queue.push({ episode, force: Boolean(force) || !resumeWork, resume: resumeWork })
1534
+ queue.push({ episode, provider: info.provider, realtime: useRealtime, force: Boolean(force) || !resumeWork, resume: resumeWork })
1077
1535
  pump()
1078
1536
  return { status: 'queued' }
1079
1537
  }
@@ -1260,6 +1718,30 @@ export async function mergeTranscriptLine(id, lineId) {
1260
1718
  return transcript
1261
1719
  }
1262
1720
 
1721
+ export async function resegmentTranscript(id) {
1722
+ const transcript = await readTranscript(id)
1723
+ if (!transcript) return null
1724
+ const before = transcript.lines.length
1725
+ const lines = resegmentLines(transcript.lines)
1726
+ if (!lines.length) throw new Error('没有可以整理的字幕行')
1727
+ const hasTranslations = transcript.lines.some((line) => line.translations && Object.keys(line.translations).length)
1728
+ // 断句一改,逐行译文就对不上了:旧译文先留一份备份,不直接丢。
1729
+ if (hasTranslations) {
1730
+ transcript.legacyTranslations = transcript.lines.map((line) => ({
1731
+ id: line.id,
1732
+ start: line.start,
1733
+ end: line.end,
1734
+ text: line.text,
1735
+ translations: line.translations || {},
1736
+ }))
1737
+ }
1738
+ transcript.lines = lines
1739
+ transcript.providers = {}
1740
+ refreshText(transcript)
1741
+ await saveTranscript(transcript)
1742
+ return { transcript, before, after: lines.length, cleared: hasTranslations }
1743
+ }
1744
+
1263
1745
  export async function validateTranscript(id) {
1264
1746
  const transcript = await readTranscript(id)
1265
1747
  if (!transcript) return { ok: false, issues: ['还没有转写'] }