Skip to content

Commit

Permalink
Add --audio-only flag (#1304)
Browse files Browse the repository at this point in the history
* Add --audio-only flag

* Resolve all conflicts

* Fix go lint errors
  • Loading branch information
GNUSheep committed Jan 5, 2024
1 parent af1802e commit bd142b2
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
6 changes: 6 additions & 0 deletions app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"},
Expand Down Expand Up @@ -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"),
Expand Down
21 changes: 21 additions & 0 deletions downloader/downloader.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ type Options struct {
InfoOnly bool
Silent bool
Stream string
AudioOnly bool
Refer string
OutputPath string
OutputName string
Expand Down Expand Up @@ -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)
}
Expand Down

0 comments on commit bd142b2

Please sign in to comment.