dsh-podwave 0.11.27 → 0.11.28

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
@@ -11,8 +11,10 @@ import { DATA_DIR } 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, 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'
16
18
  import { flushJobs, getJob, getJobFresh, jobKey, removeJob, upsertJob } from './jobs.js'
17
19
 
18
20
  export const TRANSCRIPTS_DIR = path.join(DATA_DIR, 'transcripts')
@@ -67,24 +69,34 @@ export function transcribeConfig() {
67
69
  const hotwords = Array.isArray(section.hotwords) ? section.hotwords.map((word) => String(word || '').trim()).filter(Boolean) : []
68
70
  const raw = Number(section.chunkMinutes)
69
71
  const chunkMinutes = Math.max(1, Math.min(60, Number.isFinite(raw) && raw > 0 ? raw : 15))
70
- return { hotwords, chunkMinutes, diarize: section.diarize !== false }
72
+ return { provider: asrProvider(), hotwords, chunkMinutes, diarize: section.diarize !== false }
73
+ }
74
+
75
+ // 云端接口只吃 16 位 PCM WAV;本地 sherpa 也吃 wav,MLX 吃 m4a。
76
+ function wantWav() {
77
+ return asrProvider() === 'api' || currentAsrModel().engine === 'sherpa'
71
78
  }
72
79
 
