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
package service
import (
"context"
"fmt"
"krillin-ai/config"
"krillin-ai/internal/service/dubbing"
"krillin-ai/internal/storage"
"krillin-ai/internal/types"
"os/exec"
"path/filepath"
)
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
}
if stepParam.TtsSourceFilePath == "" {
stepParam.TtsSourceFilePath = targetSRTPathForDubbing(stepParam.TaskBasePath)
}
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
if outputAudio == "" {
outputAudio = filepath.Join(stepParam.TaskBasePath, types.TtsResultAudioFileName)
}
outputVideo := stepParam.VideoWithTtsFilePath
if outputVideo == "" {
outputVideo = filepath.Join(stepParam.TaskBasePath, types.SubtitleTaskVideoWithTtsFileName)
}
phase, phaseStart, phaseEnd, phaseLabel := "planning_voice", 20, 20, "正在规划配音片段"
runner := dubbing.NewRunner(dubbing.Dependencies{
TTS: s.TtsClient,
Chat: s.ChatCompleter,
Language: stepParam.TargetLanguage,
Voice: voiceCode,
Workdir: stepParam.TaskBasePath,
InputSRT: stepParam.TtsSourceFilePath,
InputVideo: stepParam.InputVideoPath,
AudioOnly: stepParam.TtsAudioOnly,
OutputAudio: outputAudio,
OutputVideo: outputVideo,
ReportPhase: func(value string) {
phase = value
switch phase {
case "planning_voice":
phaseStart, phaseEnd, phaseLabel = 20, 20, "正在规划配音片段"
case "generating_voice":
phaseStart, phaseEnd, phaseLabel = 20, 90, "正在生成配音"
case "fitting_voice":
phaseStart, phaseEnd, phaseLabel = 91, 91, "正在对齐配音时间轴"
case "assembling_voice":
phaseStart, phaseEnd, phaseLabel = 92, 95, "正在拼接配音音频"
case "muxing_voice":
phaseStart, phaseEnd, phaseLabel = 96, 97, "正在合并配音和视频"
}
stepParam.TaskPtr.ReportProgress(types.TaskProgress{Phase: phase, Percent: phaseStart, Message: phaseLabel})
},
FFmpeg: func(args []string) error {
var output []byte
var err error
if len(args) > 0 && (args[len(args)-1] == outputAudio || args[len(args)-1] == outputVideo) {
output, err = runFFmpegProgress(ctx, args, stepParam.InputVideoPath, phase, phaseLabel, stepParam.TaskPtr, phaseStart, phaseEnd)
} else {
output, err = exec.CommandContext(ctx, storage.FfmpegPath, args...).CombinedOutput()
}
if err != nil {
return fmt.Errorf("dubbing ffmpeg: %w: %s", err, compactCommandOutput(output))
}
return nil
},
ReportProgress: func(completed, total int) {
if stepParam.TaskPtr == nil || total <= 0 {
return
}
percent := 20 + int(float64(completed)/float64(total)*70)
if percent > 90 {
percent = 90
}
stepParam.TaskPtr.ReportProgress(types.TaskProgress{Phase: "generating_voice", Percent: percent, Message: fmt.Sprintf("已生成配音 %d/%d 段", completed, total), Completed: completed, Total: total})
},
Config: dubbing.Config{
MinSubtitleDuration: config.Conf.Dubbing.MinSubtitleDuration,
MaxChunkSize: config.Conf.Dubbing.MaxChunkSize,
GapTolerance: config.Conf.Dubbing.GapTolerance,
SpeedMin: config.Conf.Dubbing.SpeedMin,
SpeedAccept: config.Conf.Dubbing.SpeedAccept,
SpeedMax: config.Conf.Dubbing.SpeedMax,
EnableTextRewrite: config.Conf.Dubbing.EnableTextRewrite,
RewriteMaxAttempts: config.Conf.Dubbing.RewriteMaxAttempts,
Estimator: config.Conf.Dubbing.Estimator,
},
})
result, err := runner.Run(ctx)
if err != nil {
return fmt.Errorf("srtFileToSpeech dubbing runner error: %w", err)
}
stepParam.TtsResultFilePath = result.Audio
stepParam.VideoWithTtsFilePath = result.Video
if stepParam.TaskPtr != nil {
stepParam.TaskPtr.SetProgress(98)
}
return nil
}