refactor(video-pipeline): 移除 segments 机制,改为整段音频合成
移除 TTS 阶段逐句切分及 segments 数组逻辑,统一为整段音频合成。 CapCut 字幕切分由组装阶段按字符比例分配,简化音频上传、 时间线构建和字幕生成流程,减少冗余处理分支。
This commit is contained in:
@@ -308,7 +308,7 @@ async function addVideos(draftUrl, inputDir, items, timeline, width, height, tra
|
||||
// ============================================================================
|
||||
|
||||
async function addVoiceover(draftUrl, inputDir, items, timeline, audioUrls = {}) {
|
||||
const audioItems = items.filter(item => item.audio || (item.segments && item.segments.length > 0))
|
||||
const audioItems = items.filter(item => item.audio)
|
||||
if (audioItems.length === 0) {
|
||||
console.log(' 无 TTS 音频文件,跳过')
|
||||
return
|
||||
@@ -325,25 +325,7 @@ async function addVoiceover(draftUrl, inputDir, items, timeline, audioUrls = {})
|
||||
const item = items[i]
|
||||
const tl = timeline[i]
|
||||
|
||||
if (item.segments && item.segments.length > 0) {
|
||||
let currentTime = tl.start
|
||||
for (let si = 0; si < item.segments.length; si++) {
|
||||
const seg = item.segments[si]
|
||||
const audioUrl = resolveAudio(seg.audio)
|
||||
const segDurUs = (seg.duration || 0) * US
|
||||
if (segDurUs <= 0) continue
|
||||
const isLast = si === item.segments.length - 1
|
||||
const endTime = isLast ? tl.end : currentTime + segDurUs
|
||||
audioInfos.push({
|
||||
audio_url: audioUrl,
|
||||
start: currentTime,
|
||||
end: endTime,
|
||||
duration: endTime - currentTime,
|
||||
volume: 1.0,
|
||||
})
|
||||
currentTime = endTime
|
||||
}
|
||||
} else if (item.audio) {
|
||||
if (item.audio) {
|
||||
const audioUrl = resolveAudio(item.audio)
|
||||
const audioDurUs = item.audioDuration ? item.audioDuration * US : tl.duration
|
||||
|
||||
@@ -421,48 +403,33 @@ async function addSubtitles(draftUrl, items, timeline, style = {}, split = false
|
||||
const tl = timeline[i]
|
||||
|
||||
if (split) {
|
||||
if (item.segments && item.segments.length > 0) {
|
||||
let currentTime = tl.start
|
||||
for (let si = 0; si < item.segments.length; si++) {
|
||||
const seg = item.segments[si]
|
||||
const segDurUs = (seg.duration || 0) * US
|
||||
if (segDurUs <= 0) continue
|
||||
const isLast = si === item.segments.length - 1
|
||||
const endTime = isLast ? tl.end : currentTime + segDurUs
|
||||
const cap = { start: currentTime, end: endTime, text: seg.text }
|
||||
applyAnimationProps(cap, animStyle)
|
||||
captions.push(cap)
|
||||
currentTime = endTime
|
||||
const sentences = splitTextIntoSentences(text)
|
||||
if (sentences.length === 0) continue
|
||||
|
||||
const totalDuration = tl.end - tl.start
|
||||
const totalChars = sentences.reduce((sum, s) => sum + s.length, 0)
|
||||
let currentTime = tl.start
|
||||
|
||||
sentences.forEach((sentence, idx) => {
|
||||
const charRatio = sentence.length / totalChars
|
||||
let duration = Math.round(totalDuration * charRatio)
|
||||
|
||||
if (idx === sentences.length - 1) {
|
||||
duration = tl.end - currentTime
|
||||
}
|
||||
} else {
|
||||
const sentences = splitTextIntoSentences(text)
|
||||
if (sentences.length === 0) continue
|
||||
|
||||
const totalDuration = tl.end - tl.start
|
||||
const totalChars = sentences.reduce((sum, s) => sum + s.length, 0)
|
||||
let currentTime = tl.start
|
||||
duration = Math.max(duration, 500000)
|
||||
|
||||
sentences.forEach((sentence, idx) => {
|
||||
const charRatio = sentence.length / totalChars
|
||||
let duration = Math.round(totalDuration * charRatio)
|
||||
const cap = {
|
||||
start: currentTime,
|
||||
end: currentTime + duration,
|
||||
text: sentence,
|
||||
}
|
||||
|
||||
if (idx === sentences.length - 1) {
|
||||
duration = tl.end - currentTime
|
||||
}
|
||||
|
||||
duration = Math.max(duration, 500000)
|
||||
|
||||
const cap = {
|
||||
start: currentTime,
|
||||
end: currentTime + duration,
|
||||
text: sentence,
|
||||
}
|
||||
|
||||
applyAnimationProps(cap, animStyle)
|
||||
captions.push(cap)
|
||||
currentTime += duration
|
||||
})
|
||||
}
|
||||
applyAnimationProps(cap, animStyle)
|
||||
captions.push(cap)
|
||||
currentTime += duration
|
||||
})
|
||||
} else {
|
||||
const cap = {
|
||||
start: tl.start,
|
||||
|
||||
Reference in New Issue
Block a user