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
feat: add edge-tts support and improve speech settings, dependency checks, and concurrency
puji4810541fb61b1539
diff --git a/internal/deps/checker.go b/internal/deps/checker.goindex cb373d37..e7e37ace 100644--- a/internal/deps/checker.go+++ b/internal/deps/checker.go@@ -76,6 +76,11 @@ func CheckDependency() error {return err}}+ if config.Conf.Tts.Provider == "edge-tts" {+ if err = checkEdgeTts(); err != nil {+ log.GetLogger().Error("edge-tts环境准备失败", zap.Error(err))+ }+ }return nil}@@ -555,3 +560,51 @@ func checkModel(whisperType string) error {log.GetLogger().Info("模型检查完成", zap.String("路径", modelPath))return nil}++func checkEdgeTts() error {+ // 检查edge-tts是否已经安装+ _, err := exec.LookPath("edge-tts")+ if err == nil {+ log.GetLogger().Info("已找到edge-tts")+ storage.EdgeTtsPath = "edge-tts"+ return nil+ }++ EdgeTtsBinFilePath := "./bin/edge-tts"+ if runtime.GOOS == "windows" {+ EdgeTtsBinFilePath += ".exe"+ }+ // 先前下载过的+ if _, err = os.Stat(EdgeTtsBinFilePath); err == nil {+ log.GetLogger().Info("已找到edge-tts")+ storage.EdgeTtsPath = EdgeTtsBinFilePath+ return nil+ }+ log.GetLogger().Info("没有找到edge-tts,即将开始自动安装")+ // 确保./bin目录存在+ err = os.MkdirAll("./bin", 0755)+ if err != nil {+ log.GetLogger().Error("创建./bin目录失败", zap.Error(err))+ }++ var downloadUrl string+ if runtime.GOOS == "windows" {+ downloadUrl = "https://github.com/puji4810/edge-tts-pkg/releases/download/v0.0.1/edge-tts-windows.exe"+ } else if runtime.GOOS == "linux" {+ downloadUrl = "https://github.com/puji4810/edge-tts-pkg/releases/download/v0.0.1/edge-tts-linux"+ } else if runtime.GOOS == "darwin" {+ downloadUrl = "https://github.com/puji4810/edge-tts-pkg/releases/download/v0.0.1/edge-tts-macos"+ }else {+ log.GetLogger().Error("不支持你当前的操作系统", zap.String("当前系统", runtime.GOOS))+ return fmt.Errorf("unsupported OS: %s", runtime.GOOS)+ }+ edgettsDownloadPath := "./bin/edge-tts"+ err = util.DownloadFile(downloadUrl, edgettsDownloadPath, config.Conf.App.Proxy)+ if err != nil {+ log.GetLogger().Error("下载edge-tts失败", zap.Error(err))+ return err+ }+ storage.EdgeTtsPath = EdgeTtsBinFilePath+ log.GetLogger().Info("edge-tts安装完成", zap.String("路径", EdgeTtsBinFilePath))+ return nil+}diff --git a/internal/desktop/ui.go b/internal/desktop/ui.goindex c1619fa2..17682d88 100644--- a/internal/desktop/ui.go+++ b/internal/desktop/ui.go@@ -340,7 +340,7 @@ func createTranscribeConfigGroup() *fyne.Container {// 创建文本转语音配置组func createTtsConfigGroup() *fyne.Container {- providerOptions := []string{"openai", "aliyun"}+ providerOptions := []string{"openai", "aliyun", "edge-tts"}providerSelect := widget.NewSelect(providerOptions, func(value string) {config.Conf.Tts.Provider = value})diff --git a/internal/service/init.go b/internal/service/init.goindex 47150403..e83b4f90 100644--- a/internal/service/init.go+++ b/internal/service/init.go@@ -10,6 +10,7 @@ import ("krillin-ai/pkg/whisper""krillin-ai/pkg/whispercpp""krillin-ai/pkg/whisperkit"+ "krillin-ai/pkg/localtts""go.uber.org/zap")@@ -53,6 +54,8 @@ func NewService() *Service {ttsClient = openai.NewClient(config.Conf.Tts.Openai.BaseUrl, config.Conf.Tts.Openai.ApiKey, config.Conf.App.Proxy)case "aliyun":ttsClient = aliyun.NewTtsClient(config.Conf.Tts.Aliyun.Speech.AccessKeyId, config.Conf.Tts.Aliyun.Speech.AccessKeySecret, config.Conf.Tts.Aliyun.Speech.AppKey)+ case "edge-tts":+ ttsClient = localtts.NewEdgeTtsClient()}return &Service{diff --git a/internal/service/srt2speech.go b/internal/service/srt2speech.goindex cec469f1..c94cc58d 100644--- a/internal/service/srt2speech.go+++ b/internal/service/srt2speech.go@@ -3,7 +3,6 @@ package serviceimport ("context""fmt"- "go.uber.org/zap""krillin-ai/internal/storage""krillin-ai/internal/types""krillin-ai/log"@@ -13,7 +12,10 @@ import ("path/filepath""regexp""strings"+ "sync""time"++ "go.uber.org/zap")// 输入中文字幕,生成配音@@ -52,13 +54,15 @@ func (s Service) srtFileToSpeech(ctx context.Context, stepParam *types.SubtitleTvoiceCode = code}+ // 并发处理TTS转换+ err = s.processSubtitlesConcurrently(subtitles, voiceCode, stepParam)+ if err != nil {+ log.GetLogger().Error("srtFileToSpeech processSubtitlesConcurrently error", zap.Any("stepParam", stepParam), zap.Error(err))+ return fmt.Errorf("srtFileToSpeech processSubtitlesConcurrently error: %w", err)+ }+for i, sub := range subtitles {outputFile := filepath.Join(stepParam.TaskBasePath, fmt.Sprintf("subtitle_%d.wav", i+1))- err = s.TtsClient.Text2Speech(sub.Text, voiceCode, outputFile)- if err != nil {- log.GetLogger().Error("srtFileToSpeech Text2Speech error", zap.Any("stepParam", stepParam), zap.Any("num", i+1), zap.Error(err))- return fmt.Errorf("srtFileToSpeech Text2Speech error: %w", err)- }// Step 3: 调整音频时长startTime, err := time.Parse("15:04:05,000", sub.Start)@@ -148,6 +152,114 @@ func (s Service) srtFileToSpeech(ctx context.Context, stepParam *types.SubtitleTreturn nil}+func (s Service) processSubtitlesConcurrently(subtitles []types.SrtSentenceWithStrTime, voiceCode string, stepParam *types.SubtitleTaskStepParam) error {+ // 创建一个结果数组来存储每个字幕的处理结果+ type processingResult struct {+ index int+ err error+ }++ maxConcurrency := 3 // 降低并发数以减少网络压力+ semaphore := make(chan struct{}, maxConcurrency)+ var wg sync.WaitGroup+ resultCh := make(chan processingResult, len(subtitles))++ // 并发生成所有音频文件+ for i, sub := range subtitles {+ wg.Add(1)+ go func(index int, subtitle types.SrtSentenceWithStrTime) {+ defer wg.Done()++ // 获取信号量+ semaphore <- struct{}{}+ defer func() { <-semaphore }()++ outputFile := filepath.Join(stepParam.TaskBasePath, fmt.Sprintf("subtitle_%d.wav", index+1))+ err := s.TtsClient.Text2Speech(subtitle.Text, voiceCode, outputFile)+ if err != nil {+ log.GetLogger().Error("processSubtitlesConcurrently Text2Speech error",+ zap.Any("index", index+1),+ zap.String("text", subtitle.Text),+ zap.Error(err))+ resultCh <- processingResult{index: index, err: fmt.Errorf("subtitle %d TTS error: %w", index+1, err)}+ return+ }++ // 成功处理+ resultCh <- processingResult{index: index, err: nil}+ }(i, sub)+ }++ // 等待所有goroutine完成+ wg.Wait()+ close(resultCh)++ // 收集所有结果并统计错误+ results := make([]processingResult, len(subtitles))+ errorCount := 0+ var firstError error++ for result := range resultCh {+ results[result.index] = result+ if result.err != nil {+ errorCount+++ if firstError == nil {+ firstError = result.err+ }+ }+ }++ // 如果有超过一半的字幕失败,则返回错误+ failureThreshold := len(subtitles) / 2+ if errorCount > failureThreshold {+ log.GetLogger().Error("processSubtitlesConcurrently: too many failures",+ zap.Int("total", len(subtitles)),+ zap.Int("errors", errorCount),+ zap.Int("threshold", failureThreshold))+ return fmt.Errorf("too many TTS failures: %d/%d failed, first error: %w", errorCount, len(subtitles), firstError)+ }++ // 验证成功的文件是否存在,对于失败的文件生成静音+ for i, result := range results {+ outputFile := filepath.Join(stepParam.TaskBasePath, fmt.Sprintf("subtitle_%d.wav", i+1))++ if result.err != nil {+ // 为失败的字幕生成静音文件+ log.GetLogger().Warn("生成静音文件替代失败的TTS",+ zap.Int("index", i+1),+ zap.String("file", outputFile))++ // 生成0.5秒的静音作为替代+ err := newGenerateSilence(outputFile, 0.5)+ if err != nil {+ log.GetLogger().Error("生成替代静音文件失败",+ zap.Int("index", i+1),+ zap.Error(err))+ return fmt.Errorf("failed to generate silence for subtitle %d: %w", i+1, err)+ }+ } else {+ // 验证成功生成的文件是否存在+ if _, err := os.Stat(outputFile); os.IsNotExist(err) {+ log.GetLogger().Error("processSubtitlesConcurrently output file not exist",+ zap.Any("index", i+1),+ zap.String("file", outputFile))+ return fmt.Errorf("subtitle %d output file not exist: %s", i+1, outputFile)+ }+ }+ }++ if errorCount > 0 {+ log.GetLogger().Warn("processSubtitlesConcurrently completed with some failures",+ zap.Int("total", len(subtitles)),+ zap.Int("errors", errorCount),+ zap.Int("success", len(subtitles)-errorCount))+ } else {+ log.GetLogger().Info("processSubtitlesConcurrently completed successfully", zap.Int("total", len(subtitles)))+ }++ return nil+}+func parseSRT(filePath string) ([]types.SrtSentenceWithStrTime, error) {data, err := os.ReadFile(filePath)if err != nil {@@ -272,4 +384,4 @@ func concatenateAudioFiles(audioFiles []string, outputFile, taskBasePath string)cmd := exec.Command(storage.FfmpegPath, "-y", "-f", "concat", "-safe", "0", "-i", listFile, "-c", "copy", outputFile)cmd.Stderr = os.Stderrreturn cmd.Run()-}+}\ No newline at end of filediff --git a/internal/storage/bin.go b/internal/storage/bin.goindex 23a8c18e..b2258f38 100644--- a/internal/storage/bin.go+++ b/internal/storage/bin.go@@ -5,7 +5,8 @@ var (FfprobePath stringYtdlpPath stringFasterwhisperPath string- WhisperXPath string+ WhisperXPath stringWhisperKitPath stringWhispercppPath string+ EdgeTtsPath string)diff --git a/pkg/localtts/edgetts.go b/pkg/localtts/edgetts.gonew file mode 100644index 00000000..d4356b94--- /dev/null+++ b/pkg/localtts/edgetts.go@@ -0,0 +1,133 @@+package localtts++import (+ "context"+ "fmt"+ "io/ioutil"+ "krillin-ai/internal/storage"+ "krillin-ai/log"+ "os"+ "os/exec"+ "path/filepath"+ "strings"+ "time"++ "go.uber.org/zap"+)++type EdgeTtsClient struct {+}++func NewEdgeTtsClient() *EdgeTtsClient {+ return &EdgeTtsClient{}+}++func (c *EdgeTtsClient) Text2Speech(text, voice, outputFile string) error {+ // 清理语音名称中的额外空格+ voice = strings.TrimSpace(voice)++ // 确保输出目录存在+ outputDir := filepath.Dir(outputFile)+ if err := os.MkdirAll(outputDir, 0755); err != nil {+ log.GetLogger().Error("创建输出目录失败", zap.String("dir", outputDir), zap.Error(err))+ return fmt.Errorf("创建输出目录失败: %w", err)+ }++ // 获取绝对路径+ absOutputFile, err := filepath.Abs(outputFile)+ if err != nil {+ log.GetLogger().Error("获取输出文件绝对路径失败", zap.Error(err))+ return fmt.Errorf("获取输出文件绝对路径失败: %w", err)+ }+ absOutputDir := filepath.Dir(absOutputFile)++ // 创建临时文件来存储文本内容,避免命令行参数转义问题+ tempFile, err := ioutil.TempFile(absOutputDir, "edge_tts_text_*.txt")+ if err != nil {+ log.GetLogger().Error("创建临时文件失败", zap.Error(err))+ return fmt.Errorf("创建临时文件失败: %w", err)+ }+ tempFileName := tempFile.Name()++ // 确保在函数结束时清理临时文件+ defer func() {+ tempFile.Close()+ if err := os.Remove(tempFileName); err != nil {+ log.GetLogger().Warn("清理临时文件失败", zap.String("file", tempFileName), zap.Error(err))+ }+ }()++ // 将文本写入临时文件+ if _, err := tempFile.WriteString(text); err != nil {+ log.GetLogger().Error("写入临时文件失败", zap.Error(err))+ return fmt.Errorf("写入临时文件失败: %w", err)+ }+ tempFile.Close() // 确保文件被写入++ // 重试机制+ maxRetries := 3+ for attempt := 1; attempt <= maxRetries; attempt++ {+ log.GetLogger().Info("edge-tts转录尝试",+ zap.Int("attempt", attempt),+ zap.Int("maxRetries", maxRetries),+ zap.String("text_length", fmt.Sprintf("%d", len(text))))++ err := c.attemptTTS(tempFileName, voice, absOutputFile, attempt)+ if err == nil {+ // 成功生成+ log.GetLogger().Info("edge-tts转录完成", zap.String("output file", absOutputFile))+ if _, err := os.Stat(absOutputFile); os.IsNotExist(err) {+ log.GetLogger().Error("edge-tts 输出文件不存在", zap.String("output file", absOutputFile))+ return fmt.Errorf("edge-tts 输出文件不存在: %s", absOutputFile)+ }+ return nil+ }++ log.GetLogger().Warn("edge-tts转录失败,准备重试",+ zap.Int("attempt", attempt),+ zap.Error(err))++ // 如果不是最后一次尝试,等待一段时间再重试+ if attempt < maxRetries {+ waitTime := time.Duration(attempt) * 2 * time.Second+ log.GetLogger().Info("等待重试", zap.Duration("waitTime", waitTime))+ time.Sleep(waitTime)+ }+ }++ return fmt.Errorf("edge-tts转录失败,已重试%d次", maxRetries)+}++func (c *EdgeTtsClient) attemptTTS(tempFileName, voice, absOutputFile string, attempt int) error {+ // 使用新的edge-tts命令参数(文件输入方式)+ cmdArgs := []string{+ "--text-file", tempFileName,+ "--voice", voice,+ "--output", absOutputFile,+ "--format", "wav",+ "--sample_rate", "44100",+ }++ // 创建带超时的上下文+ ctx, cancel := context.WithTimeout(context.Background(), 60*time.Second) // 60秒超时+ defer cancel()++ cmd := exec.CommandContext(ctx, storage.EdgeTtsPath, cmdArgs...)+ log.GetLogger().Info("edge-tts转录开始",+ zap.String("cmd", cmd.String()),+ zap.String("temp_file", tempFileName),+ zap.String("output_file", absOutputFile),+ zap.Int("attempt", attempt))++ output, err := cmd.CombinedOutput()+ if err != nil {+ if ctx.Err() == context.DeadlineExceeded {+ log.GetLogger().Error("edge-tts cmd 超时", zap.String("output", string(output)), zap.Error(err))+ return fmt.Errorf("edge-tts 执行超时")+ }+ log.GetLogger().Error("edge-tts cmd 执行失败", zap.String("output", string(output)), zap.Error(err))+ return err+ }++ return nil+}diff --git a/static/index.html b/static/index.htmlindex 165ec309..1a49c515 100644--- a/static/index.html+++ b/static/index.html@@ -931,6 +931,7 @@<select id="tts-provider" class="form-select"><option value="openai">OpenAI</option><option value="aliyun">阿里云</option>+ <option value="edge-tts">Edge TTS</option></select></div><div class="form-group">