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.
TypeScript agent-skillscodex-clideskop-appdubbingimage-generationlocalizationskillsttsvideo-editingvideo-generationvideo-transcriptionvideo-translation
Fix splitting of long text
wulien917afc0a4ca6
diff --git a/internal/service/audio2subtitle.go b/internal/service/audio2subtitle.goindex ebde1ff1..b7184720 100644--- a/internal/service/audio2subtitle.go+++ b/internal/service/audio2subtitle.go@@ -147,27 +147,14 @@ func (s Service) splitTextAndTranslateV2(basePath, inputText string, originLang,continue}- // 调用大模型进行分割- log.GetLogger().Info("use llm split origin long sentence", zap.Any("sentence", sentence))- splitItems, err := s.splitOriginLongSentence(sentence)+ // 递归拆分长句子直到满足长度要求,保持顺序+ splitSentences, err := s.splitSentenceRecursively(sentence, 0, 5) // 最多5层递归if err != nil {- log.GetLogger().Error("splitTranslateItem splitLongSentence error", zap.Error(err), zap.Any("sentence", sentence))- }- //拆完之后还长,就再拆一次- for _, item := range splitItems {- if util.CountEffectiveChars(item) <= config.Conf.App.MaxSentenceLength {- shortSentences = append(shortSentences, item)- continue- }-- // 调用大模型进行分割- log.GetLogger().Info("use llm split origin long sentence", zap.Any("item", item))- splitItems, err := s.splitOriginLongSentence(item)- if err != nil {- log.GetLogger().Error("splitTranslateItem splitLongSentence error", zap.Error(err), zap.Any("item", item))- }-- shortSentences = append(shortSentences, splitItems...)+ log.GetLogger().Error("splitSentenceRecursively error", zap.Error(err), zap.Any("sentence", sentence))+ // 如果拆分失败,直接添加原句子+ shortSentences = append(shortSentences, sentence)+ } else {+ shortSentences = append(shortSentences, splitSentences...)}}@@ -1312,6 +1299,9 @@ func (s Service) splitLongSentence(item *TranslatedItem) ([]*TranslatedItem, errfunc (s Service) splitOriginLongSentence(sentence string) ([]string, error) {prompt := fmt.Sprintf(types.SplitOriginLongSentencePrompt, sentence)+ if len(sentence) > 200 {+ prompt = fmt.Sprintf(types.SplitLongTextByMeaningPrompt, sentence)+ }var response stringvar err error@@ -1348,6 +1338,47 @@ func (s Service) splitOriginLongSentence(sentence string) ([]string, error) {return shortSentences, nil}+// splitSentenceRecursively 递归拆分句子,保持顺序+func (s Service) splitSentenceRecursively(sentence string, depth int, maxDepth int) ([]string, error) {+ // 防止无限递归+ if depth >= maxDepth {+ log.GetLogger().Warn("reached max split depth", zap.Any("sentence", sentence), zap.Int("depth", depth))+ return []string{sentence}, nil+ }++ // 如果句子已经满足长度要求,直接返回+ if util.CountEffectiveChars(sentence) <= config.Conf.App.MaxSentenceLength {+ return []string{sentence}, nil+ }++ // 调用大模型进行分割+ log.GetLogger().Info("use llm split origin long sentence", zap.Any("sentence", sentence), zap.Int("depth", depth))+ splitItems, err := s.splitOriginLongSentence(sentence)+ if err != nil {+ log.GetLogger().Error("splitSentenceRecursively splitLongSentence error", zap.Error(err), zap.Any("sentence", sentence), zap.Int("depth", depth))+ return []string{sentence}, nil // 返回原句子而不是错误+ }++ // 如果没有拆分出多个部分,返回原句子+ if len(splitItems) <= 1 {+ return []string{sentence}, nil+ }++ // 递归处理每个拆分结果,保持顺序+ var result []string+ for _, item := range splitItems {+ subResults, err := s.splitSentenceRecursively(item, depth+1, maxDepth)+ if err != nil {+ log.GetLogger().Error("splitSentenceRecursively recursive error", zap.Error(err), zap.Any("item", item), zap.Int("depth", depth))+ result = append(result, item) // 如果递归失败,添加原项+ } else {+ result = append(result, subResults...)+ }+ }++ return result, nil+}+//func beautifyTranslateItems(language types.StandardLanguageCode, items []*TranslatedItem) {// if language != types.LanguageNameSimplifiedChinese && language != types.LanguageNameTraditionalChinese {// returndiff --git a/internal/service/link2file.go b/internal/service/link2file.goindex f7f994e9..200d4cd1 100644--- a/internal/service/link2file.go+++ b/internal/service/link2file.go@@ -4,7 +4,6 @@ import ("context""errors""fmt"- "go.uber.org/zap""krillin-ai/config""krillin-ai/internal/storage""krillin-ai/internal/types"@@ -12,6 +11,8 @@ import ("krillin-ai/pkg/util""os/exec""strings"++ "go.uber.org/zap")func (s Service) linkToFile(ctx context.Context, stepParam *types.SubtitleTaskStepParam) error {@@ -40,7 +41,15 @@ func (s Service) linkToFile(ctx context.Context, stepParam *types.SubtitleTaskStreturn fmt.Errorf("linkToFile.GetYouTubeID error: %w", err)}stepParam.Link = "https://www.youtube.com/watch?v=" + videoId- cmdArgs := []string{"-f", "bestaudio", "--extract-audio", "--audio-format", "mp3", "--audio-quality", "192K", "-o", audioPath, stepParam.Link}+ // 使用更灵活的音频格式选择器,避免 HTTP 403 错误+ cmdArgs := []string{+ "-f", "bestaudio[ext=m4a]/bestaudio[ext=mp3]/bestaudio/worst",+ "--extract-audio",+ "--audio-format", "mp3",+ "--audio-quality", "192K",+ "-o", audioPath,+ stepParam.Link,+ }if config.Conf.App.Proxy != "" {cmdArgs = append(cmdArgs, "--proxy", config.Conf.App.Proxy)}diff --git a/internal/types/subtitle_task.go b/internal/types/subtitle_task.goindex 2518bd63..7d3ab102 100644--- a/internal/types/subtitle_task.go+++ b/internal/types/subtitle_task.go@@ -129,7 +129,7 @@ var SplitLongSentencePrompt = `请将以下原文和译文分割成多个部分4. 务必返回JSON格式,包含origin_part和translated_part数组,例如:{"align":[{"origin_part":"原文部分1","translated_part":"译文部分1"},{"origin_part":"原文部分2","translated_part":"译文部分2"}]}`-var SplitOriginLongSentencePrompt = `Please split the following text into multiple parts, ensuring it's divided into at most 3 short sentences, preferably 2:+var SplitOriginLongSentencePrompt = `Please split the following text into multiple parts, ensuring it's divided into at most 3 short sentences, preferably 2 parts,Original text: %s@@ -143,6 +143,29 @@ Requirements:`+var SplitLongTextByMeaningPrompt = `Please split the following long text into shorter sentences based on semantic meaning. Do not change, add, or remove any words from the original text.++Original text: %s++Requirements:+1. Split the text into as many shorter, meaningful sentences as possible while preserving ALL original words+2. Do NOT change, modify, add, or remove any words - only split at natural breakpoints+3. Split at natural linguistic boundaries such as:+ - Punctuation marks (commas, semicolons, periods)+ - Conjunctions (and, but, or, so, because, when, while, etc.)+ - Relative pronouns (which, that, who, where, etc.)+ - Natural pause points that maintain sentence meaning+4. Each split part should be a complete, meaningful unit that can stand alone+5. Prioritize shorter segments - split as much as possible while maintaining semantic integrity+6. No limit on the number of splits - make each part as short as possible while still being meaningful+7. Maintain the original word order and exact spelling+8. Preserve all original punctuation and capitalization+9. Return in JSON format only, no other descriptions or explanations+10. Example format:+{"short_sentences":[{"text": "first short part"},{"text": "second short part"},{"text": "third short part"}]}++`+// var SplitTextWithContextPrompt = `你是一个专业翻译专家,擅长结合上下文进行准确翻译。请根据以下提供的上下文句子和目标句子,将目标句子翻译成%s,并确保翻译结果与上下文保持连贯一致:// 上下文句子: