2026-04-30 21:18:31 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* Phase: tts — 语音合成
|
|
|
|
|
|
*
|
|
|
|
|
|
* 使用通义千问 TTS 生成旁白音频
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
const path = require('path')
|
|
|
|
|
|
const { saveManifest, ensureDir, log, getManifestDir } = require('./pipeline-utils')
|
|
|
|
|
|
|
2026-05-01 00:44:18 +08:00
|
|
|
|
async function phaseTts(manifest, manifestPath, options = {}) {
|
2026-04-30 21:18:31 +08:00
|
|
|
|
const dir = getManifestDir(manifestPath)
|
|
|
|
|
|
const audioDir = path.join(dir, 'audio')
|
|
|
|
|
|
ensureDir(audioDir)
|
|
|
|
|
|
|
|
|
|
|
|
const { synthesize } = require('../qwen-tts')
|
|
|
|
|
|
|
|
|
|
|
|
const items = manifest.items.filter(it =>
|
2026-05-01 01:52:02 +08:00
|
|
|
|
it.status === 'done' && (it.script || it.text) && !it.audio
|
2026-04-30 21:18:31 +08:00
|
|
|
|
)
|
|
|
|
|
|
if (items.length === 0) { log('tts', '无待处理 item,跳过'); return }
|
|
|
|
|
|
|
|
|
|
|
|
log('tts', `共 ${items.length} 段`)
|
|
|
|
|
|
|
|
|
|
|
|
for (let i = 0; i < items.length; i++) {
|
|
|
|
|
|
const item = items[i]
|
|
|
|
|
|
const idx = i + 1
|
|
|
|
|
|
try {
|
2026-05-01 01:52:02 +08:00
|
|
|
|
const { filePath, duration } = await synthesize(item.script || item.text, {
|
2026-04-30 21:18:31 +08:00
|
|
|
|
outputDir: audioDir,
|
|
|
|
|
|
id: item.id || idx,
|
2026-05-01 00:44:18 +08:00
|
|
|
|
voice: manifest.ttsVoice || undefined,
|
|
|
|
|
|
instruction: manifest.ttsInstruction || undefined,
|
2026-05-01 14:16:08 +08:00
|
|
|
|
rate: manifest.ttsRate || undefined,
|
2026-04-30 21:18:31 +08:00
|
|
|
|
})
|
|
|
|
|
|
item.audio = path.relative(dir, filePath).replace(/\\/g, '/')
|
|
|
|
|
|
item.audioDuration = Math.round(duration * 1000) / 1000
|
2026-05-01 01:52:02 +08:00
|
|
|
|
log('tts', `[${idx}/${items.length}] ${duration.toFixed(1)}s: ${(item.script || item.text).substring(0, 30)}...`)
|
2026-04-30 21:18:31 +08:00
|
|
|
|
} catch (err) {
|
|
|
|
|
|
item.status = 'failed'
|
|
|
|
|
|
item.error = `TTS失败: ${err.message}`
|
|
|
|
|
|
log('tts', `[${idx}/${items.length}] 失败: ${err.message}`)
|
|
|
|
|
|
}
|
|
|
|
|
|
saveManifest(manifestPath, manifest)
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
module.exports = { phaseTts }
|