Skip to content

Commit

Permalink
Added support for Gen3
Browse files Browse the repository at this point in the history
Fixed Gen2 and added support for Gen3.
Refactored command and package structure.
  • Loading branch information
igolaizola committed Jul 3, 2024
1 parent 0843812 commit 75cfd09
Show file tree
Hide file tree
Showing 13 changed files with 991 additions and 718 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,6 @@ bin
.history
.vscode
*.conf
*.yaml
logs/
cfg/
186 changes: 2 additions & 184 deletions cmd/vidai/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,11 @@ package main

import (
"context"
"flag"
"fmt"
"log"
"os"
"os/signal"
"runtime/debug"
"strings"
"time"

"github.com/igolaizola/vidai"
"github.com/peterbourgon/ff/v3"
"github.com/peterbourgon/ff/v3/ffcli"
"github.com/igolaizola/vidai/pkg/cli"
)

// Build flags
Expand All @@ -27,183 +20,8 @@ func main() {
defer cancel()

// Launch command
cmd := newCommand()
cmd := cli.NewCommand(version, commit, date)
if err := cmd.ParseAndRun(ctx, os.Args[1:]); err != nil {
log.Fatal(err)
}
}

func newCommand() *ffcli.Command {
fs := flag.NewFlagSet("vidai", flag.ExitOnError)

return &ffcli.Command{
ShortUsage: "vidai [flags] <subcommand>",
FlagSet: fs,
Exec: func(context.Context, []string) error {
return flag.ErrHelp
},
Subcommands: []*ffcli.Command{
newVersionCommand(),
newGenerateCommand(),
newExtendCommand(),
newLoopCommand(),
},
}
}

func newVersionCommand() *ffcli.Command {
return &ffcli.Command{
Name: "version",
ShortUsage: "vidai version",
ShortHelp: "print version",
Exec: func(ctx context.Context, args []string) error {
v := version
if v == "" {
if buildInfo, ok := debug.ReadBuildInfo(); ok {
v = buildInfo.Main.Version
}
}
if v == "" {
v = "dev"
}
versionFields := []string{v}
if commit != "" {
versionFields = append(versionFields, commit)
}
if date != "" {
versionFields = append(versionFields, date)
}
fmt.Println(strings.Join(versionFields, " "))
return nil
},
}
}

func newGenerateCommand() *ffcli.Command {
cmd := "generate"
fs := flag.NewFlagSet(cmd, flag.ExitOnError)
_ = fs.String("config", "", "config file (optional)")

var cfg vidai.Config
fs.BoolVar(&cfg.Debug, "debug", false, "debug mode")
fs.DurationVar(&cfg.Wait, "wait", 2*time.Second, "wait time between requests")
fs.StringVar(&cfg.Token, "token", "", "runway token")
image := fs.String("image", "", "source image")
text := fs.String("text", "", "source text")
output := fs.String("output", "", "output file (optional, if omitted it won't be saved)")
extend := fs.Int("extend", 0, "extend the video by this many times (optional)")
interpolate := fs.Bool("interpolate", true, "interpolate frames (optional)")
upscale := fs.Bool("upscale", false, "upscale frames (optional)")
watermark := fs.Bool("watermark", false, "add watermark (optional)")

return &ffcli.Command{
Name: cmd,
ShortUsage: fmt.Sprintf("vidai %s [flags] <key> <value data...>", cmd),
Options: []ff.Option{
ff.WithConfigFileFlag("config"),
ff.WithConfigFileParser(ff.PlainParser),
ff.WithEnvVarPrefix("VIDAI"),
},
ShortHelp: fmt.Sprintf("vidai %s command", cmd),
FlagSet: fs,
Exec: func(ctx context.Context, args []string) error {
if cfg.Token == "" {
return fmt.Errorf("token is required")
}
if *image == "" && *text == "" {
return fmt.Errorf("image or text is required")
}
c := vidai.New(&cfg)
id, u, err := c.Generate(ctx, *image, *text, *output, *extend,
*interpolate, *upscale, *watermark)
if err != nil {
return err
}
fmt.Printf("ID: %s URL: %s\n", id, u)
return nil
},
}
}

