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 explicit subtitle render stage
wuliene344691524de
diff --git a/internal/service/render_stage.go b/internal/service/render_stage.gonew file mode 100644index 00000000..399dd36d--- /dev/null+++ b/internal/service/render_stage.go@@ -0,0 +1,56 @@+package service++import (+ "context"+ "fmt"+ "krillin-ai/internal/storage"+ "krillin-ai/internal/types"+ "os/exec"+ "path/filepath"+ "strings"+)++type RenderVideoRequest struct {+ Workdir string+ InputVideo string+ SubtitleFile string+ OutputFile string+ Horizontal bool+ StepParam *types.SubtitleTaskStepParam+}++func (s Service) RenderVideo(ctx context.Context, req RenderVideoRequest) (string, error) {+ _ = ctx+ return renderSubtitleFile(req)+}++func buildEmbedSubtitleArgs(req RenderVideoRequest) ([]string, string) {+ assPath := filepath.Join(req.Workdir, "formatted_subtitles.ass")+ ass := strings.ReplaceAll(assPath, "\\", "/")+ return []string{+ "-y",+ "-i", req.InputVideo,+ "-vf", fmt.Sprintf("ass=%s", ass),+ "-c:a", "aac",+ "-b:a", "192k",+ req.OutputFile,+ }, assPath+}++func renderSubtitleFile(req RenderVideoRequest) (string, error) {+ assPath := filepath.Join(req.Workdir, "formatted_subtitles.ass")+ stepParam := req.StepParam+ if stepParam == nil {+ stepParam = &types.SubtitleTaskStepParam{TaskBasePath: req.Workdir}+ }+ if err := srtToAss(req.SubtitleFile, assPath, req.Horizontal, stepParam); err != nil {+ return "", fmt.Errorf("renderSubtitleFile srtToAss error: %w", err)+ }+ args, _ := buildEmbedSubtitleArgs(req)+ cmd := exec.Command(storage.FfmpegPath, args...)+ output, err := cmd.CombinedOutput()+ if err != nil {+ return "", fmt.Errorf("renderSubtitleFile ffmpeg error: %w, output: %s", err, string(output))+ }+ return req.OutputFile, nil+}diff --git a/internal/service/render_stage_test.go b/internal/service/render_stage_test.gonew file mode 100644index 00000000..6f679453--- /dev/null+++ b/internal/service/render_stage_test.go@@ -0,0 +1,28 @@+package service++import (+ "path/filepath"+ "strings"+ "testing"+)++func TestBuildEmbedSubtitleArgsUsesRequestedSubtitleAndOutput(t *testing.T) {+ req := RenderVideoRequest{+ Workdir: "tasks/demo",+ InputVideo: "tasks/demo/origin_video.mp4",+ SubtitleFile: "tasks/demo/target_language_srt.srt",+ OutputFile: "tasks/demo/output/horizontal_dubbed.mp4",+ Horizontal: true,+ }+ args, assPath := buildEmbedSubtitleArgs(req)+ joined := strings.Join(args, " ")+ if !strings.Contains(assPath, filepath.Join("tasks", "demo")) {+ t.Fatalf("assPath = %q does not use workdir", assPath)+ }+ if !strings.Contains(joined, "tasks/demo/origin_video.mp4") {+ t.Fatalf("args do not contain input video: %v", args)+ }+ if !strings.Contains(joined, "tasks/demo/output/horizontal_dubbed.mp4") {+ t.Fatalf("args do not contain output file: %v", args)+ }+}diff --git a/internal/service/srt_embed.go b/internal/service/srt_embed.goindex 1c5ba6ef..30a2a069 100644--- a/internal/service/srt_embed.go+++ b/internal/service/srt_embed.go@@ -314,24 +314,20 @@ func embedSubtitles(stepParam *types.SubtitleTaskStepParam, isHorizontal bool, wif isHorizontal {outputFileName = types.SubtitleTaskHorizontalEmbedVideoFileName}- assPath := filepath.Join(stepParam.TaskBasePath, "formatted_subtitles.ass")-- if err := srtToAss(stepParam.BilingualSrtFilePath, assPath, isHorizontal, stepParam); err != nil {- log.GetLogger().Error("embedSubtitles srtToAss error", zap.Any("step param", stepParam), zap.Error(err))- return fmt.Errorf("embedSubtitles srtToAss error: %w", err)- }input := stepParam.InputVideoPathif withTts {input = stepParam.VideoWithTtsFilePath}- 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))- return fmt.Errorf("embedSubtitles embed subtitle into video ffmpeg error: %w", err)- }- return nil+ _, err := renderSubtitleFile(RenderVideoRequest{+ Workdir: stepParam.TaskBasePath,+ InputVideo: input,+ SubtitleFile: stepParam.BilingualSrtFilePath,+ OutputFile: filepath.Join(stepParam.TaskBasePath, "output", outputFileName),+ Horizontal: isHorizontal,+ StepParam: stepParam,+ })+ return err}func getFontPaths() (string, string, error) {