/** * Phase: tts — 语音合成(整段合成) * * 每个 item 的 script 整段合成一个音频文件,保留自然语调。 * item.audio 指向完整音频,item.audioDuration 为总时长。 * 字幕切分由组装阶段按字符比例分配,不在 TTS 阶段处理。 */ const path = require('path') const { saveManifest, ensureDir, log, getManifestDir } = require('./pipeline-utils') async function phaseTts(manifest, manifestPath, options = {}) { const dir = getManifestDir(manifestPath) const audioDir = path.join(dir, 'audio') ensureDir(audioDir) const { synthesize } = require('../qwen-tts') const items = manifest.items.filter(it => it.status === 'done' && (it.script || it.text) && !it.audio ) 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 const fullText = item.script || item.text try { const { filePath, duration } = await synthesize(fullText, { outputDir: audioDir, id: String(item.id || idx), voice: manifest.ttsVoice || undefined, instruction: manifest.ttsInstruction || undefined, rate: manifest.ttsRate || undefined, }) const totalDuration = Math.round(duration * 1000) / 1000 item.audio = path.relative(dir, filePath).replace(/\\/g, '/') item.audioDuration = totalDuration log('tts', `[${idx}/${items.length}] ${totalDuration.toFixed(1)}s: ${fullText.substring(0, 30)}...`) } catch (err) { item.status = 'failed' item.error = `TTS失败: ${err.message}` log('tts', `[${idx}/${items.length}] 失败: ${err.message}`) } saveManifest(manifestPath, manifest) } } module.exports = { phaseTts }