func newExtendCommand() *ffcli.Command {
cmd := "extend"
fs := flag.NewFlagSet(cmd, flag.ExitOnError)
_ = fs.String("config", "", "config file (optional)")

var cfg vidai.Config
fs.BoolVar(&cfg.Debug, "debug", false, "debug mode")
fs.DurationVar(&cfg.Wait, "wait", 2*time.Second, "wait time between requests")
fs.StringVar(&cfg.Token, "token", "", "runway token")
input := fs.String("input", "", "input video")
output := fs.String("output", "", "output file (optional, if omitted it won't be saved)")
n := fs.Int("n", 1, "extend the video by this many times")
interpolate := fs.Bool("interpolate", true, "interpolate frames (optional)")
upscale := fs.Bool("upscale", false, "upscale frames (optional)")
watermark := fs.Bool("watermark", false, "add watermark (optional)")

return &ffcli.Command{
Name: cmd,
ShortUsage: fmt.Sprintf("vidai %s [flags] <key> <value data...>", cmd),
Options: []ff.Option{
ff.WithConfigFileFlag("config"),
ff.WithConfigFileParser(ff.PlainParser),
ff.WithEnvVarPrefix("VIDAI"),
},
ShortHelp: fmt.Sprintf("vidai %s command", cmd),
FlagSet: fs,
Exec: func(ctx context.Context, args []string) error {
if cfg.Token == "" {
return fmt.Errorf("token is required")
}
if *input == "" {
return fmt.Errorf("input is required")
}
if *n < 1 {
return fmt.Errorf("n must be greater than 0")
}

c := vidai.New(&cfg)
urls, err := c.Extend(ctx, *input, *output, *n, *interpolate, *upscale, *watermark)
if err != nil {
return err
}
for i, u := range urls {
fmt.Printf("Video URL %d: %s\n", i+1, u)
}
return nil
},
}
}

func newLoopCommand() *ffcli.Command {
cmd := "loop"
fs := flag.NewFlagSet(cmd, flag.ExitOnError)
_ = fs.String("config", "", "config file (optional)")

input := fs.String("input", "", "input video")
output := fs.String("output", "", "output file")

return &ffcli.Command{
Name: cmd,
ShortUsage: fmt.Sprintf("vidai %s [flags] <key> <value data...>", cmd),
Options: []ff.Option{
ff.WithConfigFileFlag("config"),
ff.WithConfigFileParser(ff.PlainParser),
ff.WithEnvVarPrefix("VIDAI"),
},
ShortHelp: fmt.Sprintf("vidai %s command", cmd),
FlagSet: fs,
Exec: func(ctx context.Context, args []string) error {
if *input == "" {
return fmt.Errorf("input is required")
}
if *output == "" {
return fmt.Errorf("output is required")
}
if err := vidai.Loop(ctx, *input, *output); err != nil {
return err
}
return nil
},
}
}
22 changes: 21 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,24 @@ module github.com/igolaizola/vidai

go 1.20

require github.com/peterbourgon/ff/v3 v3.3.0
require (
github.com/bogdanfinn/fhttp v0.5.28
github.com/bogdanfinn/tls-client v1.7.5
github.com/peterbourgon/ff/v3 v3.3.0
)

