Skip to content

Formerly KrillinAI, VideoTranslation and Dubbing Tool for 120+ Languages. Open-source AI workspace for creators, powered by Codex. Video downloader, video translation, create videos, images, audios, and edits with Agents in one place. For your Social Media like YouTube, TikTok, Instagram, etc.

feat: add findVttWordsForSrtBlock to locate VTT words for SRT blocks

wulienb5244c1aa5fc
diff --git a/internal/service/youtube_subtitle.go b/internal/service/youtube_subtitle.go
index 066523ff..b3697fc4 100644
--- a/internal/service/youtube_subtitle.go
+++ b/internal/service/youtube_subtitle.go
@@ -2404,6 +2404,55 @@ func (s *YouTubeSubtitleService) findVttWordsForText(text string, sentences []Se
return []VttWord{}
}
+// findVttWordsForSrtBlock 根据 SRT 块找到对应的 VTT 单词序列
+func (s *YouTubeSubtitleService) findVttWordsForSrtBlock(
+ srtBlock *util.SrtBlock,
+ sentences []Sentence,
+) []VttWord {
+ if srtBlock.OriginLanguageSentence == "" {
+ return []VttWord{}
+ }
+
+ // 清理文本
+ originText := strings.TrimSpace(srtBlock.OriginLanguageSentence)
+ originText = strings.Trim(originText, `"'`)
+ expectedWords := strings.Fields(originText)
+
+ if len(expectedWords) == 0 {
+ return []VttWord{}
+ }
+
+ // 在所有句子中查找匹配的单词序列
+ for _, sentence := range sentences {
+ if len(sentence.Words) < len(expectedWords) {
+ continue
+ }
+
+ // 尝试匹配
+ for i := 0; i <= len(sentence.Words)-len(expectedWords); i++ {
+ match := true
+ for j, expectedWord := range expectedWords {
+ actualWord := strings.TrimSpace(sentence.Words[i+j].Text)
+ expectedClean := strings.Trim(expectedWord, ".,!?;:")
+ actualClean := strings.Trim(actualWord, ".,!?;:")
+
+ if !strings.EqualFold(expectedClean, actualClean) {
+ match = false
+ break
+ }
+ }
+
+ if match {
+ return sentence.Words[i : i+len(expectedWords)]
+ }
+ }
+ }
+
+ log.GetLogger().Debug("No VTT words found for SRT block",
+ zap.String("originText", originText))
+ return []VttWord{}
+}
+
// getAllWordsFromSentences 从所有句子中获取所有单词的扁平列表
func (s *YouTubeSubtitleService) getAllWordsFromSentences(sentences []Sentence) []VttWord {
var allWords []VttWord