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.

optimize subtitle

wulienc08dece2b561
diff --git a/internal/service/audio2subtitle.go b/internal/service/audio2subtitle.go
index 74ca4e20..36816684 100644
--- a/internal/service/audio2subtitle.go
+++ b/internal/service/audio2subtitle.go
@@ -626,7 +626,7 @@ func (s Service) generateTimestamps(taskId, basePath string, originLanguage type
if err != nil || ts < lastTs {
continue
}
- lastTs = ts
+
tsOffset := float64(config.Conf.App.SegmentDuration) * 60 * float64(audioFile.Num-1)
srtBlock.Timestamp = fmt.Sprintf("%s --> %s", util.FormatTime(float32(sentenceTs.Start+tsOffset)), util.FormatTime(float32(sentenceTs.End+tsOffset)))
@@ -643,17 +643,19 @@ func (s Service) generateTimestamps(taskId, basePath string, originLanguage type
Timestamp: fmt.Sprintf("%s --> %s", util.FormatTime(float32(sentenceTs.Start+tsOffset)), util.FormatTime(float32(sentenceTs.End+tsOffset))),
OriginLanguageSentence: srtBlock.OriginLanguageSentence,
})
+ lastTs = ts
continue
}
- if len(sentenceWords) > 8 && len(sentenceWords) <= 2*originLanguageWordOneLine {
- originLanguageWordOneLine = len(sentenceWords)/2 + 1
+ thisLineWord := originLanguageWordOneLine
+ if len(sentenceWords) > originLanguageWordOneLine && len(sentenceWords) <= 2*originLanguageWordOneLine {
+ thisLineWord = len(sentenceWords)/2 + 1
} else if len(sentenceWords) > 2*originLanguageWordOneLine && len(sentenceWords) <= 3*originLanguageWordOneLine {
- originLanguageWordOneLine = len(sentenceWords)/3 + 1
+ thisLineWord = len(sentenceWords)/3 + 1
} else if len(sentenceWords) > 3*originLanguageWordOneLine && len(sentenceWords) <= 4*originLanguageWordOneLine {
- originLanguageWordOneLine = len(sentenceWords)/4 + 1
+ thisLineWord = len(sentenceWords)/4 + 1
} else if len(sentenceWords) > 4*originLanguageWordOneLine && len(sentenceWords) <= 5*originLanguageWordOneLine {
- originLanguageWordOneLine = len(sentenceWords)/5 + 1
+ thisLineWord = len(sentenceWords)/5 + 1
}
i := 1
@@ -661,6 +663,9 @@ func (s Service) generateTimestamps(taskId, basePath string, originLanguage type
for _, word := range sentenceWords {
if nextStart {
startWord = word
+ if startWord.Start < lastTs {
+ startWord.Start = lastTs
+ }
if startWord.Start < endWord.End {
startWord.Start = endWord.End
}
@@ -684,7 +689,7 @@ func (s Service) generateTimestamps(taskId, basePath string, originLanguage type
endWord.End = sentenceTs.End
}
- if i%originLanguageWordOneLine == 0 && i > 1 {
+ if i%thisLineWord == 0 && i > 1 {
shortOriginSrtMap[srtBlock.Index] = append(shortOriginSrtMap[srtBlock.Index], util.SrtBlock{
Index: srtBlock.Index,
Timestamp: fmt.Sprintf("%s --> %s", util.FormatTime(float32(startWord.Start+tsOffset)), util.FormatTime(float32(endWord.End+tsOffset))),
@@ -703,6 +708,7 @@ func (s Service) generateTimestamps(taskId, basePath string, originLanguage type
OriginLanguageSentence: originSentence,
})
}
+ lastTs = ts
}
// 保存带时间戳的原始字幕
diff --git a/pkg/util/subtitle.go b/pkg/util/subtitle.go
index 47d5b6ab..238c9562 100644
--- a/pkg/util/subtitle.go
+++ b/pkg/util/subtitle.go
@@ -98,6 +98,9 @@ func TrimString(s string) string {
// 去除结尾的空格和 ']'
s = strings.TrimRight(s, " ]")
+ //替换中文单引号
+ s = strings.ReplaceAll(s, "’", "'")
+
return s
}