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.

Improve short subtitles

wulien15b26982b99d
diff --git a/internal/service/audio2subtitle.go b/internal/service/audio2subtitle.go
index c604957e..6abc8ccc 100644
--- a/internal/service/audio2subtitle.go
+++ b/internal/service/audio2subtitle.go
@@ -232,8 +232,8 @@ func (s Service) splitTextAndTranslateV2(basePath, inputText string, originLang,
signal = make(chan struct{}, config.Conf.App.TranslateParallelNum) // 控制最大并发数
wg sync.WaitGroup
results = make([]*TranslatedItem, len(sentences))
- errChan = make(chan error, 1)
- mutex sync.Mutex
+ // errChan = make(chan error, 1)
+ // mutex sync.Mutex
)
for i, sentence := range sentences {
@@ -244,53 +244,56 @@ func (s Service) splitTextAndTranslateV2(basePath, inputText string, originLang,
defer wg.Done()
defer func() { <-signal }()
- // 准备上下文(前后各5句)
- var ctxBuilder strings.Builder
- start := max(0, index-5)
- end := min(len(sentences)-1, index+5)
+ contextSentenceNum := 3
+
+ // 生成前面3个句子的string
+ var previousSentences string
if index > 0 {
- ctxBuilder.WriteString("上文:\n")
+ start := 0
+ if index-contextSentenceNum > 0 {
+ start = index - contextSentenceNum
+ }
for i := start; i < index; i++ {
- ctxBuilder.WriteString(fmt.Sprintf("%s", sentences[i]))
+ previousSentences += sentences[i] + "\n"
}
}
+
+ // 生成后面3个句子的string
+ var nextSentences string
if index < len(sentences)-1 {
- ctxBuilder.WriteString("下文:\n")
+ end := len(sentences) - 1
+ if index+contextSentenceNum < end {
+ end = index + contextSentenceNum
+ }
for i := index + 1; i <= end; i++ {
- ctxBuilder.WriteString(fmt.Sprintf("%s", sentences[i]))
+ if i > index+1 {
+ nextSentences += "\n"
+ }
+ nextSentences += sentences[i]
}
}
- ctx := ctxBuilder.String()
- prompt := fmt.Sprintf(types.SplitTextWithContextPrompt, types.GetStandardLanguageName(targetLang), ctx, originText)
+ prompt := fmt.Sprintf(types.SplitTextWithContextPrompt, types.GetStandardLanguageName(targetLang), previousSentences, originText, nextSentences)
translatedText, err := s.ChatCompleter.ChatCompletion(prompt)
if err != nil {
- mutex.Lock()
- select {
- case errChan <- fmt.Errorf("splitTextAndTranslateV2 llm translate error: %w, original text: %s", err, originText):
- default:
+ log.GetLogger().Error("splitTextAndTranslateV2 llm translate error", zap.Error(err), zap.Any("original text", originText))
+ results[index] = &TranslatedItem{
+ OriginText: originText,
+ TranslatedText: originText,
+ }
+ } else {
+ translatedText = strings.TrimSpace(translatedText)
+ results[index] = &TranslatedItem{
+ OriginText: originText,
+ TranslatedText: translatedText,
}
- mutex.Unlock()
- return
- }
-
- translatedText = strings.TrimSpace(translatedText)
- results[index] = &TranslatedItem{
- OriginText: originText,
- TranslatedText: translatedText,
}
}(i, sentence)
}
- go func() {
- wg.Wait()
- close(errChan)
- }()
-
- if err := <-errChan; err != nil {
- return nil, err
- }
+ wg.Wait()
+ // close(errChan)
return results, nil
}
diff --git a/internal/types/subtitle_task.go b/internal/types/subtitle_task.go
index b6844d9b..bddfae68 100644
--- a/internal/types/subtitle_task.go
+++ b/internal/types/subtitle_task.go
@@ -142,20 +142,65 @@ Requirements:
`
-var SplitTextWithContextPrompt = `你是一个专业翻译专家,擅长结合上下文进行准确翻译。请根据以下提供的上下文句子和目标句子,将目标句子翻译成%s,并确保翻译结果与上下文保持连贯一致:
+// var SplitTextWithContextPrompt = `你是一个专业翻译专家,擅长结合上下文进行准确翻译。请根据以下提供的上下文句子和目标句子,将目标句子翻译成%s,并确保翻译结果与上下文保持连贯一致:
-上下文句子:
+// 上下文句子:
+// %s
+
+// 需要翻译的目标句子:%s
+
+// 翻译要求:
+// 1. 严格按照目标语言的语法和表达习惯翻译
+// 2. 保持专业术语的一致性
+// 3. 输出仅包含翻译后的文本,不添加任何额外解释或格式
+// 4. 确保翻译结果与上下文语义连贯
+
+// 请直接输出翻译结果:`
+
+// var SplitTextWithContextPrompt = `You are a professional translation expert skilled in providing accurate translations based on context. Please translate the target sentence into %s according to the provided context sentences below, ensuring the translation remains coherent and consistent.
+
+// Here's the full context:
+// %s
+
+// Target sentence to translate:
+// %s
+
+// %s
+
+// Translation requirements:
+// 1.Analyze how the target sentence connects to both the preceding and following context
+// 2.Provide the most natural translation that preserves the original tone and intent
+// 3.Highlight any idioms, cultural references, or nuanced phrases that require special attention
+// 4.If there are multiple possible interpretations, briefly explain each option
+// 5.Maintain consistent terminology with the surrounding sentences"
+// 6.Output only the translated text without any additional explanations or formatting
+
+// Please provide only the translation result:`
+
+var SplitTextWithContextPrompt = `You are a professional translation expert.
+
+[STRICT TRANSLATION TASK]
+**Objective**:
+Translate ONLY the "Target Sentence" below into %s.
+Must reference but NEVER translate surrounding context.
+
+**Critical Rules**:
+1. OUTPUT MUST BE A SINGLE LINE containing ONLY the target sentence's translation
+2. NEVER continue/complete the target sentence's thought
+3. IGNORE all "Next Sentences" completely
+4. Maintain contextual flow from "Previous Sentences" implicitly
+
+**Context**:
+[Previous Sentences]
%s
-需要翻译的目标句子:%s
+[Target Sentence]
+%s
-翻译要求:
-1. 严格按照目标语言的语法和表达习惯翻译
-2. 保持专业术语的一致性
-3. 输出仅包含翻译后的文本,不添加任何额外解释或格式
-4. 确保翻译结果与上下文语义连贯
+[Next Sentences]
+%s
-请直接输出翻译结果:`
+**Provide only the translation result:**`
type SmallAudio struct {
AudioFile string