73
80
  export function runtimeInfo() {
74
81
  const model = currentAsrModel()
75
82
  const config = transcribeConfig()
83
+ const api = asrApiConfig()
76
84
  const sherpa = model.engine === 'sherpa'
77
85
  // sherpa 走 venv 里的 python 跑脚本;MLX 走 venv 里的命令行工具。
78
86
  // 两者都必须在 Windows 的 Scripts\ 下找,否则 installed 永远 false。
79
87
  const cliPath = sherpa ? venvPython() : CLI
80
88
  const installed = sherpa ? fs.existsSync(SHERPA_SCRIPT) && fs.existsSync(cliPath) : fs.existsSync(cliPath)
81
89
  return {
90
+ provider: config.provider,
91
+ // 当前这条路到底能不能开跑:云端看有没有 key,本地看运行时 + 模型。
92
+ ready: config.provider === 'api' ? api.hasKey : installed && model.ready,
82
93
  engine: model.engine,
83
94
  installed,
84
95
  modelReady: model.ready,
85
96
  model: model.dir,
86
97
  modelName: model.name,
87
98
  cli: cliPath,
99
+ api: { base: api.base, url: api.url, hasKey: api.hasKey },
88
100
  hotwords: config.hotwords,
89
101
  chunkMinutes: config.chunkMinutes,
90
102
  diarize: config.diarize,
@@ -205,19 +217,37 @@ async function claimOrphanTranscript(id) {
205
217
  if (!Array.isArray(raw?.lines) || !raw.lines.length) continue
206
218
  if (normalizeTitle(raw.title) !== title) continue
207
219
  if (Math.abs((Number(raw.duration) || 0) - duration) > 2) continue
208
- matches.push({ file, raw })
220
+ let mtimeMs = 0
221
+ try {
222
+ mtimeMs = fs.statSync(file).mtimeMs
223
+ } catch {
224
+ mtimeMs = 0
225
+ }
226
+ matches.push({ file, raw, mtimeMs })
209
227
  }
210
228
 
211
- // 只有唯一候选才认领,避免把另一个同名同长的单集字幕错挂过来。
212
- if (matches.length !== 1) {
213
- if (matches.length > 1) {
214
- console.warn(`[dsh-podwave] 字幕认领放弃:${title} 有 ${matches.length} 个候选字幕`)
215
- }
229
+ if (!matches.length) {
216
230
  claimMisses.set(id, Date.now())
217
231
  return null
218
232
  }
219
233
 
234
+ // 多个候选时按可信度挑一个,而不是直接放弃 —— 放弃会让用户看到「字幕没了」。
235
+ // 优先级:音频路径完全一致 > 文件大小一致 > 行数更多 > 更新的文件。
236
+ const score = (match) => {
237
+ const sourcePath = String(match.raw?.source?.path || '')
238
+ const sourceSize = Number(match.raw?.source?.size || 0)
239
+ let value = 0
240
+ if (episode?.file && sourcePath === episode.file) value += 1000
241
+ if (episode?.size && sourceSize && sourceSize === Number(episode.size)) value += 100
242
+ value += Math.min(99, (match.raw?.lines?.length || 0) / 100)
243
+ return value
244
+ }
245
+ matches.sort((a, b) => score(b) - score(a) || (b.mtimeMs || 0) - (a.mtimeMs || 0))
246
+
220
247
  const [{ file, raw }] = matches
248
+ if (matches.length > 1) {
249
+ console.warn(`[dsh-podwave] 字幕认领:${title} 有 ${matches.length} 个候选,按音频路径/大小取最可能的一个`)
250
+ }
221
251
  const claimed = normalizeTranscript(
222
252
  {
223
253
  ...raw,
@@ -241,6 +271,14 @@ async function claimOrphanTranscript(id) {
241
271
  return null
242
272
  }
243
273
  console.warn(`[dsh-podwave] 字幕认领:${path.basename(file)} → ${id}.json(${title})`)
274
+ // 旧的那份已经复制成新 id 了,留着只会在下次认领时变成第二个候选;挪进备份目录,不直接删。
275
+ const backupDir = path.join(TRANSCRIPTS_DIR, '.claimed')
276
+ try {
277
+ await mkdir(backupDir, { recursive: true })
278
+ await rename(file, path.join(backupDir, path.basename(file)))
279
+ } catch {
280
+ /* 挪不动就算了,不影响读取 */
281
+ }
244
282
  return claimed
245
283
  }
246
284
 
@@ -263,6 +301,57 @@ export async function readTranscript(id) {
263
301
  }
264
302
 
265
303
  // 启动时把目录里所有老 transcript 迁成 v2,并留一份 <id>.json.v1.bak。
304
+ // 分块缓存(.work/<id>/parts、results)在转写成功后会被清掉,
305
+ // 但取消/失败/老版本留下的会一直堆着 —— 几百兆就是这么来的。启动时扫一遍。
306
+ export async function sweepWorkDirs({ maxAgeMs = 7 * 24 * 60 * 60 * 1000 } = {}) {
307
+ let entries = []
308
+ try {
309
+ entries = await readdir(WORK_DIR, { withFileTypes: true })
310
+ } catch {
311
+ return { removed: 0 }
312
+ }
313
+ let removed = 0
314
+ for (const entry of entries) {
315
+ if (!entry.isDirectory()) continue
316
+ const dir = path.join(WORK_DIR, entry.name)
317
+ // 已经有完整字幕的目录,缓存没有保留价值,直接清。
318
+ let hasTranscript = false
319
+ try {
320
+ await fs.promises.access(transcriptPath(entry.name))
321
+ hasTranscript = true
322
+ } catch {
323
+ hasTranscript = false
324
+ }
325
+ let stale = false
326
+ if (!hasTranscript) {
327
+ try {
328
+ const stat = fs.statSync(dir)
329
+ const age = Date.now() - stat.mtimeMs
330
+ // 一个结果都没产出的目录,一天以上就是纯粹的垃圾(早就没得续);
331
+ // 有结果的留满 7 天,给「续转」留余地。
332
+ let resultCount = 0
333
+ try {
334
+ resultCount = (await readdir(path.join(dir, 'results'))).length
335
+ } catch {
336
+ resultCount = 0
337
+ }
338
+ stale = resultCount === 0 ? age > 24 * 60 * 60 * 1000 : age > maxAgeMs
339
+ } catch {
340
+ stale = false
341
+ }
342
+ }
343
+ if (!hasTranscript && !stale) continue
344
+ try {
345
+ await rm(dir, { recursive: true, force: true })
346
+ removed += 1
347
+ } catch {
348
+ /* 删不掉就下次再说 */
349
+ }
350
+ }
351
+ if (removed) console.log(`[dsh-podwave] 清理了 ${removed} 个过期的分块缓存目录`)
352
+ return { removed }
353
+ }
354
+
266
355
  export async function migrateAllTranscripts() {
267
356
  let names = []
268
357
  try {
@@ -271,16 +360,39 @@ export async function migrateAllTranscripts() {
271
360
  return { migrated: 0 }
272
361
  }
273
362
  let migrated = 0
363
+ const claimed = new Set()
364
+ const raws = new Map()
274
365
  for (const name of names) {
275
366
  if (!name.endsWith('.json') || name.startsWith('.')) continue
276
- const file = path.join(TRANSCRIPTS_DIR, name)
277
- let raw
278
367
  try {
279
- raw = JSON.parse(await readFile(file, 'utf8'))
368
+ const parsed = JSON.parse(await readFile(path.join(TRANSCRIPTS_DIR, name), 'utf8'))
369
+ raws.set(name, parsed)
370
+ if (parsed?.claimedFrom) claimed.add(String(parsed.claimedFrom))
280
371
  } catch {
281
- continue
372
+ /* 坏文件交给下面的迁移分支忽略 */
282
373
  }
283
- if (raw?.schemaVersion === 2) continue
374
+ }
375
+ // 旧逻辑认领完不删原件,同一集会留下「孤儿 + 认领后」两份,用户看到重复条目。
376
+ // 这里把已经被别人认领走的孤儿挪进 .claimed/ 留档(不删)。
377
+ for (const [name, parsed] of raws) {
378
+ const id = name.replace(/\.json$/, '')
379
+ if (!claimed.has(id)) continue
380
+ const backupDir = path.join(TRANSCRIPTS_DIR, '.claimed')
381
+ try {
382
+ await mkdir(backupDir, { recursive: true })
383
+ await rename(path.join(TRANSCRIPTS_DIR, name), path.join(backupDir, name))
384
+ migrated += 1
385
+ } catch {
386
+ /* ignore */
387
+ }
388
+ raws.delete(name)
389
+ }
390
+
391
+ for (const name of raws.keys()) {
392
+ if (!name.endsWith('.json') || name.startsWith('.')) continue
393
+ const file = path.join(TRANSCRIPTS_DIR, name)
394
+ const raw = raws.get(name)
395
+ if (!raw || raw?.schemaVersion === 2) continue
284
396
  const normalized = normalizeTranscript(raw, name.replace(/\.json$/, ''))
285
397
  if (!normalized) continue
286
398
  const backup = `${file}.v1.bak`
@@ -316,7 +428,8 @@ function pump() {
316
428
  if (active || !queue.length) return
317
429
  const item = queue.shift()
318
430
  active = { key: jobId(item.episode.id), episodeId: item.episode.id }
319
- runJob(item)
431
+ if (item.provider === 'api') runApiJob(item)
432
+ else runJob(item)
320
433
  }
321
434
 
322
435
  function waitChild(child) {
@@ -403,7 +516,7 @@ async function detectSilenceMidpoints(file) {
403
516
  return mids
404
517
  }
405
518
 
406
- function planCuts(duration, chunkSeconds, silences) {
519
+ export function planCuts(duration, chunkSeconds, silences) {
407
520
  const cuts = [0]
408
521
  let target = chunkSeconds
409
522
  while (target < duration - 20) {
@@ -420,15 +533,15 @@ function planCuts(duration, chunkSeconds, silences) {
420
533
 
421
534
  async function splitAudioAt(file, dir, cuts, shouldCancel) {
422
535
  await mkdir(dir, { recursive: true })
423
- const sherpa = currentAsrModel().engine === 'sherpa'
536
+ const wav = wantWav()
424
537
  const parts = []
425
538
  for (let index = 0; index < cuts.length - 1; index += 1) {
426
539
  if (shouldCancel && shouldCancel()) throw new Error('用户取消了转写')
427
540
  const start = cuts[index]
428
541
  const end = cuts[index + 1]
429
- const ext = sherpa ? 'wav' : 'm4a'
542
+ const ext = wav ? 'wav' : 'm4a'
430
543
  const out = path.join(dir, `chunk-${String(index).padStart(3, '0')}.${ext}`)
431
- const code = sherpa
544
+ const code = wav
432
545
  ? 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
546
  : 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
547
  if (code !== 0) throw new Error('音频切分失败(ffmpeg)')
@@ -440,12 +553,12 @@ async function splitAudioAt(file, dir, cuts, shouldCancel) {
440
553
 
441
554
  async function splitAudio(file, dir, chunkSeconds) {
442
555
  await mkdir(dir, { recursive: true })
443
- const sherpa = currentAsrModel().engine === 'sherpa'
444
- const ext = sherpa ? 'wav' : 'm4a'
556
+ const wav = wantWav()
557
+ const ext = wav ? 'wav' : 'm4a'
445
558
  const pattern = path.join(dir, `chunk-%03d.${ext}`)
446
559
  const base = ['-hide_banner', '-loglevel', 'error', '-y', '-i', file, '-f', 'segment', '-segment_time', String(chunkSeconds), '-reset_timestamps', '1']
447
560
  let code
448
- if (sherpa) {
561
+ if (wav) {
449
562
  code = await runProcess(FFMPEG, [...base, '-ar', '16000', '-ac', '1', '-c:a', 'pcm_s16le', pattern])
450
563
  } else {
451
564
  code = await runProcess(FFMPEG, [...base, '-c', 'copy', pattern])
@@ -466,9 +579,11 @@ async function splitAudio(file, dir, chunkSeconds) {
466
579
  }
467
580
 
468
581
  // 生成或复用切块计划;计划存 .work/<id>/meta.json,块结果在 .work/<id>/results/<index>.json。
469
- async function ensurePlan(episode, workDir, shouldCancel) {
582
+ async function ensurePlan(episode, workDir, shouldCancel, provider = asrProvider()) {
583
+ const api = provider === 'api'
470
584
  const { chunkMinutes } = transcribeConfig()
471
- const chunkSeconds = Math.round(chunkMinutes * 60)
585
+ // 云端接口硬限制单次 120 秒,所以固定切 30 秒一块;本地引擎沿用设置里的 chunkMinutes。
586
+ const chunkSeconds = api ? API_CHUNK_SECONDS : Math.round(chunkMinutes * 60)
472
587
  const duration = Number(episode.duration) || (await probeDuration(episode.file)) || 0
473
588
  if (!duration) throw new Error('无法读取音频时长(ffprobe)')
474
589
  const metaPath = path.join(workDir, 'meta.json')
@@ -490,7 +605,7 @@ async function ensurePlan(episode, workDir, shouldCancel) {
490
605
 
491
606
  await rm(path.join(workDir, 'parts'), { recursive: true, force: true })
492
607
  await rm(path.join(workDir, 'results'), { recursive: true, force: true })
493
- const single = currentAsrModel().engine !== 'sherpa' && duration <= chunkSeconds * 1.2
608
+ const single = !api && currentAsrModel().engine !== 'sherpa' && duration <= chunkSeconds * 1.2
494
609
  let parts
495
610
  if (single) {
496
611
  parts = [{ index: 0, start: 0, end: round2(duration), file: episode.file, duration: round2(duration) }]
@@ -591,8 +706,27 @@ function countResults(resultsDir, plan) {
591
706
  return { count, seconds }
592
707
  }
593
708
 
709
+ // 切点即使吸附到静音,块边界仍可能落在句子中间;云端接口还会给被截断的片段补个句号。
710
+ // 所以合并时把「上一块最后一行」和「下一块第一行」按时间连续性接回去。
711
+ const TERMINAL_END = /[。!?!?…][」』))】》”’"]?$/
712
+
713
+ export function stitchAcrossParts(prev, next) {
714
+ const gap = Number(next.start) - Number(prev.end)
715
+ if (!(gap >= -0.25 && gap <= 0.45)) return null
716
+ const left = String(prev.text || '')
717
+ const right = String(next.text || '')
718
+ if (!left || !right) return null
719
+ // 上一行没有句末标点,或者短得像半句(「明日。」),就认定它被切断了。
720
+ const suspicious = !TERMINAL_END.test(left) || left.replace(/\s/g, '').length <= 6
721
+ if (!suspicious) return null
722
+ const joiner = /[A-Za-z0-9]$/.test(left) && /^[A-Za-z0-9]/.test(right) ? ' ' : ''
723
+ const text = `${left}${joiner}${right}`
724
+ if (text.length > 120) return null
725
+ return { text, end: Number(next.end) }
726
+ }
727
+
594
728
  function mergedFromResults(plan, resultsDir) {
595
- const lines = []
729
+ const groups = []
596
730
  let language = ''
597
731
  const texts = []
598
732
  for (const part of [...plan.parts].sort((a, b) => a.index - b.index)) {
@@ -605,19 +739,34 @@ function mergedFromResults(plan, resultsDir) {
605
739
  }
606
740
  if (!language && data.language) language = data.language
607
741
  if (data.text) texts.push(data.text)
742
+ const group = []
608
743
  for (const line of data.lines || []) {
609
744
  const text = String(line.text || '').trim()
610
745
  if (!text) continue
611
- lines.push({
612
- id: lines.length + 1,
746
+ group.push({
613
747
  start: round2(Number(line.start) + part.start),
614
748
  end: round2(Number(line.end) + part.start),
615
749
  text,
616
- speaker: null,
617
- translations: {},
618
750
  })
619
751
  }
752
+ if (group.length) groups.push(group)
753
+ }
754
+
755
+ const joined = []
756
+ for (const group of groups) {
757
+ if (joined.length && group.length) {
758
+ const stitched = stitchAcrossParts(joined[joined.length - 1], group[0])
759
+ if (stitched) {
760
+ joined[joined.length - 1] = { ...joined[joined.length - 1], text: stitched.text, end: stitched.end }
761
+ group.shift()
762
+ }
763
+ }
764
+ joined.push(...group)
620
765
  }
766
+
767
+ // 识别出来的分段是按模型自己的声学块来的,本地引擎经常两三词一行、句尾还没标点。
768
+ // 先整理成整句再落盘:翻译按行送模型,碎片行会让小模型自己合并、译文整体错位。
769
+ const lines = resegmentLines(joined)
621
770
  return { lines, language, texts }
622
771
  }
623
772
 
@@ -1044,10 +1193,209 @@ function buildSegments(raw, words) {
1044
1193
  }))
1045
1194
  }
1046
1195
 
1196
+ // 云端转写:把音频切成 30 秒小块并发打到 DeepSeek ASR,再把每块文本按字数摊进块内时间。
1197
+ // 和本地引擎共用 ensurePlan / mergedFromResults / 字幕落盘,差别只在「谁来识别」。
1198
+ // 并发 4:接口允许 50 并发,但一次任务打太多容易被同一账号的其它请求挤,4 条既快又不冒进。
1199
+ const API_CONCURRENCY = 4
1200
+
1201
+ function apiTranscript(episode, plan, merged, speakers, source) {
1202
+ return {
1203
+ schemaVersion: 2,
1204
+ id: episode.id,
1205
+ title: episode.title,
1206
+ createdAt: Date.now(),
1207
+ updatedAt: Date.now(),
1208
+ model: 'deepseek-asr',
1209
+ language: merged.language,
1210
+ duration: plan.duration || episode.duration || merged.lines[merged.lines.length - 1]?.end || 0,
1211
+ text: merged.texts.join(' '),
1212
+ source,
1213
+ lines: merged.lines,
1214
+ providers: {},
1215
+ speakers,
1216
+ }
1217
+ }
1218
+
1219
+ async function runApiJob(item) {
1220
+ const { episode, force } = item
1221
+ const key = jobId(episode.id)
1222
+ const work = path.join(WORK_DIR, episode.id)
1223
+ const target = transcriptPath(episode.id)
1224
+ const backup = `${target}.previous`
1225
+ let plan = null
1226
+ let backedUp = false
1227
+ let partialWritten = false
1228
+ try {
1229
+ upsertJob({
1230
+ key,
1231
+ type: 'transcribe',
1232
+ episodeId: episode.id,
1233
+ episodeTitle: episode.title || '',
1234
+ status: 'running',
1235
+ stage: 'preparing',
1236
+ startedAt: Date.now(),
1237
+ finishedAt: null,
1238
+ error: null,
1239
+ progress: { done: 0, total: 0, percent: 0, unit: 'seconds' },
1240
+ chunks: { total: 0, done: 0, current: null },
1241
+ })
1242
+ if (force) await rm(work, { recursive: true, force: true })
1243
+ await mkdir(work, { recursive: true })
1244
+ plan = await ensurePlan(episode, work, () => cancelRequested.has(key), 'api')
1245
+ const resultsDir = path.join(work, 'results')
1246
+ await mkdir(resultsDir, { recursive: true })
1247
+
1248
+ if (fs.existsSync(target)) {
1249
+ await copyFile(target, backup).catch(() => {})
1250
+ backedUp = fs.existsSync(backup)
1251
+ }
1252
+
1253
+ // 续跑:已经落地的块直接跳过,只补缺的。
1254
+ const pending = plan.parts.filter((part) => !hasResult(resultsDir, part.index))
1255
+ let doneCount = plan.parts.length - pending.length
1256
+ let doneSeconds = plan.parts
1257
+ .filter((part) => hasResult(resultsDir, part.index))
1258
+ .reduce((sum, part) => sum + part.duration, 0)
1259
+ let cursor = 0
1260
+ let lastPreview = doneCount
1261
+
1262
+ upsertJob({
1263
+ key,
1264
+ stage: 'transcribing',
1265
+ chunks: { total: plan.parts.length, done: doneCount, current: pending[0] ? pending[0].index + 1 : null },
1266
+ })
1267
+
1268
+ const worker = async () => {
1269
+ for (;;) {
1270
+ if (cancelRequested.has(key)) return
1271
+ const index = cursor
1272
+ cursor += 1
1273
+ const part = pending[index]
1274
+ if (!part) return
1275
+ // 取消意图可能来自另一个模块实例(只写磁盘),所以这里轮询而不是只认内存标记。
1276
+ const controller = new AbortController()
1277
+ const watcher = setInterval(() => {
1278
+ if (cancelRequested.has(key)) controller.abort()
1279
+ }, 400)
1280
+ try {
1281
+ const { text } = await transcribeChunk(part.file, asrApiConfig(), { signal: controller.signal })
1282
+ // 存的是「块内相对时间」:mergedFromResults 会把 part.start 加回去。
1283
+ const lines = buildApiLines(text, 0, part.duration)
1284
+ await writeFile(
1285
+ resultPath(resultsDir, part.index),
1286
+ JSON.stringify({ index: part.index, start: part.start, lines, language: '', text }),
1287
+ 'utf8',
1288
+ )
1289
+ } finally {
1290
+ clearInterval(watcher)
1291
+ }
1292
+ doneCount += 1
1293
+ doneSeconds += part.duration
1294
+ upsertJob({
1295
+ key,
1296
+ stage: 'transcribing',
1297
+ chunks: { total: plan.parts.length, done: doneCount, current: part.index + 1 },
1298
+ progress: progressOf(plan, doneSeconds, 0),
1299
+ })
1300
+ // 每完成五分之一写一次预览,用户能边转边看,不用干等到最后。
1301
+ const step = Math.max(1, Math.round(plan.parts.length / 5))
1302
+ if (doneCount - lastPreview >= step) {
1303
+ lastPreview = doneCount
1304
+ const preview = mergedFromResults(plan, resultsDir)
1305
+ if (preview.lines.length) {
1306
+ const built = buildPreview(episode, plan, preview)
1307
+ built.model = 'deepseek-asr'
1308
+ await saveTranscript(built).catch(() => {})
1309
+ partialWritten = true
1310
+ }
1311
+ }
1312
+ }
1313
+ }
1314
+
1315
+ await Promise.all(Array.from({ length: Math.min(API_CONCURRENCY, pending.length) }, () => worker()))
1316
+ if (cancelRequested.has(key)) throw new Error('用户取消了转写')
1317
+ const counts = countResults(resultsDir, plan)
1318
+ if (counts.count < plan.parts.length) throw new Error('部分音频块没有识别结果')
1319
+
1320
+ upsertJob({ key, stage: 'merging' })
1321
+ const merged = mergedFromResults(plan, resultsDir)
1322
+ let speakers = []
1323
+ if (transcribeConfig().diarize && diarizeInfo().modelsReady) {
1324
+ const prevPercent = getJob(key)?.progress?.percent || 0
1325
+ upsertJob({ key, stage: 'diarizing', progress: { done: 0, total: 0, percent: Math.max(prevPercent, 96), unit: 'seconds' } })
1326
+ try {
1327
+ const result = await runDiarization(episode.file)
1328
+ speakers = assignSpeakers(merged.lines, result.segments || [])
1329
+ } catch {
1330
+ speakers = []
1331
+ }
1332
+ }
1333
+
1334
+ let source = null
1335
+ try {
1336
+ const info = fs.statSync(episode.file)
1337
+ source = { path: episode.file, size: info.size, mtime: info.mtimeMs }
1338
+ } catch {
1339
+ source = { path: episode.file }
1340
+ }
1341
+
1342
+ await saveTranscript(apiTranscript(episode, plan, merged, speakers, source))
1343
+ await rm(backup, { force: true }).catch(() => {})
1344
+ upsertJob({
1345
+ key,
1346
+ status: 'done',
1347
+ stage: 'done',
1348
+ finishedAt: Date.now(),
1349
+ lineCount: merged.lines.length,
1350
+ language: merged.language,
1351
+ speakers: speakers.length,
1352
+ error: null,
1353
+ progress: { ...progressOf(plan, plan.duration, 0), percent: 100, done: plan.duration, total: plan.duration },
1354
+ chunks: { total: plan.parts.length, done: plan.parts.length, current: null },
1355
+ })
1356
+ await rm(work, { recursive: true, force: true })
1357
+ } catch (error) {
1358
+ if (backedUp) await copyFile(backup, target).catch(() => {})
1359
+ else if (partialWritten) await rm(target, { force: true }).catch(() => {})
1360
+ const disk = getJobFresh(key)
1361
+ const cancelledOnDisk = disk?.status === 'cancelled' || disk?.stage === 'cancelling'
1362
+ if (cancelRequested.has(key) || cancelledOnDisk) {
1363
+ cancelRequested.delete(key)
1364
+ upsertJob({ key, status: 'cancelled', stage: 'cancelled', finishedAt: Date.now(), error: '用户取消了转写', pid: null })
1365
+ } else {
1366
+ upsertJob({
1367
+ key,
1368
+ status: 'error',
1369
+ stage: 'error',
1370
+ finishedAt: Date.now(),
1371
+ error: error.message || String(error),
1372
+ })
1373
+ }
1374
+ } finally {
1375
+ cancelRequested.delete(key)
1376
+ active = null
1377
+ activeChild = null
1378
+ upsertJob({ key, pid: null, orphan: false })
1379
+ flushJobs()
1380
+ pump()
1381
+ }
1382
+ }
1383
+
1047
1384
  export async function startTranscription(episode, { force = false, resume = false } = {}) {
1048
1385
  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 })
1386
+ // 云端只要一把 key:不装 python 运行时、不下 2.4G 模型也能转写,这就是「装好就能用」。
1387
+ // 本地才检查运行时和模型,缺了就让用户去设置里一键装。
1388
+ if (info.provider === 'api') {
1389
+ if (!info.api.hasKey) {
1390
+ throw Object.assign(
1391
+ new Error('还没有填 DeepSeek API key。去「设置 → 播客 → 转写」填一个,或者切换成本地模型(免费但需要先下载模型)'),
1392
+ { status: 400 },
1393
+ )
1394
+ }
1395
+ } else {
1396
+ if (!info.installed) throw Object.assign(new Error('转写运行环境未安装(~/.podwave/runtime),在设置里点「一键安装本地模型」'), { status: 503 })
1397
+ if (!info.modelReady) throw Object.assign(new Error('转写模型未下载,请在设置里安装或检查模型目录'), { status: 503 })
1398
+ }
1051
1399
 
1052
1400
  const key = jobId(episode.id)
1053
1401
  const currentJob = getJob(key)
@@ -1073,7 +1421,7 @@ export async function startTranscription(episode, { force = false, resume = fals
1073
1421
  progress: { done: 0, total: 0, percent: 0, unit: 'seconds' },
1074
1422
  chunks: { total: 0, done: 0, current: null },
1075
1423
  })
1076
- queue.push({ episode, force: Boolean(force) || !resumeWork, resume: resumeWork })
1424
+ queue.push({ episode, provider: info.provider, force: Boolean(force) || !resumeWork, resume: resumeWork })
1077
1425
  pump()
1078
1426
  return { status: 'queued' }
1079
1427
  }
@@ -1260,6 +1608,30 @@ export async function mergeTranscriptLine(id, lineId) {
1260
1608
  return transcript
1261
1609
  }
1262
1610
 
1611
+ export async function resegmentTranscript(id) {
1612
+ const transcript = await readTranscript(id)
1613
+ if (!transcript) return null
1614
+ const before = transcript.lines.length
1615
+ const lines = resegmentLines(transcript.lines)
1616
+ if (!lines.length) throw new Error('没有可以整理的字幕行')
1617
+ const hasTranslations = transcript.lines.some((line) => line.translations && Object.keys(line.translations).length)
1618
+ // 断句一改,逐行译文就对不上了:旧译文先留一份备份,不直接丢。
1619
+ if (hasTranslations) {
1620
+ transcript.legacyTranslations = transcript.lines.map((line) => ({
1621
+ id: line.id,
1622
+ start: line.start,
1623
+ end: line.end,
1624
+ text: line.text,
1625
+ translations: line.translations || {},
1626
+ }))
1627
+ }
1628
+ transcript.lines = lines
1629
+ transcript.providers = {}
1630
+ refreshText(transcript)
1631
+ await saveTranscript(transcript)
1632
+ return { transcript, before, after: lines.length, cleared: hasTranslations }
1633
+ }
1634
+
1263
1635
  export async function validateTranscript(id) {
1264
1636
  const transcript = await readTranscript(id)
1265
1637
  if (!transcript) return { ok: false, issues: ['还没有转写'] }