From cd5853da3986313f55cb90ab802a50333844fcfb Mon Sep 17 00:00:00 2001 From: GNUSheep Date: Tue, 2 Jan 2024 20:34:25 +0100 Subject: [PATCH 1/3] Add --audio-only flag --- app/app.go | 6 ++++++ downloader/downloader.go | 15 +++++++++++++++ 2 files changed, 21 insertions(+) diff --git a/app/app.go b/app/app.go index 05167995f..13a3da127 100644 --- a/app/app.go +++ b/app/app.go @@ -89,6 +89,11 @@ func New() *cli.App { Aliases: []string{"f"}, Usage: "Select specific stream to download", }, + &cli.BoolFlag{ + Name: "audio-only", + Aliases: []string{"ao"}, + Usage: "Download audio only at best quality", + }, &cli.StringFlag{ Name: "file", Aliases: []string{"F"}, @@ -300,6 +305,7 @@ func download(c *cli.Context, videoURL string) error { Silent: c.Bool("silent"), InfoOnly: c.Bool("info"), Stream: c.String("stream-format"), + AudioOnly: c.Bool("audio-only"), Refer: c.String("refer"), OutputPath: c.String("output-path"), OutputName: c.String("output-name"), diff --git a/downloader/downloader.go b/downloader/downloader.go index 8537ab487..8baeec7f4 100644 --- a/downloader/downloader.go +++ b/downloader/downloader.go @@ -28,6 +28,7 @@ type Options struct { InfoOnly bool Silent bool Stream string + AudioOnly bool Refer string OutputPath string OutputName string @@ -568,6 +569,20 @@ func (downloader *Downloader) Download(data *extractors.Data) error { return errors.Errorf("no stream named %s", streamName) } + if downloader.option.AudioOnly { + for _, s := range sortedStreams { + // Looking for the best quality + matches, ok := regexp.MatchString("audio", s.Quality) + if ok != nil { + return errors.Errorf("No audio stream found") + } + if matches { + stream, _ = data.Streams[s.ID] + break + } + } + } + if !downloader.option.Silent { printStreamInfo(data, stream) } From 31d3adf0f80e1d697e1863e18958c03ebbe58f1c Mon Sep 17 00:00:00 2001 From: GNUSheep Date: Wed, 3 Jan 2024 17:35:49 +0100 Subject: [PATCH 2/3] Resolve all conflicts --- downloader/downloader.go | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/downloader/downloader.go b/downloader/downloader.go index 8baeec7f4..e1f4ddf5c 100644 --- a/downloader/downloader.go +++ b/downloader/downloader.go @@ -570,17 +570,22 @@ func (downloader *Downloader) Download(data *extractors.Data) error { } if downloader.option.AudioOnly { + var isFound bool for _, s := range sortedStreams { // Looking for the best quality - matches, ok := regexp.MatchString("audio", s.Quality) - if ok != nil { - return errors.Errorf("No audio stream found") + matches, err := regexp.MatchString("audio", s.Quality) + if err != nil { + return err } if matches { + isFound = true stream, _ = data.Streams[s.ID] break } } + if !isFound { + return errors.Errorf("No audio stream found") + } } if !downloader.option.Silent { From 4c6079482b19c100327696c5a6055570b47ec500 Mon Sep 17 00:00:00 2001 From: GNUSheep Date: Thu, 4 Jan 2024 18:02:07 +0100 Subject: [PATCH 3/3] Fix go lint errors --- downloader/downloader.go | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/downloader/downloader.go b/downloader/downloader.go index e1f4ddf5c..c70e71787 100644 --- a/downloader/downloader.go +++ b/downloader/downloader.go @@ -571,15 +571,16 @@ func (downloader *Downloader) Download(data *extractors.Data) error { if downloader.option.AudioOnly { var isFound bool + reg, err := regexp.Compile("audio+") + if err != nil { + return err + } + for _, s := range sortedStreams { // Looking for the best quality - matches, err := regexp.MatchString("audio", s.Quality) - if err != nil { - return err - } - if matches { + if reg.MatchString(s.Quality) { isFound = true - stream, _ = data.Streams[s.ID] + stream = data.Streams[s.ID] break } }