require (
github.com/andybalholm/brotli v1.0.5 // indirect
github.com/bogdanfinn/utls v1.6.1 // indirect
github.com/cloudflare/circl v1.3.6 // indirect
github.com/klauspost/compress v1.16.7 // indirect
github.com/kr/text v0.2.0 // indirect
github.com/quic-go/quic-go v0.37.4 // indirect
github.com/rogpeppe/go-internal v1.12.0 // indirect
github.com/tam7t/hpkp v0.0.0-20160821193359-2b70b4024ed5 // indirect
golang.org/x/crypto v0.14.0 // indirect
golang.org/x/net v0.17.0 // indirect
golang.org/x/sys v0.13.0 // indirect
golang.org/x/text v0.13.0 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
)
42 changes: 42 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,2 +1,44 @@
github.com/andybalholm/brotli v1.0.5 h1:8uQZIdzKmjc/iuPu7O2ioW48L81FgatrcpfFmiq/cCs=
github.com/andybalholm/brotli v1.0.5/go.mod h1:fO7iG3H7G2nSZ7m0zPUDn85XEX2GTukHGRSepvi9Eig=
github.com/bogdanfinn/fhttp v0.5.28 h1:G6thT8s8v6z1IuvXMUsX9QKy3ZHseTQTzxuIhSiaaAw=
github.com/bogdanfinn/fhttp v0.5.28/go.mod h1:oJiYPG3jQTKzk/VFmogH8jxjH5yiv2rrOH48Xso2lrE=
github.com/bogdanfinn/tls-client v1.7.5 h1:R1aTwe5oja5niLnQggzbWnzJEssw9n+3O4kR0H/Tjl4=
github.com/bogdanfinn/tls-client v1.7.5/go.mod h1:pQwF0eqfL0gf0mu8hikvu6deZ3ijSPruJDzEKEnnXjU=
github.com/bogdanfinn/utls v1.6.1 h1:dKDYAcXEyFFJ3GaWaN89DEyjyRraD1qb4osdEK89ass=
github.com/bogdanfinn/utls v1.6.1/go.mod h1:VXIbRZaiY/wHZc6Hu+DZ4O2CgTzjhjCg/Ou3V4r/39Y=
github.com/cloudflare/circl v1.3.6 h1:/xbKIqSHbZXHwkhbrhrt2YOHIwYJlXH94E3tI/gDlUg=
github.com/cloudflare/circl v1.3.6/go.mod h1:5XYMA4rFBvNIrhs50XuiBJ15vF2pZn4nnUKZrLbUZFA=
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
github.com/go-logr/logr v1.2.4 h1:g01GSCwiDw2xSZfjJ2/T9M+S6pFdcNtFYsp+Y43HYDQ=
github.com/go-task/slim-sprig v0.0.0-20230315185526-52ccab3ef572 h1:tfuBGBXKqDEevZMzYi5KSi8KkcZtzBcTgAUUtapy0OI=
github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38=
github.com/google/pprof v0.0.0-20210407192527-94a9f03dee38 h1:yAJXTCF9TqKcTiHJAE8dj7HMvPfh66eeA2JYW7eFpSE=
github.com/klauspost/compress v1.16.7 h1:2mk3MPGNzKyxErAw8YaohYh69+pa4sIQSC0fPGCFR9I=
github.com/klauspost/compress v1.16.7/go.mod h1:ntbaceVETuRiXiv4DpjP66DpAtAGkEQskQzEyD//IeE=
github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE=
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
github.com/onsi/ginkgo/v2 v2.9.5 h1:+6Hr4uxzP4XIUyAkg61dWBw8lb/gc4/X5luuxN/EC+Q=
github.com/onsi/gomega v1.27.6 h1:ENqfyGeS5AX/rlXDd/ETokDz93u0YufY1Pgxuy/PvWE=
github.com/peterbourgon/ff/v3 v3.3.0 h1:PaKe7GW8orVFh8Unb5jNHS+JZBwWUMa2se0HM6/BI24=
github.com/peterbourgon/ff/v3 v3.3.0/go.mod h1:zjJVUhx+twciwfDl0zBcFzl4dW8axCRyXE/eKY9RztQ=
github.com/quic-go/quic-go v0.37.4 h1:ke8B73yMCWGq9MfrCCAw0Uzdm7GaViC3i39dsIdDlH4=
github.com/quic-go/quic-go v0.37.4/go.mod h1:YsbH1r4mSHPJcLF4k4zruUkLBqctEMBDR6VPvcYjIsU=
github.com/rogpeppe/go-internal v1.12.0 h1:exVL4IDcn6na9z1rAb56Vxr+CgyK3nn3O+epU5NdKM8=
github.com/rogpeppe/go-internal v1.12.0/go.mod h1:E+RYuTGaKKdloAfM02xzb0FW3Paa99yedzYV+kq4uf4=
github.com/tam7t/hpkp v0.0.0-20160821193359-2b70b4024ed5 h1:YqAladjX7xpA6BM04leXMWAEjS0mTZ5kUU9KRBriQJc=
github.com/tam7t/hpkp v0.0.0-20160821193359-2b70b4024ed5/go.mod h1:2JjD2zLQYH5HO74y5+aE3remJQvl6q4Sn6aWA2wD1Ng=
golang.org/x/crypto v0.14.0 h1:wBqGXzWJW6m1XrIKlAH0Hs1JJ7+9KBwnIO8v66Q9cHc=
golang.org/x/crypto v0.14.0/go.mod h1:MVFd36DqK4CsrnJYDkBA3VC4m2GkXAM0PvzMCn4JQf4=
golang.org/x/net v0.17.0 h1:pVaXccu2ozPjCXewfr1S7xza/zcXTity9cCdXQYSjIM=
golang.org/x/net v0.17.0/go.mod h1:NxSsAGuq816PNPmqtQdLE42eU2Fs7NoRIZrHJAlaCOE=
golang.org/x/sys v0.13.0 h1:Af8nKPmuFypiUBjVoU9V20FiaFXOcuZI21p0ycVYYGE=
golang.org/x/sys v0.13.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/text v0.13.0 h1:ablQoSUd0tRdKxZewP80B+BaqeKJuVhuRxj/dkrun3k=
golang.org/x/text v0.13.0/go.mod h1:TvPlkZtksWOMsz7fbANvkp4WM8x/WCo/om8BMLbz+aE=
golang.org/x/tools v0.9.1 h1:8WMNJAz3zrtPmnYC7ISf5dEn3MT0gY7jBJfw27yrrLo=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 h1:qIbj1fsPNlZgppZ+VLlY7N33q108Sa+fhmuc+sWQYwY=
gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
Loading

0 comments on commit 75cfd09

Please sign in to comment.