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.

fix: generate Thai short subtitles

MijaelX89b2840f3b4c
diff --git a/runtime/krillinai/internal/service/audio2subtitle.go b/runtime/krillinai/internal/service/audio2subtitle.go
index f899bac7..44bf51c9 100644
--- a/runtime/krillinai/internal/service/audio2subtitle.go
+++ b/runtime/krillinai/internal/service/audio2subtitle.go
@@ -1526,11 +1526,23 @@ func generateSrtWithTimestamps(srtBlocks []*util.SrtBlock, tsOffset float64, wor
srtBlocks[index].Timestamp = newSrtBlocks[index].Timestamp
}
- for _, srtBlock := range srtBlocks {
+ for blockIndex, srtBlock := range srtBlocks {
if srtBlock.OriginLanguageSentence == "" {
continue
}
- sentenceTs, sentenceWords, ts, err := getSentenceTimestamps(words, srtBlock.OriginLanguageSentence, lastTs, stepParam.OriginLanguage)
+ var sentenceTs types.SrtSentence
+ var sentenceWords []types.Word
+ var ts float64
+ if stepParam.OriginLanguage == types.LanguageNameThai {
+ sentenceTs, sentenceWords, ts, err = getSentenceTimestampsFromGeneratedBlock(
+ words,
+ newSrtBlocks[blockIndex].Timestamp,
+ lastTs,
+ tsOffset,
+ )
+ } else {
+ sentenceTs, sentenceWords, ts, err = getSentenceTimestamps(words, srtBlock.OriginLanguageSentence, lastTs, stepParam.OriginLanguage)
+ }
if err != nil || ts < lastTs {
continue
}
@@ -1702,6 +1714,39 @@ func generateSrtWithTimestamps(srtBlocks []*util.SrtBlock, tsOffset float64, wor
return nil
}
+func getSentenceTimestampsFromGeneratedBlock(
+ words []types.Word,
+ timestamp string,
+ lastTs float64,
+ tsOffset float64,
+) (types.SrtSentence, []types.Word, float64, error) {
+ startMillis, endMillis, err := parseSRTTimeline(timestamp)
+ if err != nil {
+ return types.SrtSentence{}, nil, 0, err
+ }
+ start := float64(startMillis)/1000 - tsOffset
+ end := float64(endMillis)/1000 - tsOffset
+ if start < lastTs {
+ start = lastTs
+ }
+ if end <= start {
+ return types.SrtSentence{}, nil, 0, fmt.Errorf("generated subtitle timestamp is not increasing: %s", timestamp)
+ }
+
+ matchedWords := make([]types.Word, 0)
+ for _, word := range words {
+ if strings.TrimSpace(word.Text) == "" || word.End <= start || word.Start >= end {
+ continue
+ }
+ matchedWords = append(matchedWords, word)
+ }
+ if len(matchedWords) == 0 {
+ return types.SrtSentence{}, nil, 0, fmt.Errorf("generated subtitle timestamp has no matching words: %s", timestamp)
+ }
+
+ return types.SrtSentence{Start: start, End: end}, matchedWords, end, nil
+}
+
func parseAndCheckContent(splitContent, originalText string) ([]*TranslatedItem, error) {
var result []*TranslatedItem
diff --git a/runtime/krillinai/internal/service/audio2subtitle_test.go b/runtime/krillinai/internal/service/audio2subtitle_test.go
index eb49c0fc..89e04fa1 100644
--- a/runtime/krillinai/internal/service/audio2subtitle_test.go
+++ b/runtime/krillinai/internal/service/audio2subtitle_test.go
@@ -8,6 +8,7 @@ import (
"krillin-ai/config"
"krillin-ai/internal/types"
"krillin-ai/log"
+ "krillin-ai/pkg/util"
"os"
"path/filepath"
"strings"
@@ -23,6 +24,42 @@ func TestThaiUsesNonSpaceTextSplitting(t *testing.T) {
}
}
+func TestGenerateSrtWithTimestampsWritesThaiShortSubtitle(t *testing.T) {
+ dir := t.TempDir()
+ blocks := []*util.SrtBlock{
+ {
+ Index: 1,
+ OriginLanguageSentence: "สวัสดี",
+ TargetLanguageSentence: "你好",
+ },
+ }
+ words := []types.Word{
+ {Num: 0, Text: "สวัสดี", Start: 0, End: 0.447},
+ }
+ stepParam := &types.SubtitleTaskStepParam{
+ TaskBasePath: dir,
+ OriginLanguage: types.LanguageNameThai,
+ MaxWordOneLine: 10,
+ SubtitleResultType: types.SubtitleResultTypeBilingualTranslationOnTop,
+ }
+
+ if err := generateSrtWithTimestamps(blocks, 0, words, 0, stepParam); err != nil {
+ t.Fatal(err)
+ }
+
+ shortPath := filepath.Join(dir, fmt.Sprintf(types.SubtitleTaskSplitShortOriginSrtFileNamePattern, 0))
+ if err := NormalizeSRTFile(shortPath, false); err != nil {
+ t.Fatalf("generated Thai short subtitle is invalid: %v", err)
+ }
+ content, err := os.ReadFile(shortPath)
+ if err != nil {
+ t.Fatal(err)
+ }
+ if !strings.Contains(string(content), "สวัสดี") {
+ t.Fatalf("Thai short subtitle does not contain source text: %q", string(content))
+ }
+}
+
func Test_isValidSplitContent(t *testing.T) {
dir := t.TempDir()
splitContentFile := filepath.Join(dir, "srt_no_ts_1.srt")