dsh-podwave 0.11.28 → 0.11.30

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,14 +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, asrApiConfig, asrProvider, buildApiLines, transcribeChunk } from './asr-api.js'
14
+ import { API_CHUNK_SECONDS, REALTIME_CHUNK_SECONDS, asrApiConfig, asrProvider, buildApiLines, transcribeChunk } from './asr-api.js'
15
15
  import { artifactByDir } from './catalog.js'
16
16
  import { assignSpeakers, diarizeInfo, runDiarization } from './diarize.js'
17
17
  import { resegmentLines } from './segment.js'
18
+ import { closeLiveSpeakers, createLiveSpeakers } from './live-speaker.js'
19
+ import { loadConfig as loadTranslateConfig, needsTranslation, translateWithApi } from './translate.js'
18
20
  import { flushJobs, getJob, getJobFresh, jobKey, removeJob, upsertJob } from './jobs.js'
19
21
 
20
22
  export const TRANSCRIPTS_DIR = path.join(DATA_DIR, 'transcripts')
@@ -579,11 +581,12 @@ async function splitAudio(file, dir, chunkSeconds) {
579
581
  }
580
582
 
581
583
  // 生成或复用切块计划;计划存 .work/<id>/meta.json,块结果在 .work/<id>/results/<index>.json。
