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
Improve single-line subtitles
wulien8b284b083594
diff --git a/config/config-example.toml b/config/config-example.tomlindex 9ab86024..c571a1e4 100644--- a/config/config-example.toml+++ b/config/config-example.toml@@ -4,6 +4,7 @@translate_parallel_num = 3 # 并发进行翻译的数量上限,建议值:3,倍于转录的并发量,如果使用TPM限制严格的API,可以适当调低transcribe_max_attempts = 3 # 转录最大尝试次数,建议值:3translate_max_attempts = 5 # 翻译最大尝试次数,建议值:5,如果模型参数量较少或翻译失败率较高可以适当调高+ max_sentence_length = 70 # 最大句子长度,超过这个长度的句子会被拆分,建议值:50-70proxy = "" # 网络代理地址,格式如http://127.0.0.1:7890,可不填[server]@@ -31,7 +32,7 @@[transcribe.whispercpp]model = "large-v2" # whispercpp的本地模型可选值:large-v2[transcribe.aliyun] # provider选aliyun这块就都要填- [tts.aliyun.oss]+ [transcribe.aliyun.oss]access_key_id = ""access_key_secret = ""bucket = ""diff --git a/config/config.go b/config/config.goindex 0d133db8..009c6d73 100644--- a/config/config.go+++ b/config/config.go@@ -21,6 +21,7 @@ type App struct {TranslateParallelNum int `toml:"translate_parallel_num"`TranscribeMaxAttempts int `toml:"transcribe_max_attempts"`TranslateMaxAttempts int `toml:"translate_max_attempts"`+ MaxSentenceLength int `toml:"max_sentence_length"`Proxy string `toml:"proxy"`ParsedProxy *url.URL `toml:"-"`}diff --git a/go.mod b/go.modindex 4aa0bafc..72049de2 100644--- a/go.mod+++ b/go.mod@@ -13,13 +13,13 @@ require (github.com/gorilla/websocket v1.5.0github.com/samber/lo v1.38.1github.com/sashabaranov/go-openai v1.36.0+ github.com/texttheater/golang-levenshtein v1.0.1go.uber.org/zap v1.25.0golang.org/x/sync v0.9.0)require (fyne.io/systray v1.11.0 // indirect- github.com/aliyun/alibabacloud-nls-go-sdk v1.1.1 // indirectgithub.com/bytedance/sonic v1.11.6 // indirectgithub.com/bytedance/sonic/loader v0.1.1 // indirectgithub.com/cloudwego/base64x v0.1.4 // indirect@@ -59,11 +59,9 @@ require (github.com/pelletier/go-toml/v2 v2.2.2 // indirectgithub.com/pmezard/go-difflib v1.0.0 // indirectgithub.com/rymdport/portal v0.3.0 // indirect- github.com/satori/go.uuid v1.2.0 // indirectgithub.com/srwiley/oksvg v0.0.0-20221011165216-be6e8873101c // indirectgithub.com/srwiley/rasterx v0.0.0-20220730225603-2ab79fcdd4ef // indirectgithub.com/stretchr/testify v1.9.0 // indirect- github.com/texttheater/golang-levenshtein v1.0.1 // indirectgithub.com/twitchyliquid64/golang-asm v0.15.1 // indirectgithub.com/ugorji/go/codec v1.2.12 // indirectgithub.com/yuin/goldmark v1.7.1 // indirectdiff --git a/go.sum b/go.sumindex 55a5de9f..99d51ab4 100644--- a/go.sum+++ b/go.sum@@ -47,11 +47,8 @@ github.com/BurntSushi/toml v1.4.0/go.mod h1:ukJfTF/6rtPPRCnwkur4qwRxa8vTRFBF0uk2github.com/BurntSushi/xgb v0.0.0-20160522181843-27f122750802/go.mod h1:IVnqGOEym/WlBOVXweHU+Q+/VP0lqqI8lqeDx9IjBqo=github.com/HdrHistogram/hdrhistogram-go v1.1.2/go.mod h1:yDgFjdqOqDEKOvasDdhWNXYg9BVp4O+o5f6V/ehm6Oo=github.com/ajstarks/svgo v0.0.0-20180226025133-644b8db467af/go.mod h1:K08gAheRH3/J6wwsYMMT4xOr94bZjxIelGM0+d/wbFw=-github.com/aliyun/alibaba-cloud-sdk-go v1.61.1376/go.mod h1:9CMdKNL3ynIGPpfTcdwTvIm8SGuAZYYC4jFVSSvE1YQ=github.com/aliyun/alibaba-cloud-sdk-go v1.63.72 h1:HvFZUzEbNvfe8F2Mg0wBGv90bPhWDxgVtDHR5zoBOU0=github.com/aliyun/alibaba-cloud-sdk-go v1.63.72/go.mod h1:SOSDHfe1kX91v3W5QiBsWSLqeLxImobbMX1mxrFHsVQ=-github.com/aliyun/alibabacloud-nls-go-sdk v1.1.1 h1:LjItoNZuu5xHlsByFo+kr3nGa4LRIESCGWhfurayxBg=-github.com/aliyun/alibabacloud-nls-go-sdk v1.1.1/go.mod h1:4BDMUKpEaP/Ct79w0ozR0nbnEj49g1k3mrgX/IKG5I4=github.com/aliyun/alibabacloud-oss-go-sdk-v2 v1.1.3 h1:grJyLSdRJtfxKKhCTWSeJhnOQsp2WoLNdK8XA5FE9oo=github.com/aliyun/alibabacloud-oss-go-sdk-v2 v1.1.3/go.mod h1:FTzydeQVmR24FI0D6XWUOMKckjXehM/jgMn1xC+DA9M=github.com/antihax/optional v1.0.0/go.mod h1:uupD/76wgC+ih3iEmQUL+0Ugr19nfwCT1kdvxnR2qWY=@@ -217,7 +214,6 @@ github.com/gopherjs/gopherjs v0.0.0-20181017120253-0766667cb4d1/go.mod h1:wJfORRgithub.com/gopherjs/gopherjs v0.0.0-20211219123610-ec9572f70e60/go.mod h1:cz9oNYuRUWGdHmLF2IodMLkAhcPtXeULvcBNagUrxTI=github.com/gopherjs/gopherjs v1.17.2 h1:fQnZVsXk8uxXIStYb0N4bGk7jeyTalG/wsZjQ25dO0g=github.com/gopherjs/gopherjs v1.17.2/go.mod h1:pRRIvn/QzFLrKfvEz3qUuEhtE/zLCWfreZ6J5gM2i+k=-github.com/gorilla/websocket v1.4.2/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=github.com/gorilla/websocket v1.5.0 h1:PPwGk2jz7EePpoHN/+ClbZu8SPxiqlu12wZP/3sWmnc=github.com/gorilla/websocket v1.5.0/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=github.com/goxjs/gl v0.0.0-20210104184919-e3fafc6f8f2a/go.mod h1:dy/f2gjY09hwVfIyATps4G2ai7/hLwLkc5TrPqONuXY=@@ -247,12 +243,10 @@ github.com/ianlancetaylor/demangle v0.0.0-20200824232613-28f6c0f3b639/go.mod h1:github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8=github.com/jeandeaual/go-locale v0.0.0-20240223122105-ce5225dcaa49 h1:Po+wkNdMmN+Zj1tDsJQy7mJlPlwGNQd9JZoPjObagf8=github.com/jeandeaual/go-locale v0.0.0-20240223122105-ce5225dcaa49/go.mod h1:YiutDnxPRLk5DLUFj6Rw4pRBBURZY07GFr54NdV9mQg=-github.com/jmespath/go-jmespath v0.0.0-20180206201540-c2b33e8439af/go.mod h1:Nht3zPeWKUH0NzdCt2Blrr5ys8VGpn0CEB0cQHVjt7k=github.com/jmespath/go-jmespath v0.4.0 h1:BEgLn5cpjn8UN1mAw4NjwDrS35OdebyEtFe+9YPoQUg=github.com/jmespath/go-jmespath v0.4.0/go.mod h1:T8mJZnbsbmF+m6zOOFylbeCJqk5+pHWvzYPziyZiYoo=github.com/jmespath/go-jmespath/internal/testify v1.5.1 h1:shLQSRRSCCPj3f2gpwzGwWFoC7ycTf1rcQZHOlsJ6N8=github.com/jmespath/go-jmespath/internal/testify v1.5.1/go.mod h1:L3OGu8Wl2/fWfCI6z80xFu9LTZmf1ZRjMHUOPmWr69U=-github.com/json-iterator/go v1.1.5/go.mod h1:+SdeFBvtyEkXs7REEP0seUULqWtbJapLOCVDaaPEHmU=github.com/json-iterator/go v1.1.11/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/uOdHXbAo4=github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM=github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo=@@ -336,8 +330,6 @@ github.com/samber/lo v1.38.1 h1:j2XEAqXKb09Am4ebOg31SpvzUTTs6EN3VfgeLUhPdXM=github.com/samber/lo v1.38.1/go.mod h1:+m/ZKRl6ClXCE2Lgf3MsQlWfh4bn1bz6CXEOxnEXnEA=github.com/sashabaranov/go-openai v1.36.0 h1:fcSrn8uGuorzPWCBp8L0aCR95Zjb/Dd+ZSML0YZy9EI=github.com/sashabaranov/go-openai v1.36.0/go.mod h1:lj5b/K+zjTSFxVLijLSTDZuP7adOgerWeFyZLUhAKRg=-github.com/satori/go.uuid v1.2.0 h1:0uYX9dsZ2yD7q2RtLRtPSdGDWzjeM3TbMJP9utgA0ww=-github.com/satori/go.uuid v1.2.0/go.mod h1:dA0hQrYB0VpLJoorglMZABFdXlWrHn1NEOzdhQKdks0=github.com/sean-/seed v0.0.0-20170313163322-e2103e2c3529/go.mod h1:DxrIzT+xaE7yg65j358z/aeFdxmN0P9QXhEzd20vsDc=github.com/shurcooL/go v0.0.0-20200502201357-93f07166e636/go.mod h1:TDJrrUr11Vxrven61rcy3hJMUqaf/CLWYhHNPmT14Lk=github.com/shurcooL/httpfs v0.0.0-20190707220628-8d4bc4ba7749/go.mod h1:ZY1cvUeJuFPAdZ/B6v7RHavJWZn2YPVFQ1OSXhCGOkg=@@ -776,7 +768,6 @@ gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNgopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q=gopkg.in/errgo.v2 v2.1.0/go.mod h1:hNsd1EY+bozCKY1Ytp96fpM3vjJbqLJn88ws8XvfDNI=gopkg.in/ini.v1 v1.62.0/go.mod h1:pNLf8WUiyNEtQjuu5G5vTm06TEv9tsIgeAvK8hOrP4k=-gopkg.in/ini.v1 v1.66.2/go.mod h1:pNLf8WUiyNEtQjuu5G5vTm06TEv9tsIgeAvK8hOrP4k=gopkg.in/ini.v1 v1.67.0 h1:Dgnx+6+nfE+IfzjUEISNeydPJh9AXNNsWbGP9KzCsOA=gopkg.in/ini.v1 v1.67.0/go.mod h1:pNLf8WUiyNEtQjuu5G5vTm06TEv9tsIgeAvK8hOrP4k=gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=diff --git a/internal/service/audio2subtitle.go b/internal/service/audio2subtitle.goindex 8a053920..c604957e 100644--- a/internal/service/audio2subtitle.go+++ b/internal/service/audio2subtitle.go@@ -175,6 +175,59 @@ func (s Service) splitTextAndTranslateV2(basePath, inputText string, originLang,sentences = newSentences}+ shortSentences := make([]string, 0)+ lastMerged := false+ //判断句子如果还是过长,就继续用大模型拆句+ for i, sentence := range sentences {+ // 如果前一次循环已经处理了当前句子(作为合并的一部分),跳过+ if lastMerged {+ lastMerged = false+ continue+ }++ // 如果当前句子小于10个字符,尝试与下一个句子合并+ if len(sentence) < 10 && i < len(sentences)-1 {+ // 与下一个句子合并+ mergedSentence := sentence + " " + sentences[i+1]+ if calcLength(mergedSentence) <= float64(config.Conf.App.MaxSentenceLength) {+ shortSentences = append(shortSentences, mergedSentence)+ // 标记下一个句子已处理,需要在外层循环中跳过+ lastMerged = true+ continue+ }+ }++ if calcLength(sentence) <= float64(config.Conf.App.MaxSentenceLength) {+ shortSentences = append(shortSentences, sentence)+ continue+ }++ // 调用大模型进行分割+ log.GetLogger().Info("use llm split origin long sentence", zap.Any("sentence", sentence))+ splitItems, err := s.splitOriginLongSentence(sentence)+ if err != nil {+ log.GetLogger().Error("splitTranslateItem splitLongSentence error", zap.Error(err), zap.Any("sentence", sentence))+ }+ //拆完之后还长,就再拆一次+ for _, item := range splitItems {+ if calcLength(item) <= float64(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...)+ }+ }++ sentences = shortSentences+var (signal = make(chan struct{}, config.Conf.App.TranslateParallelNum) // 控制最大并发数wg sync.WaitGroup@@ -1304,6 +1357,44 @@ func (s Service) splitLongSentence(item *TranslatedItem) ([]*TranslatedItem, errreturn splitItems, nil}+func (s Service) splitOriginLongSentence(sentence string) ([]string, error) {+ prompt := fmt.Sprintf(types.SplitOriginLongSentencePrompt, sentence, config.Conf.App.MaxSentenceLength)++ var response string+ var err error+ shortSentences := make([]string, 0)+ // 尝试调用3次+ for i := range 3 {+ response, err = s.ChatCompleter.ChatCompletion(prompt)+ if err != nil {+ log.GetLogger().Error("splitOriginLongSentence chat completion error", zap.Error(err), zap.String("sentence", sentence), zap.Any("time", i))+ continue+ }+ var splitResult struct {+ ShortSentences []struct {+ Text string `json:"text"`+ } `json:"short_sentences"`+ }++ cleanResponse := util.CleanMarkdownCodeBlock(response)+ if err = json.Unmarshal([]byte(cleanResponse), &splitResult); err != nil {+ log.GetLogger().Error("splitOriginLongSentence parse split result error", zap.Error(err), zap.Any("response", response))+ continue+ }++ for _, shortSentence := range splitResult.ShortSentences {+ shortSentences = append(shortSentences, shortSentence.Text)+ }+ break+ }++ if err != nil {+ return nil, fmt.Errorf("parse split result error: %w", err)+ }++ return shortSentences, nil+}+//func beautifyTranslateItems(language types.StandardLanguageCode, items []*TranslatedItem) {// if language != types.LanguageNameSimplifiedChinese && language != types.LanguageNameTraditionalChinese {// returndiff --git a/internal/service/audio2subtitle_test.go b/internal/service/audio2subtitle_test.goindex 22b48b0a..4a094fac 100644--- a/internal/service/audio2subtitle_test.go+++ b/internal/service/audio2subtitle_test.go@@ -1,8 +1,14 @@package serviceimport (+ "fmt"+ "krillin-ai/config"+ "krillin-ai/log""os""testing"++ "github.com/BurntSushi/toml"+ "go.uber.org/zap")func Test_isValidSplitContent(t *testing.T) {@@ -27,3 +33,41 @@ func Test_isValidSplitContent(t *testing.T) {t.Errorf("parseAndCheckContent() error = %v, want nil", err)}}++func loadTestConfig() bool {+ var err error+ configPath := "../../config/config.toml"+ if _, err = os.Stat(configPath); os.IsNotExist(err) {+ log.GetLogger().Info("未找到配置文件")+ return false+ } else {+ log.GetLogger().Info("已找到配置文件,从配置文件中加载配置")+ if _, err = toml.DecodeFile(configPath, &config.Conf); err != nil {+ log.GetLogger().Error("加载配置文件失败", zap.Error(err))+ return false+ }+ return true+ }+}++func initService() *Service {+ log.InitLogger()+ loadTestConfig()+ return NewService()+}++func Test_splitOriginLongSentence(t *testing.T) {+ // 固定的测试文件路径+ testText := "then one more thing is search for file count file explorer note count is the name of the plug in install it and once enabled you can see that now I can see how many files are in each are inside each individual folder even the nested folders are showing properly now how many files are in them"+ s := initService()+ // 执行测试+ splitTextSentences, err := s.splitOriginLongSentence(testText)+ if err != nil {+ t.Errorf("splitOriginLongSentence() error = %v, want nil", err)+ }++ fmt.Println("testText:", testText)+ for i, sentence := range splitTextSentences {+ fmt.Printf("Sentence %d: %s\n", i+1, sentence)+ }+}diff --git a/internal/types/subtitle_task.go b/internal/types/subtitle_task.goindex c74bb3ad..b6844d9b 100644--- a/internal/types/subtitle_task.go+++ b/internal/types/subtitle_task.go@@ -129,6 +129,19 @@ 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 each part does not exceed the character limit:++Original text: %s++Requirements:+1. The split sentences must exactly match the original text, absolutely no changes to the original text are allowed+2. Split based on sentence meaning, keeping each part under %d characters+3. Return in JSON format only, no other descriptions or explanations+4. Example format:+{"short_sentences":[{"text": "split sentence 1"},{"text": "split sentence 2"}]}++`+var SplitTextWithContextPrompt = `你是一个专业翻译专家,擅长结合上下文进行准确翻译。请根据以下提供的上下文句子和目标句子,将目标句子翻译成%s,并确保翻译结果与上下文保持连贯一致:上下文句子: