diff --git a/pkg/argparse/argparse.go b/pkg/argparse/argparse.go index e9637bc..88a2502 100644 --- a/pkg/argparse/argparse.go +++ b/pkg/argparse/argparse.go @@ -20,6 +20,7 @@ type Opts struct { Days int NoSync bool MarkWatched bool + Format bool } func Parse(version string) *Opts { diff --git a/pkg/config/config.go b/pkg/config/config.go index 4255ae3..cec97a5 100644 --- a/pkg/config/config.go +++ b/pkg/config/config.go @@ -13,6 +13,7 @@ type Config struct { VideoFolder string `toml:"videoFolder"` URLS []string `toml:"urls"` Feeds map[string]Feed `toml:"feed"` + Quality string `toml:"quality"` } type Feed struct { URL string `toml:"url"` @@ -59,6 +60,10 @@ func checkConfig(conf *Config) { help.HelpConfig() goreland.LogFatal("No URLs set in config file") } + if conf.Quality == "" { + help.HelpConfig() + goreland.LogFatal("Quality not set in config file") + } } func readConfig(path string) *Config { diff --git a/pkg/help/help.go b/pkg/help/help.go index 70a1969..6c07f6b 100644 --- a/pkg/help/help.go +++ b/pkg/help/help.go @@ -39,7 +39,7 @@ func HelpUpdate() { func HelpDownload() { pelp.Print("Download a video") pelp.HeaderWithDescription("Usage", []string{"sinister download"}) - pelp.Flags("flag", []string{"no-spinner"}, []string{"Disable spinner"}) + pelp.Flags("flag", []string{"no-spinner", "select-format"}, []string{"Disable spinner", "Select format manually"}) pelp.HeaderWithDescription("Description", []string{ "Prompt for a creator and a video to download.", diff --git a/pkg/tui/download.go b/pkg/tui/download.go index 1b692d4..075f021 100644 --- a/pkg/tui/download.go +++ b/pkg/tui/download.go @@ -2,6 +2,7 @@ package tui import ( "flag" + "fmt" "io" "os" "path/filepath" @@ -21,6 +22,7 @@ import ( func parseDownloadArgs(opts *argparse.Opts) { flag := flag.NewFlagSet("sinister download", flag.ExitOnError) flag.BoolVar(&opts.NoSpinner, "no-spinner", false, "Disable spinner") + flag.BoolVar(&opts.Format, "select-format", false, "Select format manually") flag.Usage = help.HelpDownload flag.Parse(opts.Args[1:]) } @@ -75,7 +77,7 @@ func selectEntry() *feed.Entry { func startSpinner(opts *argparse.Opts) func() { - if opts.NoSpinner { + if opts.NoSpinner || opts.Format { return func() {} } @@ -113,11 +115,31 @@ func getVideo(entry *feed.Entry) (*youtube.Client, *youtube.Video) { return &client, video } -func selectFormat(video *youtube.Video) *youtube.Format { - formats := video.Formats.Quality("720p").WithAudioChannels() +func formatListString(formats youtube.FormatList) []string { + results := []string{} + for _, format := range formats { + line := fmt.Sprintf("Quality: %s, Bitrate: %d", format.Quality, format.Bitrate) + results = append(results, line) + } + + return results +} +func selectFormat(video *youtube.Video, opts *argparse.Opts) *youtube.Format { + formats := video.Formats formats.Sort() + if opts.Format { + index := promptSelection("Select format", formatListString(formats)) + return &formats[index] + } + + conf := config.ParseConfig(opts) + + formatQuery := conf.Quality + + formats = formats.Quality(formatQuery).WithAudioChannels() + if len(formats) == 0 { goreland.LogFatal("No suitable formats found") } @@ -138,7 +160,7 @@ func downloadVideo(entry *feed.Entry, opts *argparse.Opts) { client, video := getVideo(entry) - format := selectFormat(video) + format := selectFormat(video, opts) stream := getStream(client, video, format)