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: harden high quality dubbing entry

wuliene04275716528
diff --git a/internal/service/srt2speech.go b/internal/service/srt2speech.go
index 44f27434..001ffccb 100644
--- a/internal/service/srt2speech.go
+++ b/internal/service/srt2speech.go
@@ -13,8 +13,27 @@ func targetSRTPathForDubbing(taskBasePath string) string {
return filepath.Join(taskBasePath, types.SubtitleTaskTargetLanguageSrtFileName)
}
+type voiceCloneFunc func(prefix, audioURL string) (string, error)
+
+func resolveDubbingVoiceCode(baseVoice, cloneURL string, clone voiceCloneFunc) (string, error) {
+ if cloneURL == "" {
+ return baseVoice, nil
+ }
+ if clone == nil {
+ return "", fmt.Errorf("srtFileToSpeech CosyVoiceClone error: voice clone client is nil")
+ }
+ code, err := clone("krillinai", cloneURL)
+ if err != nil {
+ return "", fmt.Errorf("srtFileToSpeech CosyVoiceClone error: %w", err)
+ }
+ return code, nil
+}
+
// 输入目标语言字幕,生成配音
func (s Service) srtFileToSpeech(ctx context.Context, stepParam *types.SubtitleTaskStepParam) error {
+ if stepParam == nil {
+ return fmt.Errorf("srtFileToSpeech stepParam is nil")
+ }
if !stepParam.EnableTts {
return nil
}
@@ -22,16 +41,13 @@ func (s Service) srtFileToSpeech(ctx context.Context, stepParam *types.SubtitleT
stepParam.TtsSourceFilePath = targetSRTPathForDubbing(stepParam.TaskBasePath)
}
- voiceCode := stepParam.TtsVoiceCode
- if stepParam.VoiceCloneAudioUrl != "" {
- if s.VoiceCloneClient == nil {
- return fmt.Errorf("srtFileToSpeech CosyVoiceClone error: voice clone client is nil")
- }
- code, err := s.VoiceCloneClient.CosyVoiceClone("krillinai", stepParam.VoiceCloneAudioUrl)
- if err != nil {
- return fmt.Errorf("srtFileToSpeech CosyVoiceClone error: %w", err)
- }
- voiceCode = code
+ var clone voiceCloneFunc
+ if s.VoiceCloneClient != nil {
+ clone = s.VoiceCloneClient.CosyVoiceClone
+ }
+ voiceCode, err := resolveDubbingVoiceCode(stepParam.TtsVoiceCode, stepParam.VoiceCloneAudioUrl, clone)
+ if err != nil {
+ return err
}
outputAudio := stepParam.TtsResultFilePath
diff --git a/internal/service/srt2speech_test.go b/internal/service/srt2speech_test.go
index 98e1df67..6885ab60 100644
--- a/internal/service/srt2speech_test.go
+++ b/internal/service/srt2speech_test.go
@@ -1,10 +1,65 @@
package service
import (
+ "context"
"path/filepath"
+ "strings"
"testing"
)
+func TestSrtFileToSpeechRejectsNilStepParam(t *testing.T) {
+ err := Service{}.srtFileToSpeech(context.Background(), nil)
+ if err == nil {
+ t.Fatal("srtFileToSpeech() error = nil, want error")
+ }
+ if !strings.Contains(err.Error(), "srtFileToSpeech stepParam is nil") {
+ t.Fatalf("srtFileToSpeech() error = %q, want nil stepParam error", err)
+ }
+}
+
+func TestResolveDubbingVoiceCodeUsesCloneCode(t *testing.T) {
+ var gotPrefix string
+ var gotAudioURL string
+ clone := func(prefix, audioURL string) (string, error) {
+ gotPrefix = prefix
+ gotAudioURL = audioURL
+ return "cloned-code", nil
+ }
+
+ got, err := resolveDubbingVoiceCode("base", "clone.wav", clone)
+ if err != nil {
+ t.Fatalf("resolveDubbingVoiceCode() error = %v, want nil", err)
+ }
+ if got != "cloned-code" {
+ t.Fatalf("resolveDubbingVoiceCode() = %q, want %q", got, "cloned-code")
+ }
+ if gotPrefix != "krillinai" {
+ t.Fatalf("clone prefix = %q, want %q", gotPrefix, "krillinai")
+ }
+ if gotAudioURL != "clone.wav" {
+ t.Fatalf("clone audioURL = %q, want %q", gotAudioURL, "clone.wav")
+ }
+}
+
+func TestResolveDubbingVoiceCodeWithoutCloneURLReturnsBaseVoice(t *testing.T) {
+ called := false
+ clone := func(prefix, audioURL string) (string, error) {
+ called = true
+ return "cloned-code", nil
+ }
+
+ got, err := resolveDubbingVoiceCode("base", "", clone)
+ if err != nil {
+ t.Fatalf("resolveDubbingVoiceCode() error = %v, want nil", err)
+ }
+ if got != "base" {
+ t.Fatalf("resolveDubbingVoiceCode() = %q, want %q", got, "base")
+ }
+ if called {
+ t.Fatal("clone was called without clone URL")
+ }
+}
+
func TestTargetSRTPathForDubbingUsesTargetLanguageFile(t *testing.T) {
base := filepath.Join("tasks", "demo")
got := targetSRTPathForDubbing(base)