582
- async function ensurePlan(episode, workDir, shouldCancel, provider = asrProvider()) {
584
+ async function ensurePlan(episode, workDir, shouldCancel, provider = asrProvider(), chunkSecondsOverride) {
583
585
  const api = provider === 'api'
584
586
  const { chunkMinutes } = transcribeConfig()
585
- // 云端接口硬限制单次 120 秒,所以固定切 30 秒一块;本地引擎沿用设置里的 chunkMinutes。
586
- const chunkSeconds = api ? API_CHUNK_SECONDS : Math.round(chunkMinutes * 60)
587
+ // 云端接口硬限制单次 120 秒:整段转写用 100 秒的大块,实时跟播用 12 秒的小块。
588
+ // 本地引擎沿用设置里的 chunkMinutes。
589
+ const chunkSeconds = chunkSecondsOverride || (api ? API_CHUNK_SECONDS : Math.round(chunkMinutes * 60))
587
590
  const duration = Number(episode.duration) || (await probeDuration(episode.file)) || 0
588
591
  if (!duration) throw new Error('无法读取音频时长(ffprobe)')
589
592
  const metaPath = path.join(workDir, 'meta.json')
@@ -770,6 +773,46 @@ function mergedFromResults(plan, resultsDir) {
770
773
  return { lines, language, texts }
771
774
  }
772
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
+
773
816
  function buildPreview(episode, plan, merged) {
774
817
  return {
775
818
  schemaVersion: 2,
@@ -1218,8 +1261,10 @@ function apiTranscript(episode, plan, merged, speakers, source) {
1218
1261
 
1219
1262
  async function runApiJob(item) {
1220
1263
  const { episode, force } = item
1264
+ const realtime = Boolean(item.realtime)
1221
1265
  const key = jobId(episode.id)
1222
- const work = path.join(WORK_DIR, episode.id)
1266
+ // 实时跟播和整段转写的块号不通用,缓存目录必须分开,否则续跑会读到别的切法。
1267
+ const work = path.join(WORK_DIR, realtime ? `${episode.id}-rt` : episode.id)
1223
1268
  const target = transcriptPath(episode.id)
1224
1269
  const backup = `${target}.previous`
1225
1270
  let plan = null
@@ -1241,7 +1286,7 @@ async function runApiJob(item) {
1241
1286
  })
1242
1287
  if (force) await rm(work, { recursive: true, force: true })
1243
1288
  await mkdir(work, { recursive: true })
1244
- plan = await ensurePlan(episode, work, () => cancelRequested.has(key), 'api')
1289
+ plan = await ensurePlan(episode, work, () => cancelRequested.has(key), 'api', realtime ? REALTIME_CHUNK_SECONDS : undefined)
1245
1290
  const resultsDir = path.join(work, 'results')
1246
1291
  await mkdir(resultsDir, { recursive: true })
1247
1292
 
@@ -1258,6 +1303,32 @@ async function runApiJob(item) {
1258
1303
  .reduce((sum, part) => sum + part.duration, 0)
1259
1304
  let cursor = 0
1260
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
+ }
1261
1332
 
1262
1333
  upsertJob({
1263
1334
  key,
@@ -1265,12 +1336,33 @@ async function runApiJob(item) {
1265
1336
  chunks: { total: plan.parts.length, done: doneCount, current: pending[0] ? pending[0].index + 1 : null },
1266
1337
  })
1267
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
+
1268
1362
  const worker = async () => {
1269
1363
  for (;;) {
1270
1364
  if (cancelRequested.has(key)) return
1271
- const index = cursor
1272
- cursor += 1
1273
- const part = pending[index]
1365
+ const part = takePart()
1274
1366
  if (!part) return
1275
1367
  // 取消意图可能来自另一个模块实例(只写磁盘),所以这里轮询而不是只认内存标记。
1276
1368
  const controller = new AbortController()
@@ -1297,9 +1389,11 @@ async function runApiJob(item) {
1297
1389
  chunks: { total: plan.parts.length, done: doneCount, current: part.index + 1 },
1298
1390
  progress: progressOf(plan, doneSeconds, 0),
1299
1391
  })
1300
- // 每完成五分之一写一次预览,用户能边转边看,不用干等到最后。
1392
+ // 实时:每块都发一次,字幕才跟得上;批量:每完成五分之一写一次预览。
1301
1393
  const step = Math.max(1, Math.round(plan.parts.length / 5))
1302
- if (doneCount - lastPreview >= step) {
1394
+ if (realtime) {
1395
+ await publishLivePreview().catch(() => {})
1396
+ } else if (doneCount - lastPreview >= step) {
1303
1397
  lastPreview = doneCount
1304
1398
  const preview = mergedFromResults(plan, resultsDir)
1305
1399
  if (preview.lines.length) {
@@ -1320,6 +1414,11 @@ async function runApiJob(item) {
1320
1414
  upsertJob({ key, stage: 'merging' })
1321
1415
  const merged = mergedFromResults(plan, resultsDir)
1322
1416
  let speakers = []
1417
+ // 实时期间攒下的译文要带进最终字幕;整段说话人分离不可用时,保留在线标签。
1418
+ if (realtime) {
1419
+ applyLiveTranslations(merged.lines, liveTranslations)
1420
+ if (live) speakers = live.speakers()
1421
+ }
1323
1422
  if (transcribeConfig().diarize && diarizeInfo().modelsReady) {
1324
1423
  const prevPercent = getJob(key)?.progress?.percent || 0
1325
1424
  upsertJob({ key, stage: 'diarizing', progress: { done: 0, total: 0, percent: Math.max(prevPercent, 96), unit: 'seconds' } })
@@ -1339,7 +1438,9 @@ async function runApiJob(item) {
1339
1438
  source = { path: episode.file }
1340
1439
  }
1341
1440
 
1342
- await saveTranscript(apiTranscript(episode, plan, merged, speakers, source))
1441
+ const final = apiTranscript(episode, plan, merged, speakers, source)
1442
+ if (realtime && liveTranslations.size) final.providers = { [LIVE_TARGET]: 'api' }
1443
+ await saveTranscript(final)
1343
1444
  await rm(backup, { force: true }).catch(() => {})
1344
1445
  upsertJob({
1345
1446
  key,
@@ -1356,7 +1457,7 @@ async function runApiJob(item) {
1356
1457
  await rm(work, { recursive: true, force: true })
1357
1458
  } catch (error) {
1358
1459
  if (backedUp) await copyFile(backup, target).catch(() => {})
1359
- else if (partialWritten) await rm(target, { force: true }).catch(() => {})
1460
+ else if (partialWritten && !realtime) await rm(target, { force: true }).catch(() => {})
1360
1461
  const disk = getJobFresh(key)
1361
1462
  const cancelledOnDisk = disk?.status === 'cancelled' || disk?.stage === 'cancelling'
1362
1463
  if (cancelRequested.has(key) || cancelledOnDisk) {
@@ -1375,13 +1476,17 @@ async function runApiJob(item) {
1375
1476
  cancelRequested.delete(key)
1376
1477
  active = null
1377
1478
  activeChild = null
1479
+ // 声纹服务是常驻进程,任务结束就关掉,否则会一直挂在后台。
1480
+ if (realtime) closeLiveSpeakers()
1378
1481
  upsertJob({ key, pid: null, orphan: false })
1379
1482
  flushJobs()
1380
1483
  pump()
1381
1484
  }
1382
1485
  }
1383
1486
 
1384
- export async function startTranscription(episode, { force = false, resume = false } = {}) {
1487
+ // realtime=true:边播边转(12 秒小块、按播放头挑块、每块立刻出字幕/译文/说话人)。
1488
+ // 只有云端 API 支持这种走法;本地引擎是整段 batch 的,忽略 realtime 走普通流程。
1489
+ export async function startTranscription(episode, { force = false, resume = false, realtime = false } = {}) {
1385
1490
  const info = runtimeInfo()
1386
1491
  // 云端只要一把 key:不装 python 运行时、不下 2.4G 模型也能转写,这就是「装好就能用」。
1387
1492
  // 本地才检查运行时和模型,缺了就让用户去设置里一键装。
@@ -1403,10 +1508,15 @@ export async function startTranscription(episode, { force = false, resume = fals
1403
1508
  return { status: currentJob.status }
1404
1509
  }
1405
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
+
1406
1514
  const existing = await readTranscript(episode.id)
1407
- 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 }
1408
1518
 
1409
- 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'))
1410
1520
  upsertJob({
1411
1521
  key,
1412
1522
  type: 'transcribe',
@@ -1421,7 +1531,7 @@ export async function startTranscription(episode, { force = false, resume = fals
1421
1531
  progress: { done: 0, total: 0, percent: 0, unit: 'seconds' },
1422
1532
  chunks: { total: 0, done: 0, current: null },
1423
1533
  })
1424
- queue.push({ episode, provider: info.provider, force: Boolean(force) || !resumeWork, resume: resumeWork })
1534
+ queue.push({ episode, provider: info.provider, realtime: useRealtime, force: Boolean(force) || !resumeWork, resume: resumeWork })
1425
1535
  pump()
1426
1536
  return { status: 'queued' }
1427
1537
  }