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: embed tts result into video
沈浩6883e785a23c
diff --git a/internal/desktop/ui.go b/internal/desktop/ui.goindex 811257f4..fa42f0c8 100644--- a/internal/desktop/ui.go+++ b/internal/desktop/ui.go@@ -78,7 +78,7 @@ func CreateSubtitleTab(window fyne.Window) fyne.CanvasObject {// 创建配音设置区域voiceSettingsCard := createVoiceSettingsCard(sm)- // 创建字幕嵌入设置区域+ // 创建视频合成区域embedSettingsCard := createEmbedSettingsCard(sm)// 创建进度和下载区域@@ -539,7 +539,7 @@ func createVoiceSettingsCard(sm *SubtitleManager) *fyne.Container {voiceCodeEntry.Disable()// todo 限制为仅阿里云- audioSampleButton := SecondaryButton("选择音色克隆样本 Choose voice clone sample", theme.MediaMusicIcon(), sm.ShowAudioFileDialog)+ audioSampleButton := SecondaryButton("选择音色克隆样本(仅支持阿里云tts) Choose voice clone sample(Aliyun tts only)", theme.MediaMusicIcon(), sm.ShowAudioFileDialog)audioSampleButton.Disable()voiceoverCheck := widget.NewCheck("启用配音 Enable dubbing", func(checked bool) {@@ -561,10 +561,9 @@ func createVoiceSettingsCard(sm *SubtitleManager) *fyne.Container {return StyledCard("3. 配音设置 Dubbing setting", grid)}-// 创建字幕嵌入设置卡片+// 视频合成卡片func createEmbedSettingsCard(sm *SubtitleManager) *fyne.Container {- // 创建字幕嵌入复选框- embedCheck := widget.NewCheck("合成字幕嵌入视频 Embed subtitles into video", nil)+ embedCheck := widget.NewCheck("合成视频 Composite video", nil)// 创建视频类型选择embedTypeSelect := StyledSelect([]string{@@ -623,7 +622,7 @@ func createEmbedSettingsCard(sm *SubtitleManager) *fyne.Container {container.NewPadded(titleInputContainer),)- return StyledCard("字幕嵌入设置 Subtitle embed setting", mainContainer)+ return StyledCard("视频合成设置 Subtitle embed setting", mainContainer)}// 创建进度和下载区域diff --git a/internal/service/srt2speech.go b/internal/service/srt2speech.goindex a06db68d..cec469f1 100644--- a/internal/service/srt2speech.go+++ b/internal/service/srt2speech.go@@ -134,6 +134,14 @@ func (s Service) srtFileToSpeech(ctx context.Context, stepParam *types.SubtitleTreturn fmt.Errorf("srtFileToSpeech concatenateAudioFiles error: %w", err)}stepParam.TtsResultFilePath = finalOutput++ videoWithTtsPath := filepath.Join(stepParam.TaskBasePath, types.SubtitleTaskVideoWithTtsFileName)+ // 合成音频替换后的新视频+ err = util.ReplaceAudioInVideo(stepParam.InputVideoPath, finalOutput, videoWithTtsPath)+ if err != nil {+ log.GetLogger().Error("srtFileToSpeech ReplaceAudioInVideo error", zap.Any("stepParam", stepParam), zap.Error(err))+ }+ stepParam.VideoWithTtsFilePath = videoWithTtsPath// 更新字幕任务信息stepParam.TaskPtr.ProcessPct = 98log.GetLogger().Info("srtFileToSpeech success", zap.String("task id", stepParam.TaskId))diff --git a/internal/service/srt_embed.go b/internal/service/srt_embed.goindex 1daddb7c..02ab0306 100644--- a/internal/service/srt_embed.go+++ b/internal/service/srt_embed.go@@ -37,8 +37,8 @@ func (s Service) embedSubtitles(ctx context.Context, stepParam *types.SubtitleTalog.GetLogger().Info("检测到输入视频是竖屏,无法合成横屏视频,跳过")return nil}- log.GetLogger().Info("合成字幕嵌入视频:横屏")- err = embedSubtitles(stepParam, true)+ log.GetLogger().Info("合成视频:横屏")+ err = embedSubtitles(stepParam, true, stepParam.EnableTts)if err != nil {log.GetLogger().Error("embedSubtitles embedSubtitles error", zap.Any("step param", stepParam), zap.Error(err))return fmt.Errorf("embedSubtitles embedSubtitles error: %w", err)@@ -55,8 +55,8 @@ func (s Service) embedSubtitles(ctx context.Context, stepParam *types.SubtitleTa}stepParam.InputVideoPath = transferredVerticalVideoPath}- log.GetLogger().Info("合成字幕嵌入视频:竖屏")- err = embedSubtitles(stepParam, false)+ log.GetLogger().Info("合成视频:竖屏")+ err = embedSubtitles(stepParam, false, stepParam.EnableTts)if err != nil {log.GetLogger().Error("embedSubtitles embedSubtitles error", zap.Any("step param", stepParam), zap.Error(err))return fmt.Errorf("embedSubtitles embedSubtitles error: %w", err)@@ -65,7 +65,7 @@ func (s Service) embedSubtitles(ctx context.Context, stepParam *types.SubtitleTalog.GetLogger().Info("字幕嵌入视频成功")return nil}- log.GetLogger().Info("合成字幕嵌入视频:不合成")+ log.GetLogger().Info("合成视频:不合成")return nil}@@ -310,7 +310,7 @@ func srtToAss(inputSRT, outputASS string, isHorizontal bool, stepParam *types.Sureturn nil}-func embedSubtitles(stepParam *types.SubtitleTaskStepParam, isHorizontal bool) error {+func embedSubtitles(stepParam *types.SubtitleTaskStepParam, isHorizontal bool, withTts bool) error {outputFileName := types.SubtitleTaskVerticalEmbedVideoFileNameif isHorizontal {outputFileName = types.SubtitleTaskHorizontalEmbedVideoFileName@@ -321,8 +321,12 @@ func embedSubtitles(stepParam *types.SubtitleTaskStepParam, isHorizontal bool) elog.GetLogger().Error("embedSubtitles srtToAss error", zap.Any("step param", stepParam), zap.Error(err))return fmt.Errorf("embedSubtitles srtToAss error: %w", err)}+ input := stepParam.InputVideoPath+ if withTts {+ input = stepParam.VideoWithTtsFilePath+ }- cmd := exec.Command(storage.FfmpegPath, "-y", "-i", stepParam.InputVideoPath, "-vf", fmt.Sprintf("ass=%s", strings.ReplaceAll(assPath, "\\", "/")), "-c:a", "aac", "-b:a", "192k", filepath.Join(stepParam.TaskBasePath, fmt.Sprintf("/output/%s", outputFileName)))+ cmd := exec.Command(storage.FfmpegPath, "-y", "-i", input, "-vf", fmt.Sprintf("ass=%s", strings.ReplaceAll(assPath, "\\", "/")), "-c:a", "aac", "-b:a", "192k", filepath.Join(stepParam.TaskBasePath, fmt.Sprintf("/output/%s", outputFileName)))output, err := cmd.CombinedOutput()if err != nil {log.GetLogger().Error("embedSubtitles embed subtitle into video ffmpeg error", zap.String("video path", stepParam.InputVideoPath), zap.String("output", string(output)), zap.Error(err))diff --git a/internal/types/subtitle_task.go b/internal/types/subtitle_task.goindex 30ccdded..3ffde187 100644--- a/internal/types/subtitle_task.go+++ b/internal/types/subtitle_task.go@@ -152,6 +152,7 @@ const (SubtitleTaskTransferredVerticalVideoFileName = "transferred_vertical_video.mp4"SubtitleTaskHorizontalEmbedVideoFileName = "horizontal_embed.mp4"SubtitleTaskVerticalEmbedVideoFileName = "vertical_embed.mp4"+ SubtitleTaskVideoWithTtsFileName = "video_with_tts.mp4")const (@@ -194,7 +195,8 @@ type SubtitleTaskStepParam struct {EmbedSubtitleVideoType string // 合成字幕嵌入的视频类型 none不嵌入 horizontal横屏 vertical竖屏VerticalVideoMajorTitle string // 合成竖屏视频的主标题VerticalVideoMinorTitle string- MaxWordOneLine int // 字幕一行最多显示多少个字+ MaxWordOneLine int // 字幕一行最多显示多少个字+ VideoWithTtsFilePath string // 替换源视频的音频为tts结果后的视频路径}type SrtSentence struct {diff --git a/pkg/util/video.go b/pkg/util/video.gonew file mode 100644index 00000000..5c4af5c4--- /dev/null+++ b/pkg/util/video.go@@ -0,0 +1,17 @@+package util++import (+ "fmt"+ "krillin-ai/internal/storage"+ "os/exec"+)++func ReplaceAudioInVideo(videoFile string, audioFile string, outputFile string) error {+ cmd := exec.Command(storage.FfmpegPath, "-i", videoFile, "-i", audioFile, "-c:v", "copy", "-map", "0:v:0", "-map", "1:a:0", outputFile)++ if err := cmd.Run(); err != nil {+ return fmt.Errorf("error replacing audio in video: %v", err)+ }++ return nil+}diff --git a/static/index.html b/static/index.htmlindex 83421909..efeec294 100644--- a/static/index.html+++ b/static/index.html@@ -414,13 +414,15 @@<div class="formGroup"><label>配音:</label><input type="checkbox" id="voiceover-toggle">- <select id="voiceover-voice" disabled>- <option value="2">男声</option>- <option value="1">女声</option>- </select>+ <input+ type="text"+ id="voiceover-voice"+ placeholder="输入 voice code"+ disabled+ ><span>或</span><label for="voiceFileInput" id="voiceFileInputName" class="voice-file-input">- 选择音色克隆样本+ 选择音色克隆样本(仅阿里云)</label><span class="voice-file-input-remove" id="voiceFileInputRemove">删除</span><input style="display: none" type="file" id="voiceFileInput" name="voiceFileInput" accept="audio/*">@@ -449,7 +451,7 @@<!-- 合成字幕嵌入视频 --><div class="formGroup" id="embed-subtitle-container">- <label>合成字幕嵌入视频:</label>+ <label>合成视频:</label><input type="checkbox" id="embed-subtitle-toggle"><select id="embed-subtitle"><option value="horizontal">横屏视频</option>