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.

feat: add cli command parser

wulien6c98bf4663a5
diff --git a/cmd/cli/main.go b/cmd/cli/main.go
new file mode 100644
index 00000000..ea98b17c
--- /dev/null
+++ b/cmd/cli/main.go
@@ -0,0 +1,67 @@
+package main
+
+import (
+ "context"
+ "encoding/json"
+ "fmt"
+ "krillin-ai/config"
+ "krillin-ai/internal/cli"
+ "krillin-ai/internal/deps"
+ "krillin-ai/internal/pipeline"
+ "krillin-ai/internal/service"
+ "krillin-ai/log"
+ "os"
+)
+
+func main() {
+ log.InitLogger()
+ defer log.GetLogger().Sync()
+
+ if !config.LoadConfig() {
+ writeAndExit(pipeline.Response{
+ OK: false,
+ Error: &pipeline.Error{
+ Kind: pipeline.ErrorKindUsage,
+ Code: "config_not_found",
+ Message: "未找到配置文件",
+ },
+ })
+ }
+ if err := config.CheckConfig(); err != nil {
+ writeAndExit(errorResponse(err, pipeline.ErrorKindUsage))
+ }
+ if err := deps.CheckDependency(); err != nil {
+ writeAndExit(errorResponse(err, pipeline.ErrorKindDependency))
+ }
+ cmd, err := cli.Parse(os.Args[1:])
+ if err != nil {
+ writeAndExit(errorResponse(err, pipeline.ErrorKindUsage))
+ }
+ svc := service.NewService()
+ adapter := pipeline.NewServiceAdapter(svc)
+ writeAndExit(cli.Execute(context.Background(), adapter, cmd))
+}
+
+func errorResponse(err error, kind pipeline.ErrorKind) pipeline.Response {
+ return pipeline.Response{
+ OK: false,
+ Error: &pipeline.Error{
+ Kind: kind,
+ Code: string(kind),
+ Message: err.Error(),
+ Retryable: kind == pipeline.ErrorKindRetryable,
+ },
+ }
+}
+
+func writeAndExit(resp pipeline.Response) {
+ data, err := json.Marshal(resp)
+ if 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/internal/cli/commands.go b/internal/cli/commands.go
new file mode 100644
index 00000000..df2d0769
--- /dev/null
+++ b/internal/cli/commands.go
@@ -0,0 +1,206 @@
+package cli
+
+import (
+ "context"
+ "errors"
+ "flag"
+ "fmt"
+ "io"
+ "krillin-ai/internal/pipeline"
+ "strings"
+)
+
+type Command struct {
+ Name string
+ Subtitle pipeline.SubtitleRequest
+ TTS pipeline.TTSRequest
+ Render pipeline.RenderRequest
+ Pipeline pipeline.PipelineRequest
+}
+
+func Parse(args []string) (Command, error) {
+ if len(args) == 0 {
+ return Command{}, errors.New("missing command")
+ }
+ name := args[0]
+ switch name {
+ case "subtitle":
+ return parseSubtitle(name, args[1:])
+ case "tts":
+ return parseTTS(name, args[1:])
+ case "render-horizontal":
+ return parseRender(name, args[1:], true)
+ case "render-vertical":
+ return parseRender(name, args[1:], false)
+ case "pipeline":
+ return parsePipeline(name, args[1:])
+ case "cover", "status":
+ return Command{Name: name}, nil
+ default:
+ return Command{}, fmt.Errorf("unknown command: %s", name)
+ }
+}
+
+func Execute(ctx context.Context, svc pipeline.StageService, cmd Command) pipeline.Response {
+ switch cmd.Name {
+ case "subtitle":
+ resp, err := pipeline.GenerateSubtitles(ctx, svc, cmd.Subtitle)
+ return responseWithError(resp, err)
+ case "tts":
+ resp, err := pipeline.GenerateTTS(ctx, svc, cmd.TTS)
+ return responseWithError(resp, err)
+ case "render-horizontal", "render-vertical":
+ resp, err := pipeline.Render(ctx, svc, cmd.Render)
+ return responseWithError(resp, err)
+ default:
+ return pipeline.Response{
+ OK: false,
+ Error: &pipeline.Error{
+ Kind: pipeline.ErrorKindUsage,
+ Code: "unsupported_command",
+ Message: fmt.Sprintf("unsupported command: %s", cmd.Name),
+ },
+ }
+ }
+}
+
+func parseSubtitle(name string, args []string) (Command, error) {
+ fs := newFlagSet(name)
+ originLang := fs.String("origin-lang", "", "origin language")
+ targetLang := fs.String("target-lang", "", "target language")
+ userLang := fs.String("user-lang", "", "user interface language")
+ workdir := fs.String("workdir", "", "workdir")
+ taskID := fs.String("task-id", "", "task id")
+ captionSource := fs.String("caption-source", string(pipeline.CaptionSourceAny), "caption source")
+ bilingualTop := fs.Bool("bilingual-top", false, "put target subtitle on top")
+ maxWordOneLine := fs.Int("max-word-one-line", 0, "max words per line")
+ input := ""
+ parseArgs := args
+ if len(args) > 0 && !strings.HasPrefix(args[0], "-") {
+ input = args[0]
+ parseArgs = args[1:]
+ }
+ if err := fs.Parse(parseArgs); err != nil {
+ return Command{}, err
+ }
+ if input == "" && fs.NArg() == 1 {
+ input = fs.Arg(0)
+ }
+ if input == "" || fs.NArg() > 1 {
+ return Command{}, errors.New("subtitle requires input")
+ }
+ return Command{
+ Name: name,
+ Subtitle: pipeline.SubtitleRequest{
+ Input: input,
+ Workdir: *workdir,
+ TaskID: *taskID,
+ OriginLang: *originLang,
+ TargetLang: *targetLang,
+ UserLang: *userLang,
+ CaptionSource: pipeline.CaptionSource(*captionSource),
+ BilingualTop: *bilingualTop,
+ MaxWordOneLine: *maxWordOneLine,
+ },
+ }, nil
+}
+
+func parseTTS(name string, args []string) (Command, error) {
+ fs := newFlagSet(name)
+ workdir := fs.String("workdir", "", "workdir")
+ taskID := fs.String("task-id", "", "task id")
+ inputSRT := fs.String("input-srt", "", "input srt")
+ lineMode := fs.String("line-mode", string(pipeline.LineModeTargetOnly), "line mode")
+ video := fs.String("video", "", "input video")
+ voice := fs.String("voice", "", "voice")
+ voiceCloneSource := fs.String("voice-clone-source", "", "voice clone source")
+ if err := fs.Parse(args); err != nil {
+ return Command{}, err
+ }
+ if *inputSRT == "" {
+ return Command{}, errors.New("tts requires --input-srt")
+ }
+ return Command{
+ Name: name,
+ TTS: pipeline.TTSRequest{
+ Workdir: *workdir,
+ TaskID: *taskID,
+ InputSRT: *inputSRT,
+ LineMode: pipeline.LineMode(*lineMode),
+ Video: *video,
+ Voice: *voice,
+ VoiceCloneSource: *voiceCloneSource,
+ },
+ }, nil
+}
+
+func parseRender(name string, args []string, horizontal bool) (Command, error) {
+ fs := newFlagSet(name)
+ workdir := fs.String("workdir", "", "workdir")
+ taskID := fs.String("task-id", "", "task id")
+ video := fs.String("video", "", "input video")
+ audio := fs.String("audio", "", "input audio")
+ subtitle := fs.String("subtitle", "", "subtitle")
+ dubbed := fs.Bool("dubbed", false, "render dubbed video")
+ majorTitle := fs.String("major-title", "", "vertical major title")
+ minorTitle := fs.String("minor-title", "", "vertical minor title")
+ if err := fs.Parse(args); err != nil {
+ return Command{}, err
+ }
+ return Command{
+ Name: name,
+ Render: pipeline.RenderRequest{
+ Workdir: *workdir,
+ TaskID: *taskID,
+ Video: *video,
+ Audio: *audio,
+ Subtitle: *subtitle,
+ Horizontal: horizontal,
+ Dubbed: *dubbed,
+ MajorTitle: *majorTitle,
+ MinorTitle: *minorTitle,
+ },
+ }, nil
+}
+
+func parsePipeline(name string, args []string) (Command, error) {
+ fs := newFlagSet(name)
+ outputs := fs.String("outputs", "subtitle", "outputs")
+ async := fs.Bool("async", false, "run async")
+ if err := fs.Parse(args); err != nil {
+ return Command{}, err
+ }
+ if _, err := pipeline.PlanOutputs(*outputs); err != nil {
+ return Command{}, err
+ }
+ return Command{
+ Name: name,
+ Pipeline: pipeline.PipelineRequest{
+ Outputs: *outputs,
+ Async: *async,
+ },
+ }, nil
+}
+
+func newFlagSet(name string) *flag.FlagSet {
+ fs := flag.NewFlagSet(name, flag.ContinueOnError)
+ fs.SetOutput(io.Discard)
+ return fs
+}
+
+func responseWithError(resp pipeline.Response, err error) pipeline.Response {
+ if err == nil {
+ return resp
+ }
+ if resp.Error != nil {
+ return resp
+ }
+ resp.OK = false
+ resp.Error = &pipeline.Error{
+ Kind: pipeline.ErrorKindRetryable,
+ Code: "command_failed",
+ Message: err.Error(),
+ Retryable: true,
+ }
+ return resp
+}
diff --git a/internal/cli/commands_test.go b/internal/cli/commands_test.go
new file mode 100644
index 00000000..3f01bc6b
--- /dev/null
+++ b/internal/cli/commands_test.go
@@ -0,0 +1,33 @@
+package cli
+
+import "testing"
+
+func TestParseSubtitleCommand(t *testing.T) {
+ cmd, err := Parse([]string{
+ "subtitle",
+ "https://www.youtube.com/watch?v=abc",
+ "--origin-lang", "en",
+ "--target-lang", "zh_cn",
+ "--workdir", "tasks/demo",
+ "--caption-source", "any",
+ })
+ if err != nil {
+ t.Fatalf("Parse() error = %v", err)
+ }
+ if cmd.Name != "subtitle" {
+ t.Fatalf("Name = %q, want subtitle", cmd.Name)
+ }
+ if cmd.Subtitle.Input != "https://www.youtube.com/watch?v=abc" {
+ t.Fatalf("Input = %q", cmd.Subtitle.Input)
+ }
+ if cmd.Subtitle.Workdir != "tasks/demo" {
+ t.Fatalf("Workdir = %q", cmd.Subtitle.Workdir)
+ }
+}
+
+func TestParseTTSCommandRequiresInputSRT(t *testing.T) {
+ _, err := Parse([]string{"tts", "--workdir", "tasks/demo"})
+ if err == nil {
+ t.Fatalf("Parse() error = nil, want error")
+ }
+}