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(cli): support local media and stream progress

wulien-center78f8ba81e287
diff --git a/cmd/cli/main.go b/cmd/cli/main.go
index db08143f..06f9c630 100644
--- a/cmd/cli/main.go
+++ b/cmd/cli/main.go
@@ -4,6 +4,7 @@ import (
"context"
"encoding/json"
"fmt"
+ "io"
"krillin-ai/config"
"krillin-ai/internal/cli"
"krillin-ai/internal/deps"
@@ -12,15 +13,17 @@ import (
"krillin-ai/log"
"os"
"strings"
+ "sync"
)
func main() {
log.InitLogger()
defer log.GetLogger().Sync()
+ output := &jsonLineWriter{output: os.Stdout}
cmd, err := cli.Parse(os.Args[1:])
if err != nil {
- writeAndExit(errorResponse(err, pipeline.ErrorKindUsage))
+ writeAndExit(output, errorResponse(err, pipeline.ErrorKindUsage))
}
if cmd.Help {
fmt.Print(cli.Help(cmd))
@@ -30,20 +33,21 @@ func main() {
if cmd.Voices.Provider == "" {
_ = config.LoadConfig()
}
- writeAndExit(cli.Execute(context.Background(), nil, cmd))
+ writeAndExit(output, cli.Execute(context.Background(), nil, cmd))
return
}
if cmd.DryRun {
- writeAndExit(cli.Execute(context.Background(), nil, cmd))
+ writeAndExit(output, cli.Execute(context.Background(), nil, cmd))
return
}
if cmd.Name == "update" {
- writeAndExit(cli.Execute(context.Background(), nil, cmd))
+ writeAndExit(output, cli.Execute(context.Background(), nil, cmd))
return
}
+ configureOpenCreatorProgress(&cmd, output)
if !config.LoadConfig() {
- writeAndExit(pipeline.Response{
+ writeAndExit(output, pipeline.Response{
OK: false,
Error: &pipeline.Error{
Kind: pipeline.ErrorKindUsage,
@@ -53,29 +57,70 @@ func main() {
})
}
if err := config.CheckBaseConfig(); err != nil {
- writeAndExit(errorResponse(err, pipeline.ErrorKindUsage))
+ writeAndExit(output, errorResponse(err, pipeline.ErrorKindUsage))
}
if requiresTranscriptionAtStart(cmd) {
if err := config.ValidateTranscriptionConfig(); err != nil {
- writeAndExit(errorResponse(err, pipeline.ErrorKindUsage))
+ writeAndExit(output, errorResponse(err, pipeline.ErrorKindUsage))
}
}
if err := deps.CheckCoreDependencies(); err != nil {
- writeAndExit(errorResponse(err, pipeline.ErrorKindDependency))
+ writeAndExit(output, errorResponse(err, pipeline.ErrorKindDependency))
}
if requiresTranscriptionAtStart(cmd) {
if err := deps.CheckTranscriptionDependency(); err != nil {
- writeAndExit(errorResponse(err, pipeline.ErrorKindDependency))
+ writeAndExit(output, errorResponse(err, pipeline.ErrorKindDependency))
}
}
if cmd.Name == "tts" {
if err := deps.CheckTTSDependency(); err != nil {
- writeAndExit(errorResponse(err, pipeline.ErrorKindDependency))
+ writeAndExit(output, errorResponse(err, pipeline.ErrorKindDependency))
}
}
svc := service.NewService()
adapter := pipeline.NewServiceAdapter(svc)
- writeAndExit(cli.Execute(context.Background(), adapter, cmd))
+ writeAndExit(output, cli.Execute(context.Background(), adapter, cmd))
+}
+
+type progressFrame struct {
+ Type string `json:"type"`
+ Phase string `json:"phase"`
+ Percent int `json:"percent"`
+ Message string `json:"message,omitempty"`
+}
+
+type jsonLineWriter struct {
+ mu sync.Mutex
+ output io.Writer
+}
+
+func (w *jsonLineWriter) Write(value any) error {
+ data, err := json.Marshal(value)
+ if err != nil {
+ return err
+ }
+ w.mu.Lock()
+ defer w.mu.Unlock()
+ _, err = fmt.Fprintln(w.output, string(data))
+ return err
+}
+
+func configureOpenCreatorProgress(cmd *cli.Command, output *jsonLineWriter) {
+ if os.Getenv("OPENCREATOR_KRILLINAI_CLI") != "1" || cmd.Name != "subtitle" {
+ return
+ }
+ previous := cmd.Subtitle.ReportProgress
+ cmd.Subtitle.ReportProgress = func(phase string, percent int, message string) {
+ if previous != nil {
+ previous(phase, percent, message)
+ }
+ _ = output.Write(progressFrame{
+ Type: "progress",
+ Phase: phase,
+ Percent: percent,
+ Message: message,
+ })
+ }
}
func requiresTranscriptionAtStart(cmd cli.Command) bool {
@@ -101,13 +146,11 @@ func errorResponse(err error, kind pipeline.ErrorKind) pipeline.Response {
}
}
-func writeAndExit(resp pipeline.Response) {
- data, err := json.Marshal(resp)
- if err != nil {
+func writeAndExit(output *jsonLineWriter, resp pipeline.Response) {
+ if err := output.Write(resp); err != nil {
_, _ = fmt.Fprintf(os.Stderr, `{"ok":false,"error":{"kind":"internal","code":"json_marshal_failed","message":%q}}`+"\n", err.Error())
os.Exit(1)
}
- fmt.Println(string(data))
if !resp.OK {
os.Exit(pipeline.ExitCodeForError(resp.Error))
}
diff --git a/cmd/cli/main_test.go b/cmd/cli/main_test.go
index 8260e1e6..e81e9f88 100644
--- a/cmd/cli/main_test.go
+++ b/cmd/cli/main_test.go
@@ -1,8 +1,11 @@
package main
import (
+ "bytes"
+ "encoding/json"
"krillin-ai/internal/cli"
"krillin-ai/internal/pipeline"
+ "strings"
"testing"
)
@@ -48,3 +51,34 @@ func TestRequiresTranscriptionAtStart(t *testing.T) {
})
}
}
+
+func TestConfigureOpenCreatorProgressWritesJSONLines(t *testing.T) {
+ t.Setenv("OPENCREATOR_KRILLINAI_CLI", "1")
+ var buffer bytes.Buffer
+ output := &jsonLineWriter{output: &buffer}
+ cmd := cli.Command{Name: "subtitle"}
+
+ configureOpenCreatorProgress(&cmd, output)
+ if cmd.Subtitle.ReportProgress == nil {
+ t.Fatal("ReportProgress is nil")
+ }
+ cmd.Subtitle.ReportProgress("transcribing_audio", 42, "working")
+
+ line := strings.TrimSpace(buffer.String())
+ var frame progressFrame
+ if err := json.Unmarshal([]byte(line), &frame); err != nil {
+ t.Fatalf("invalid progress JSON %q: %v", line, err)
+ }
+ if frame.Type != "progress" || frame.Phase != "transcribing_audio" || frame.Percent != 42 || frame.Message != "working" {
+ t.Fatalf("unexpected progress frame: %+v", frame)
+ }
+}
+
+func TestConfigureOpenCreatorProgressStaysDisabledByDefault(t *testing.T) {
+ t.Setenv("OPENCREATOR_KRILLINAI_CLI", "")
+ cmd := cli.Command{Name: "subtitle"}
+ configureOpenCreatorProgress(&cmd, &jsonLineWriter{output: &bytes.Buffer{}})
+ if cmd.Subtitle.ReportProgress != nil {
+ t.Fatal("ReportProgress should remain nil")
+ }
+}
diff --git a/internal/pipeline/subtitle.go b/internal/pipeline/subtitle.go
index 0af7d841..3ba03885 100644
--- a/internal/pipeline/subtitle.go
+++ b/internal/pipeline/subtitle.go
@@ -55,6 +55,7 @@ func GenerateSubtitles(ctx context.Context, svc StageService, req SubtitleReques
if err := svc.PrepareMedia(ctx, stepParam); err != nil {
return failSubtitleStage(req, manifest, ErrorKindRetryable, "prepare_media_failed", err)
}
+ syncPreparedMediaOutputs(manifest, stepParam)
var platformCaptionErr error
if isYouTubeInput(req.Input) && req.CaptionSource != CaptionSourceWhisper {
@@ -80,6 +81,7 @@ func GenerateSubtitles(ctx context.Context, svc StageService, req SubtitleReques
if err := prepareOriginalMediaForRendering(ctx, svc, stepParam); err != nil {
return failSubtitleStage(req, manifest, ErrorKindRetryable, "prepare_media_for_render_failed", err)
}
+ syncPreparedMediaOutputs(manifest, stepParam)
reportSubtitleProgress(req, "collecting_outputs", 95, "正在整理字幕和视频产物")
return saveSubtitleSuccess(manifest, req, CaptionSource("youtube_vtt"))
}
@@ -101,6 +103,7 @@ func GenerateSubtitles(ctx context.Context, svc StageService, req SubtitleReques
err,
))
}
+ syncPreparedMediaOutputs(manifest, stepParam)
}
stepParam.TaskPtr.SetProgressReporter(audioSubtitleProgressReporter(req))
@@ -158,6 +161,15 @@ func minInt(left, right int) int {
return right
}
+func syncPreparedMediaOutputs(manifest *Manifest, stepParam *types.SubtitleTaskStepParam) {
+ if stepParam.InputVideoPath != "" {
+ manifest.Outputs.OriginVideo = stepParam.InputVideoPath
+ }
+ if stepParam.AudioFilePath != "" {
+ manifest.Outputs.OriginAudio = stepParam.AudioFilePath
+ }
+}
+
func subtitleManifest(req SubtitleRequest) (*Manifest, error) {
manifest, err := LoadManifest(req.Workdir)
if err == nil {
diff --git a/internal/pipeline/subtitle_test.go b/internal/pipeline/subtitle_test.go
index b85dcfb9..9cd483b7 100644
--- a/internal/pipeline/subtitle_test.go
+++ b/internal/pipeline/subtitle_test.go
@@ -21,6 +21,8 @@ type fakeStageService struct {
lastCoverPrompt string
lastCoverSize string
coverImageB64 string
+ preparedVideoPath string
+ preparedAudioPath string
}
func (f *fakeStageService) PrepareMedia(_ context.Context, p *types.SubtitleTaskStepParam) error {
@@ -28,6 +30,12 @@ func (f *fakeStageService) PrepareMedia(_ context.Context, p *types.SubtitleTask
f.prepareVTT = append(f.prepareVTT, p.VttSwitch)
f.prepareEmbedTypes = append(f.prepareEmbedTypes, p.EmbedSubtitleVideoType)
f.lastPrepare = p
+ if f.preparedVideoPath != "" {
+ p.InputVideoPath = f.preparedVideoPath
+ }
+ if f.preparedAudioPath != "" {
+ p.AudioFilePath = f.preparedAudioPath
+ }
return nil
}
@@ -124,6 +132,35 @@ func TestGenerateSubtitlesPassesSubtitleStyleToStepParam(t *testing.T) {
}
}
+func TestGenerateSubtitlesReportsPreparedLocalMediaOutputs(t *testing.T) {
+ dir := t.TempDir()
+ source := dir + "/source.webm"
+ audio := dir + "/origin_audio.mp3"
+ fake := &fakeStageService{
+ preparedVideoPath: source,
+ preparedAudioPath: audio,
+ }
+ req := SubtitleRequest{
+ Input: "local:" + source,
+ Workdir: dir,
+ TaskID: "demo",
+ OriginLang: "en",
+ TargetLang: "zh_cn",
+ CaptionSource: CaptionSourceWhisper,
+ }
+
+ resp, err := GenerateSubtitles(context.Background(), fake, req)
+ if err != nil {
+ t.Fatalf("GenerateSubtitles() error = %v", err)
+ }
+ if resp.Outputs.OriginVideo != source {
+ t.Fatalf("OriginVideo = %q, want %q", resp.Outputs.OriginVideo, source)
+ }
+ if resp.Outputs.OriginAudio != audio {
+ t.Fatalf("OriginAudio = %q, want %q", resp.Outputs.OriginAudio, audio)
+ }
+}
+
func TestGenerateSubtitlesManualDoesNotFallback(t *testing.T) {
dir := t.TempDir()
fake := &fakeStageService{downloadErr: errors.New("no captions")}
diff --git a/internal/service/link2file.go b/internal/service/link2file.go
index e4d5c90f..dcfd262f 100644
--- a/internal/service/link2file.go
+++ b/internal/service/link2file.go
@@ -9,6 +9,7 @@ import (
"krillin-ai/internal/types"
"krillin-ai/log"
"krillin-ai/pkg/util"
+ "os"
"os/exec"
"strings"
@@ -23,10 +24,11 @@ func (s Service) linkToFile(ctx context.Context, stepParam *types.SubtitleTaskSt
link := stepParam.Link
audioPath := fmt.Sprintf("%s/%s", stepParam.TaskBasePath, types.SubtitleTaskAudioFileName)
videoPath := fmt.Sprintf("%s/%s", stepParam.TaskBasePath, types.SubtitleTaskVideoFileName)
+ localVideoPath, isLocal := resolveLocalMediaInput(link)
stepParam.TaskPtr.SetProgress(3)
- if strings.Contains(link, "local:") {
+ if isLocal {
// 本地文件
- videoPath = strings.ReplaceAll(link, "local:", "")
+ videoPath = localVideoPath
cmd := exec.Command(storage.FfmpegPath, "-i", videoPath, "-vn", "-ar", "44100", "-ac", "2", "-ab", "192k", "-f", "mp3", audioPath)
output, err = cmd.CombinedOutput()
if err != nil {
@@ -91,7 +93,7 @@ func (s Service) linkToFile(ctx context.Context, stepParam *types.SubtitleTaskSt
stepParam.TaskPtr.SetProgress(6)
stepParam.AudioFilePath = audioPath
- if !strings.HasPrefix(link, "local:") && stepParam.EmbedSubtitleVideoType != "none" {
+ if !isLocal && stepParam.EmbedSubtitleVideoType != "none" {
// 需要下载原视频
cmdArgs := []string{"-f", "bestvideo[height<=1080][ext=mp4]+bestaudio[ext=m4a]/bestvideo[height<=720][ext=mp4]+bestaudio[ext=m4a]/bestvideo[height<=480][ext=mp4]+bestaudio[ext=m4a]", "-o", videoPath, stepParam.Link}
if config.Conf.App.Proxy != "" {
@@ -114,6 +116,18 @@ func (s Service) linkToFile(ctx context.Context, stepParam *types.SubtitleTaskSt
return nil
}
+func resolveLocalMediaInput(input string) (string, bool) {
+ value := strings.TrimSpace(input)
+ if strings.HasPrefix(value, "local:") {
+ return strings.TrimPrefix(value, "local:"), true
+ }
+ info, err := os.Stat(value)
+ if err != nil || !info.Mode().IsRegular() {
+ return "", false
+ }
+ return value, true
+}
+
func compactCommandOutput(output []byte) string {
const maximum = 1200
value := strings.TrimSpace(string(output))
diff --git a/internal/service/link2file_test.go b/internal/service/link2file_test.go
new file mode 100644
index 00000000..04bbf5bd
--- /dev/null
+++ b/internal/service/link2file_test.go
@@ -0,0 +1,26 @@
+package service
+
+import (
+ "os"
+ "path/filepath"
+ "testing"
+)
+
+func TestResolveLocalMediaInput(t *testing.T) {
+ dir := t.TempDir()
+ path := filepath.Join(dir, "source.webm")
+ if err := os.WriteFile(path, []byte("video"), 0600); err != nil {
+ t.Fatal(err)
+ }
+
+ for _, input := range []string{path, "local:" + path} {
+ got, ok := resolveLocalMediaInput(input)
+ if !ok || got != path {
+ t.Fatalf("resolveLocalMediaInput(%q) = %q, %v; want %q, true", input, got, ok, path)
+ }
+ }
+
+ if got, ok := resolveLocalMediaInput(filepath.Join(dir, "missing.mp4")); ok || got != "" {
+ t.Fatalf("missing input = %q, %v; want empty, false", got, ok)
+ }
+}