dsh-podwave 0.10.15 → 0.10.20
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/README.md +1 -0
- package/dist/client.js +117 -69
- package/package.json +2 -2
- package/scripts/asr_sherpa_batch.py +3 -0
- package/src/downloader.js +101 -4
- package/src/host.js +49 -7
- package/src/setup.js +15 -5
- package/src/transcribe.js +104 -16
- package/src/translate.js +29 -1
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "dsh-podwave",
|
|
3
|
-
"version": "0.10.
|
|
3
|
+
"version": "0.10.20",
|
|
4
4
|
"description": "DeepSeek Harness 插件:本地优先的播客播放器 + 转写 + 双语字幕 + 字幕翻译。",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./src/host.js",
|
|
@@ -23,7 +23,7 @@
|
|
|
23
23
|
},
|
|
24
24
|
"engines": {
|
|
25
25
|
"node": ">=20",
|
|
26
|
-
"dsh": ">=0.2.1-rc.5 <0.3.0"
|
|
26
|
+
"dsh": ">=0.2.1-rc.5 <0.3.0 || >=0.2.2-rc.0 <0.3.0 || >=0.2.3-rc.0 <0.3.0 || >=0.2.4-rc.0 <0.3.0"
|
|
27
27
|
},
|
|
28
28
|
"keywords": [
|
|
29
29
|
"dsh",
|
|
@@ -16,7 +16,10 @@ def main():
|
|
|
16
16
|
parser.add_argument("--model-dir", required=True)
|
|
17
17
|
parser.add_argument("--out-dir", required=True)
|
|
18
18
|
parser.add_argument("--threads", type=int, default=4)
|
|
19
|
+
parser.add_argument("--hotwords", default="")
|
|
19
20
|
args = parser.parse_args()
|
|
21
|
+
if args.hotwords.strip():
|
|
22
|
+
print("note: SenseVoice 模型不支持热词,已忽略 --hotwords", file=sys.stderr)
|
|
20
23
|
os.makedirs(args.out_dir, exist_ok=True)
|
|
21
24
|
for wav in args.files:
|
|
22
25
|
base = os.path.splitext(os.path.basename(wav))[0]
|
package/src/downloader.js
CHANGED
|
@@ -4,6 +4,7 @@ import fs from 'node:fs'
|
|
|
4
4
|
import fsp from 'node:fs/promises'
|
|
5
5
|
import path from 'node:path'
|
|
6
6
|
import { spawn } from 'node:child_process'
|
|
7
|
+
import crypto from 'node:crypto'
|
|
7
8
|
import { Readable } from 'node:stream'
|
|
8
9
|
import { pipeline } from 'node:stream/promises'
|
|
9
10
|
|
|
@@ -38,7 +39,7 @@ export async function listHfRepo(repo, base, { include, skip } = {}) {
|
|
|
38
39
|
.filter((entry) => entry.type === 'file')
|
|
39
40
|
.filter((entry) => (include ? include.includes(entry.path) : true))
|
|
40
41
|
.filter((entry) => (skip ? !skip.some((re) => re.test(entry.path)) : true))
|
|
41
|
-
.map((entry) => ({ path: entry.path, size: entry.size || 0 }))
|
|
42
|
+
.map((entry) => ({ path: entry.path, size: entry.size || 0, sha256: entry.lfs?.oid || '' }))
|
|
42
43
|
}
|
|
43
44
|
|
|
44
45
|
export async function directHfFiles(repo, base, { include, files, skip } = {}) {
|
|
@@ -74,11 +75,93 @@ export async function listMsRepo(repo) {
|
|
|
74
75
|
.map((file) => ({ path: file.Path, size: file.Size || 0 }))
|
|
75
76
|
}
|
|
76
77
|
|
|
77
|
-
|
|
78
|
+
export function isSafeArchivePath(name) {
|
|
79
|
+
const value = String(name || '').replace(/\\/g, '/')
|
|
80
|
+
if (!value || value.startsWith('/')) return false
|
|
81
|
+
if (/^[a-zA-Z]:/.test(value)) return false
|
|
82
|
+
return !value.split('/').some((part) => part === '..')
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
function runCapture(cmd, args) {
|
|
86
|
+
return new Promise((resolve) => {
|
|
87
|
+
let output = ''
|
|
88
|
+
const child = spawn(cmd, args, { stdio: ['ignore', 'pipe', 'ignore'] })
|
|
89
|
+
child.stdout.on('data', (chunk) => {
|
|
90
|
+
output += chunk.toString('utf8')
|
|
91
|
+
})
|
|
92
|
+
child.on('error', () => resolve(null))
|
|
93
|
+
child.on('exit', (code) => resolve(code === 0 ? output : null))
|
|
94
|
+
})
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
async function listArchiveEntries(file) {
|
|
98
|
+
const lower = file.toLowerCase()
|
|
99
|
+
if (lower.endsWith('.zip')) {
|
|
100
|
+
const candidates = [
|
|
101
|
+
['unzip', ['-Z1', file]],
|
|
102
|
+
['tar', ['-tf', file]],
|
|
103
|
+
['python3', ['-c', 'import zipfile,sys;print("\n".join(zipfile.ZipFile(sys.argv[1]).namelist()))', file]],
|
|
104
|
+
]
|
|
105
|
+
for (const [cmd, args] of candidates) {
|
|
106
|
+
const output = await runCapture(cmd, args)
|
|
107
|
+
if (output) return output.split(/\r?\n/).filter(Boolean)
|
|
108
|
+
}
|
|
109
|
+
return null
|
|
110
|
+
}
|
|
111
|
+
const output = await runCapture('tar', ['-tf', file])
|
|
112
|
+
return output ? output.split(/\r?\n/).filter(Boolean) : null
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
async function sha256File(file) {
|
|
116
|
+
const hash = crypto.createHash('sha256')
|
|
117
|
+
await pipeline(fs.createReadStream(file), hash)
|
|
118
|
+
return hash.digest('hex')
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
const digestCache = new Map()
|
|
122
|
+
|
|
123
|
+
async function githubDigest(url) {
|
|
124
|
+
try {
|
|
125
|
+
const match = /^https:\/\/github\.com\/([^/]+)\/([^/]+)\/releases\/download\/([^/]+)\/(.+)$/.exec(url)
|
|
126
|
+
if (!match) return ''
|
|
127
|
+
const [, owner, repo, tag, assetName] = match
|
|
128
|
+
const key = `${owner}/${repo}@${tag}#${assetName}`
|
|
129
|
+
if (digestCache.has(key)) return digestCache.get(key)
|
|
130
|
+
const api =
|
|
131
|
+
tag === 'latest'
|
|
132
|
+
? `https://api.github.com/repos/${owner}/${repo}/releases/latest`
|
|
133
|
+
: `https://api.github.com/repos/${owner}/${repo}/releases/tags/${tag}`
|
|
134
|
+
const res = await fetch(api, {
|
|
135
|
+
headers: { 'user-agent': UA, accept: 'application/vnd.github+json' },
|
|
136
|
+
signal: timeout(15000),
|
|
137
|
+
})
|
|
138
|
+
if (!res.ok) {
|
|
139
|
+
digestCache.set(key, '')
|
|
140
|
+
return ''
|
|
141
|
+
}
|
|
142
|
+
const data = await res.json()
|
|
143
|
+
const asset = (data.assets || []).find((item) => item.name === decodeURIComponent(assetName))
|
|
144
|
+
const digest = asset?.digest ? String(asset.digest).replace(/^sha256:/, '') : ''
|
|
145
|
+
digestCache.set(key, digest)
|
|
146
|
+
return digest
|
|
147
|
+
} catch {
|
|
148
|
+
return ''
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
async function downloadOne(url, dest, { expected = 0, sha256 = '', onProgress, signal } = {}) {
|
|
78
153
|
await fsp.mkdir(path.dirname(dest), { recursive: true })
|
|
79
154
|
try {
|
|
80
155
|
const stat = await fsp.stat(dest)
|
|
81
|
-
if (expected && stat.size === expected)
|
|
156
|
+
if (expected && stat.size === expected) {
|
|
157
|
+
if (sha256) {
|
|
158
|
+
const actual = await sha256File(dest)
|
|
159
|
+
if (actual.toLowerCase() === String(sha256).toLowerCase()) return { bytes: stat.size, skipped: true }
|
|
160
|
+
await fsp.rm(dest, { force: true })
|
|
161
|
+
} else {
|
|
162
|
+
return { bytes: stat.size, skipped: true }
|
|
163
|
+
}
|
|
164
|
+
}
|
|
82
165
|
if (expected && stat.size > expected) await fsp.rm(dest, { force: true })
|
|
83
166
|
} catch {
|
|
84
167
|
// 文件不存在
|
|
@@ -108,6 +191,13 @@ async function downloadOne(url, dest, { expected = 0, onProgress, signal } = {})
|
|
|
108
191
|
await pipeline(source, write)
|
|
109
192
|
const stat = await fsp.stat(part)
|
|
110
193
|
if (expected && stat.size !== expected) throw new Error(`文件大小不符(期望 ${expected},实际 ${stat.size})`)
|
|
194
|
+
if (sha256) {
|
|
195
|
+
const actual = await sha256File(part)
|
|
196
|
+
if (actual.toLowerCase() !== String(sha256).toLowerCase()) {
|
|
197
|
+
await fsp.rm(part, { force: true })
|
|
198
|
+
throw new Error('文件校验失败(sha256 不匹配),已丢弃下载内容')
|
|
199
|
+
}
|
|
200
|
+
}
|
|
111
201
|
await fsp.rename(part, dest)
|
|
112
202
|
return { bytes: stat.size, skipped: false }
|
|
113
203
|
}
|
|
@@ -161,7 +251,8 @@ export async function downloadArtifact(artifact, { sourceId = 'auto', onProgress
|
|
|
161
251
|
const url = source.id === 'cn' ? `https://gh-proxy.com/${asset.url}` : asset.url
|
|
162
252
|
const dest = path.join(destDir, asset.file)
|
|
163
253
|
const before = result.bytes
|
|
164
|
-
const
|
|
254
|
+
const sha256 = await githubDigest(asset.url)
|
|
255
|
+
const { bytes } = await downloadOne(url, dest, { sha256, onProgress, signal })
|
|
165
256
|
result.bytes += bytes
|
|
166
257
|
result.files.push({ path: asset.file, bytes })
|
|
167
258
|
if (asset.extract) await extractArchive(dest, destDir)
|
|
@@ -214,6 +305,7 @@ export async function downloadArtifact(artifact, { sourceId = 'auto', onProgress
|
|
|
214
305
|
const dest = path.join(destDir, file.path)
|
|
215
306
|
const { bytes } = await downloadOne(url, dest, {
|
|
216
307
|
expected: file.size,
|
|
308
|
+
sha256: file.sha256 || '',
|
|
217
309
|
signal,
|
|
218
310
|
onProgress: (event) => {
|
|
219
311
|
done += event.delta
|
|
@@ -239,6 +331,11 @@ function runExtract(cmd, args) {
|
|
|
239
331
|
}
|
|
240
332
|
|
|
241
333
|
export async function extractArchive(file, destDir) {
|
|
334
|
+
const entries = await listArchiveEntries(file)
|
|
335
|
+
if (entries && entries.length) {
|
|
336
|
+
const unsafe = entries.find((entry) => !isSafeArchivePath(entry))
|
|
337
|
+
if (unsafe) throw new Error(`压缩包包含不安全路径:${unsafe}`)
|
|
338
|
+
}
|
|
242
339
|
const lower = file.toLowerCase()
|
|
243
340
|
const attempts = []
|
|
244
341
|
if (lower.endsWith('.zip')) {
|
package/src/host.js
CHANGED
|
@@ -8,7 +8,7 @@ import path from 'node:path'
|
|
|
8
8
|
import { createReadStream, createWriteStream } from 'node:fs'
|
|
9
9
|
import { execFile } from 'node:child_process'
|
|
10
10
|
import { pipeline } from 'node:stream/promises'
|
|
11
|
-
import { DATA_DIR } from './state.js'
|
|
11
|
+
import { DATA_DIR, COVERS_DIR } from './state.js'
|
|
12
12
|
import {
|
|
13
13
|
addFolderState,
|
|
14
14
|
getState,
|
|
@@ -142,7 +142,9 @@ function boot() {
|
|
|
142
142
|
await initState()
|
|
143
143
|
await migrateAllTranscripts().catch((error) => console.error('[dsh-podwave] transcript migration failed:', error.message))
|
|
144
144
|
if (getState().folders.length) {
|
|
145
|
-
scanLibrary()
|
|
145
|
+
scanLibrary()
|
|
146
|
+
.then(() => refreshWatchers())
|
|
147
|
+
.catch((error) => console.error('[dsh-podwave] initial scan failed:', error.message))
|
|
146
148
|
}
|
|
147
149
|
})().catch((error) => {
|
|
148
150
|
booted = null
|
|
@@ -386,7 +388,7 @@ async function route(ctx, req, res) {
|
|
|
386
388
|
}
|
|
387
389
|
if ((method === 'GET' || method === 'HEAD') && routePath === 'api/health') {
|
|
388
390
|
await mediaReady
|
|
389
|
-
return json(res, 200, { ok: true, app: 'podwave', version: '0.10.
|
|
391
|
+
return json(res, 200, { ok: true, app: 'podwave', version: '0.10.20', mediaBase, transcribe: runtimeInfo(), diarize: diarizeInfo(), translate: publicConfig() })
|
|
390
392
|
}
|
|
391
393
|
if (method === 'GET' && routePath === 'api/state') {
|
|
392
394
|
const state = getState()
|
|
@@ -508,8 +510,44 @@ async function route(ctx, req, res) {
|
|
|
508
510
|
const id = decodeURIComponent(hideMatch[1])
|
|
509
511
|
const hidden = new Set(getState().hidden || [])
|
|
510
512
|
hidden.add(id)
|
|
511
|
-
|
|
512
|
-
|
|
513
|
+
await deleteTranscript(id).catch(() => {})
|
|
514
|
+
for (const ext of ['jpg', 'png']) await rm(path.join(COVERS_DIR, `${id}.${ext}`), { force: true }).catch(() => {})
|
|
515
|
+
const favorites = new Set(getState().favorites || [])
|
|
516
|
+
favorites.delete(id)
|
|
517
|
+
const progress = { ...(getState().progress || {}) }
|
|
518
|
+
delete progress[id]
|
|
519
|
+
updateState({ hidden: [...hidden], favorites: [...favorites], progress })
|
|
520
|
+
return json(res, 200, { id, hidden: true, purged: true })
|
|
521
|
+
}
|
|
522
|
+
|
|
523
|
+
if (routePath === 'api/search' && method === 'GET') {
|
|
524
|
+
const query = String(new URL(req.url, 'http://127.0.0.1').searchParams.get('q') || '').trim()
|
|
525
|
+
if (query.length < 2) return json(res, 200, { items: [], total: 0 })
|
|
526
|
+
const needle = query.toLowerCase()
|
|
527
|
+
const titles = new Map(getEpisodes().map((episode) => [episode.id, episode]))
|
|
528
|
+
const items = []
|
|
529
|
+
const names = await readdir(TRANSCRIPTS_DIR).catch(() => [])
|
|
530
|
+
for (const name of names) {
|
|
531
|
+
if (!name.endsWith('.json') || items.length >= 80) continue
|
|
532
|
+
const id = name.slice(0, -5)
|
|
533
|
+
const transcript = await readTranscript(id).catch(() => null)
|
|
534
|
+
if (!transcript) continue
|
|
535
|
+
for (const line of transcript.lines || []) {
|
|
536
|
+
if (items.length >= 80) break
|
|
537
|
+
if (!String(line.text || '').toLowerCase().includes(needle)) continue
|
|
538
|
+
const episode = titles.get(id)
|
|
539
|
+
items.push({
|
|
540
|
+
episodeId: id,
|
|
541
|
+
lineId: line.id,
|
|
542
|
+
start: line.start,
|
|
543
|
+
end: line.end,
|
|
544
|
+
text: line.text,
|
|
545
|
+
title: episode?.title || transcript.title || '',
|
|
546
|
+
episodeTitle: episode?.title || transcript.title || '',
|
|
547
|
+
})
|
|
548
|
+
}
|
|
549
|
+
}
|
|
550
|
+
return json(res, 200, { items, total: items.length })
|
|
513
551
|
}
|
|
514
552
|
|
|
515
553
|
if (routePath === 'api/import/settings') {
|
|
@@ -542,7 +580,9 @@ async function route(ctx, req, res) {
|
|
|
542
580
|
const body = await readJson(req)
|
|
543
581
|
const folder = await addFolder(body?.path)
|
|
544
582
|
addFolderState(folder)
|
|
545
|
-
scanLibrary()
|
|
583
|
+
scanLibrary()
|
|
584
|
+
.then(() => refreshWatchers())
|
|
585
|
+
.catch((error) => console.error('[dsh-podwave] scan failed:', error.message))
|
|
546
586
|
return json(res, 200, { folder, folders: getState().folders })
|
|
547
587
|
}
|
|
548
588
|
if (method === 'DELETE') {
|
|
@@ -556,7 +596,9 @@ async function route(ctx, req, res) {
|
|
|
556
596
|
const removed = forgetEpisodesUnder(target)
|
|
557
597
|
removeProgressFor((id) => removed.has(id))
|
|
558
598
|
removeFolderState(target)
|
|
559
|
-
scanLibrary()
|
|
599
|
+
scanLibrary()
|
|
600
|
+
.then(() => refreshWatchers())
|
|
601
|
+
.catch((error) => console.error('[dsh-podwave] scan failed:', error.message))
|
|
560
602
|
return json(res, 200, { folders: next })
|
|
561
603
|
}
|
|
562
604
|
return json(res, 405, { error: '不支持的方法' })
|
package/src/setup.js
CHANGED
|
@@ -9,7 +9,7 @@ import { getSection, updateSection } from './config.js'
|
|
|
9
9
|
import { getJob, upsertJob } from './jobs.js'
|
|
10
10
|
import { CATALOG, FFMPEG_BINARY, LLAMA_BINARY, SOURCE_LABELS, artifactFor, ffmpegAsset, llamaAsset, sourcesFor } from './catalog.js'
|
|
11
11
|
import { downloadArtifact, extractArchive } from './downloader.js'
|
|
12
|
-
import { detectPlatform, findExecutable, ffmpegPath, ffprobePath, recommendEngine, venvDir, venvPython, isWindows } from './platform.js'
|
|
12
|
+
import { detectPlatform, findExecutable, ffmpegPath, ffprobePath, recommendEngine, venvDir, venvPython, isWindows, isMac } from './platform.js'
|
|
13
13
|
|
|
14
14
|
const RUNTIME_DIR = path.join(DATA_DIR, 'runtime')
|
|
15
15
|
const SETUP_PATH = path.join(DATA_DIR, 'setup.json')
|
|
@@ -338,7 +338,7 @@ export function scan() {
|
|
|
338
338
|
label: 'ffmpeg / ffprobe',
|
|
339
339
|
required: true,
|
|
340
340
|
ready: Boolean(ffmpegPath() && ffprobePath()),
|
|
341
|
-
detail: ffmpegPath() || (ffAsset ? '未找到,将下载静态构建' : '未找到;macOS 请先 brew install ffmpeg'),
|
|
341
|
+
detail: ffmpegPath() || (ffAsset ? '未找到,将下载静态构建' : isMac && findExecutable('brew') ? '未找到,一键安装将通过 Homebrew 安装' : '未找到;macOS 请先 brew install ffmpeg'),
|
|
342
342
|
bytes: ffAsset?.bytes || 0,
|
|
343
343
|
})
|
|
344
344
|
push({
|
|
@@ -427,9 +427,18 @@ export async function install({ source = 'auto' } = {}) {
|
|
|
427
427
|
// 0. ffmpeg(Windows / Linux 直接下载静态构建;macOS 优先用系统/Homebrew)
|
|
428
428
|
if (!ffmpegPath()) {
|
|
429
429
|
const ffAsset = ffmpegAsset(platform)
|
|
430
|
-
if (
|
|
431
|
-
|
|
432
|
-
|
|
430
|
+
if (isMac) {
|
|
431
|
+
const brew = findExecutable('brew')
|
|
432
|
+
if (!brew) throw new Error('没有找到 ffmpeg,也没有 Homebrew;请安装 Homebrew 或把 ffmpeg/ffprobe 放进 PATH')
|
|
433
|
+
report({ stage: '通过 Homebrew 安装 ffmpeg' })
|
|
434
|
+
await run(brew, ['install', 'ffmpeg'])
|
|
435
|
+
if (!ffmpegPath()) throw new Error('Homebrew 安装后仍未找到 ffmpeg,请检查 brew 输出')
|
|
436
|
+
summary.push({ id: 'ffmpeg', label: 'ffmpeg / ffprobe(Homebrew)', bytes: 0 })
|
|
437
|
+
doneBytes += 0
|
|
438
|
+
report({ stage: '检查环境' })
|
|
439
|
+
} else if (!ffAsset) {
|
|
440
|
+
throw new Error('没有找到 ffmpeg:请安装 ffmpeg 或把它放进 PATH')
|
|
441
|
+
} else {
|
|
433
442
|
report({ stage: '下载 ffmpeg' })
|
|
434
443
|
await downloadArtifact(
|
|
435
444
|
{
|
|
@@ -446,6 +455,7 @@ export async function install({ source = 'auto' } = {}) {
|
|
|
446
455
|
if (fs.existsSync(archive)) await extractArchive(archive, target)
|
|
447
456
|
doneBytes += ffAsset.bytes
|
|
448
457
|
summary.push({ id: 'ffmpeg', label: 'ffmpeg / ffprobe', bytes: ffAsset.bytes })
|
|
458
|
+
}
|
|
449
459
|
}
|
|
450
460
|
|
|
451
461
|
// 1. Python 运行时
|
package/src/transcribe.js
CHANGED
|
@@ -225,6 +225,24 @@ function waitChild(child) {
|
|
|
225
225
|
})
|
|
226
226
|
}
|
|
227
227
|
|
|
228
|
+
function runCapture(command, args) {
|
|
229
|
+
return new Promise((resolve) => {
|
|
230
|
+
let output = ''
|
|
231
|
+
let child
|
|
232
|
+
try {
|
|
233
|
+
child = spawn(command, args, { stdio: ['ignore', 'ignore', 'pipe'] })
|
|
234
|
+
} catch {
|
|
235
|
+
resolve('')
|
|
236
|
+
return
|
|
237
|
+
}
|
|
238
|
+
child.stderr.on('data', (chunk) => {
|
|
239
|
+
output += chunk.toString('utf8')
|
|
240
|
+
})
|
|
241
|
+
child.on('error', () => resolve(''))
|
|
242
|
+
child.on('close', () => resolve(output))
|
|
243
|
+
})
|
|
244
|
+
}
|
|
245
|
+
|
|
228
246
|
function runProcess(command, args) {
|
|
229
247
|
return new Promise((resolve) => {
|
|
230
248
|
let child
|
|
@@ -260,6 +278,64 @@ async function probeDuration(file) {
|
|
|
260
278
|
}
|
|
261
279
|
|
|
262
280
|
// 用 ffmpeg 把长音频切成若干块;先尝试不重编码(-c copy),失败再转 AAC。
|
|
281
|
+
async function detectSilenceMidpoints(file) {
|
|
282
|
+
const output = await runCapture(FFMPEG, [
|
|
283
|
+
'-hide_banner',
|
|
284
|
+
'-nostats',
|
|
285
|
+
'-i', file,
|
|
286
|
+
'-af', 'silencedetect=noise=-30dB:d=0.45',
|
|
287
|
+
'-f', 'null',
|
|
288
|
+
'-',
|
|
289
|
+
])
|
|
290
|
+
const starts = []
|
|
291
|
+
const mids = []
|
|
292
|
+
for (const line of output.split(/\r?\n/)) {
|
|
293
|
+
const start = /silence_start: (-?[\d.]+)/.exec(line)
|
|
294
|
+
if (start) starts.push(Number(start[1]))
|
|
295
|
+
const end = /silence_end: ([\d.]+)/.exec(line)
|
|
296
|
+
if (end && starts.length) {
|
|
297
|
+
const from = starts.pop()
|
|
298
|
+
const to = Number(end[1])
|
|
299
|
+
if (to > from) mids.push((from + to) / 2)
|
|
300
|
+
}
|
|
301
|
+
}
|
|
302
|
+
return mids
|
|
303
|
+
}
|
|
304
|
+
|
|
305
|
+
function planCuts(duration, chunkSeconds, silences) {
|
|
306
|
+
const cuts = [0]
|
|
307
|
+
let target = chunkSeconds
|
|
308
|
+
while (target < duration - 20) {
|
|
309
|
+
const floor = cuts[cuts.length - 1] + Math.min(chunkSeconds * 0.5, 30)
|
|
310
|
+
const window = chunkSeconds * 0.45
|
|
311
|
+
const candidates = silences.filter((time) => time > floor && time < duration - 8 && Math.abs(time - target) <= window)
|
|
312
|
+
const pick = candidates.sort((a, b) => Math.abs(a - target) - Math.abs(b - target))[0]
|
|
313
|
+
cuts.push(round2(pick ?? target))
|
|
314
|
+
target = (pick ?? target) + chunkSeconds
|
|
315
|
+
}
|
|
316
|
+
cuts.push(round2(duration))
|
|
317
|
+
return cuts.filter((value, index, list) => index === 0 || value > list[index - 1] + 5)
|
|
318
|
+
}
|
|
319
|
+
|
|
320
|
+
async function splitAudioAt(file, dir, cuts) {
|
|
321
|
+
await mkdir(dir, { recursive: true })
|
|
322
|
+
const sherpa = currentAsrModel().engine === 'sherpa'
|
|
323
|
+
const parts = []
|
|
324
|
+
for (let index = 0; index < cuts.length - 1; index += 1) {
|
|
325
|
+
const start = cuts[index]
|
|
326
|
+
const end = cuts[index + 1]
|
|
327
|
+
const ext = sherpa ? 'wav' : 'm4a'
|
|
328
|
+
const out = path.join(dir, `chunk-${String(index).padStart(3, '0')}.${ext}`)
|
|
329
|
+
const code = sherpa
|
|
330
|
+
? 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])
|
|
331
|
+
: await runProcess(FFMPEG, ['-hide_banner', '-loglevel', 'error', '-y', '-i', file, '-ss', String(start), '-to', String(end), '-vn', '-c:a', 'aac', '-b:a', '96k', out])
|
|
332
|
+
if (code !== 0) throw new Error('音频切分失败(ffmpeg)')
|
|
333
|
+
const actual = (await probeDuration(out)) || end - start
|
|
334
|
+
parts.push({ index, start: round2(start), end: round2(start + actual), file: out, duration: round2(actual) })
|
|
335
|
+
}
|
|
336
|
+
return parts
|
|
337
|
+
}
|
|
338
|
+
|
|
263
339
|
async function splitAudio(file, dir, chunkSeconds) {
|
|
264
340
|
await mkdir(dir, { recursive: true })
|
|
265
341
|
const sherpa = currentAsrModel().engine === 'sherpa'
|
|
@@ -312,9 +388,16 @@ async function ensurePlan(episode, workDir) {
|
|
|
312
388
|
await rm(path.join(workDir, 'parts'), { recursive: true, force: true })
|
|
313
389
|
await rm(path.join(workDir, 'results'), { recursive: true, force: true })
|
|
314
390
|
const single = currentAsrModel().engine !== 'sherpa' && duration <= chunkSeconds * 1.2
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
:
|
|
391
|
+
let parts
|
|
392
|
+
if (single) {
|
|
393
|
+
parts = [{ index: 0, start: 0, end: round2(duration), file: episode.file, duration: round2(duration) }]
|
|
394
|
+
} else {
|
|
395
|
+
const silences = await detectSilenceMidpoints(episode.file).catch(() => [])
|
|
396
|
+
const cuts = planCuts(duration, chunkSeconds, silences)
|
|
397
|
+
parts = cuts.length > 2
|
|
398
|
+
? await splitAudioAt(episode.file, path.join(workDir, 'parts'), cuts)
|
|
399
|
+
: await splitAudio(episode.file, path.join(workDir, 'parts'), chunkSeconds)
|
|
400
|
+
}
|
|
318
401
|
const plan = { duration: round2(duration), chunkSeconds, chunked: !single, parts }
|
|
319
402
|
await writeFile(metaPath, JSON.stringify(plan, null, 2), 'utf8')
|
|
320
403
|
return plan
|
|
@@ -370,10 +453,12 @@ function wordsOf(raw) {
|
|
|
370
453
|
|
|
371
454
|
function buildBatchArgs(files, outDir) {
|
|
372
455
|
const model = currentAsrModel()
|
|
456
|
+
const { hotwords } = transcribeConfig()
|
|
373
457
|
if (model.engine === 'sherpa') {
|
|
374
|
-
|
|
458
|
+
const args = [SHERPA_SCRIPT, ...files, '--model-dir', model.dir, '--out-dir', outDir, '--threads', '4']
|
|
459
|
+
if (hotwords.length) args.push('--hotwords', hotwords.join(' '))
|
|
460
|
+
return args
|
|
375
461
|
}
|
|
376
|
-
const { hotwords } = transcribeConfig()
|
|
377
462
|
const args = [
|
|
378
463
|
...files,
|
|
379
464
|
'--model', currentAsrModel().dir,
|
|
@@ -703,9 +788,9 @@ function groupWords(words) {
|
|
|
703
788
|
const elapsed = current.end - current.start
|
|
704
789
|
if (
|
|
705
790
|
/[。!?!?…]$/.test(joined) ||
|
|
706
|
-
(/[,,;;::]$/.test(joined) && joined.length >
|
|
707
|
-
joined.length >=
|
|
708
|
-
elapsed >=
|
|
791
|
+
(/[,,;;::]$/.test(joined) && joined.length > 42) ||
|
|
792
|
+
joined.length >= 90 ||
|
|
793
|
+
elapsed >= 15
|
|
709
794
|
) {
|
|
710
795
|
flush()
|
|
711
796
|
}
|
|
@@ -812,18 +897,21 @@ function buildSegments(raw, words) {
|
|
|
812
897
|
cursor += 1
|
|
813
898
|
}
|
|
814
899
|
appendWord(token.text, matched ? matched.start : chunkStart, matched ? matched.end : chunkEnd)
|
|
815
|
-
if (current && (current.text
|
|
900
|
+
if (current && /[,,;;]$/.test(current.text) && current.text.length >= 48) flush()
|
|
901
|
+
if (current && (current.text.length >= 90 || current.end - current.start >= 15)) flush()
|
|
816
902
|
}
|
|
817
|
-
flush()
|
|
903
|
+
if (current && /[。!?!?…]$/.test(current.text) && current.text.length > 2) flush()
|
|
818
904
|
}
|
|
819
905
|
flush()
|
|
820
906
|
|
|
821
|
-
return lines
|
|
822
|
-
|
|
823
|
-
|
|
824
|
-
|
|
825
|
-
|
|
826
|
-
|
|
907
|
+
return lines
|
|
908
|
+
.filter((line) => line.text.trim() && line.end - line.start >= 0.2)
|
|
909
|
+
.map((line, index) => ({
|
|
910
|
+
id: index + 1,
|
|
911
|
+
start: round2(line.start),
|
|
912
|
+
end: round2(line.end),
|
|
913
|
+
text: line.text.trim(),
|
|
914
|
+
}))
|
|
827
915
|
}
|
|
828
916
|
|
|
829
917
|
export async function startTranscription(episode, { force = false, resume = false } = {}) {
|
package/src/translate.js
CHANGED
|
@@ -70,6 +70,34 @@ function parseNumbered(text, count) {
|
|
|
70
70
|
return result
|
|
71
71
|
}
|
|
72
72
|
|
|
73
|
+
async function fetchWithRetry(url, init, { attempts = 3, timeoutMs = 120000 } = {}) {
|
|
74
|
+
let lastError = null
|
|
75
|
+
for (let attempt = 1; attempt <= attempts; attempt += 1) {
|
|
76
|
+
try {
|
|
77
|
+
const response = await fetch(url, { ...init, signal: AbortSignal.timeout(timeoutMs) })
|
|
78
|
+
if (response.status === 429 || response.status >= 500) {
|
|
79
|
+
lastError = new Error(`翻译 API ${response.status}`)
|
|
80
|
+
if (attempt < attempts) {
|
|
81
|
+
await new Promise((resolve) => setTimeout(resolve, attempt * 1500))
|
|
82
|
+
continue
|
|
83
|
+
}
|
|
84
|
+
const detail = await response.text().catch(() => '')
|
|
85
|
+
throw new Error(`翻译 API ${response.status}:${detail.slice(0, 200)}`)
|
|
86
|
+
}
|
|
87
|
+
return response
|
|
88
|
+
} catch (error) {
|
|
89
|
+
lastError = error
|
|
90
|
+
const retryable = error?.name === 'TimeoutError' || /fetch failed|network|ECONN|ETIMEDOUT/i.test(String(error?.message || error))
|
|
91
|
+
if (retryable && attempt < attempts) {
|
|
92
|
+
await new Promise((resolve) => setTimeout(resolve, attempt * 1500))
|
|
93
|
+
continue
|
|
94
|
+
}
|
|
95
|
+
throw error
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
throw lastError
|
|
99
|
+
}
|
|
100
|
+
|
|
73
101
|
async function translateWithApi(items, target, config) {
|
|
74
102
|
const language = target === 'zh' ? 'Simplified Chinese' : 'English'
|
|
75
103
|
const system = `You are a professional subtitle translator. Translate each numbered line into ${language}. Preserve proper names, numbers and terminology. Output only numbered lines in the format "1. translation".`
|
|
@@ -78,7 +106,7 @@ async function translateWithApi(items, target, config) {
|
|
|
78
106
|
for (let start = 0; start < items.length; start += batchSize) {
|
|
79
107
|
const batch = items.slice(start, start + batchSize)
|
|
80
108
|
const content = batch.map((line, index) => `${index + 1}. ${line.text}`).join('\n')
|
|
81
|
-
const response = await
|
|
109
|
+
const response = await fetchWithRetry(`${config.apiBase.replace(/\/+$/, '')}/chat/completions`, {
|
|
82
110
|
method: 'POST',
|
|
83
111
|
headers: {
|
|
84
112
|
'content-type': 'application/json',
|