From bd142b2c33806ee70c6f472f5fd9930c050ec807 Mon Sep 17 00:00:00 2001 From: Shaun_Sheep <54682807+GNUSheep@users.noreply.github.com> Date: Fri, 5 Jan 2024 02:46:08 +0100 Subject: [PATCH] Add --audio-only flag (#1304) * Add --audio-only flag * Resolve all conflicts * Fix go lint errors --- app/app.go | 6 ++++++ downloader/downloader.go | 21 +++++++++++++++++++++ 2 files changed, 27 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..c70e71787 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,26 @@ func (downloader *Downloader) Download(data *extractors.Data) error { return errors.Errorf("no stream named %s", streamName) } + 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 + if reg.MatchString(s.Quality) { + isFound = true + stream = data.Streams[s.ID] + break + } + } + if !isFound { + return errors.Errorf("No audio stream found") + } + } + if !downloader.option.Silent { printStreamInfo(data, stream) }