From efe764098004cebcc38761cf0d7432a8ae137253 Mon Sep 17 00:00:00 2001 From: Mattias Wadman Date: Sat, 13 Jan 2024 17:38:12 +0100 Subject: [PATCH] By default probe for youtube-dl or yt-dlp binary Still possible to override with Path --- README.md | 17 +++++++++-------- goutubedl.go | 26 ++++++++++++++++++++------ goutubedl_test.go | 6 ------ 3 files changed, 29 insertions(+), 20 deletions(-) diff --git a/README.md b/README.md index 344f6b5..fbe4588 100644 --- a/README.md +++ b/README.md @@ -1,18 +1,19 @@ ## goutubedl -Go wrapper for [youtube-dl](https://github.com/ytdl-org/youtube-dl) and [yt-dlp](https://github.com/yt-dlp/yt-dlp), currently tested and -developed using yt-dlp. +Go wrapper for +[youtube-dl](https://github.com/ytdl-org/youtube-dl) and +[yt-dlp](https://github.com/yt-dlp/yt-dlp). +Currently only tested and developed using yt-dlp. API documentation can be found at [godoc.org](https://pkg.go.dev/github.com/wader/goutubedl?tab=doc). See [yt-dlp documentation](https://github.com/yt-dlp/yt-dlp) for how to -install and what is recommended to install in addition to youtube-dl. +install and what is recommended to install in addition to yt-dl. -goutubedl default uses `PATH` to find youtube-dl but it can be configured with the `goutubedl.Path` -variable. Default is currently `youtube-dl` for backwards compability. If your using yt-dlp you -probably want to set it to `yt-dlp`. +goutubedl default uses `PATH` to find `youtube-dl` or `yt-dlp` (in that order) but can be configured with the +`goutubedl.Path` variable. -Due to the nature and frequent updates of youtube-dl only the latest version -is tested. But it seems to work well with older versions also. +Due to the nature of and frequent updates of yt-dl only the latest version is tested. +But it seems to work well with older versions also. ### Usage diff --git a/goutubedl.go b/goutubedl.go index 6340d1a..7b86e6a 100644 --- a/goutubedl.go +++ b/goutubedl.go @@ -18,8 +18,22 @@ import ( "strings" ) -// Path to youtube-dl binary. Default look for "youtube-dl" in PATH. -var Path = "youtube-dl" +// Path to youtube-dl binary. If not set look for "youtube-dl" then "yt-dlp" in PATH. +var Path = "" + +func ProbePath() string { + if Path != "" { + return Path + } + + for _, n := range []string{"youtube-dl", "yt-dlp"} { + if p, err := exec.LookPath(n); err == nil { + return p + } + } + + return "youtube-dl" +} // Printer is something that can print type Printer interface { @@ -223,7 +237,7 @@ type Options struct { // Version of youtube-dl. // Might be a good idea to call at start to assert that youtube-dl can be found. func Version(ctx context.Context) (string, error) { - cmd := exec.CommandContext(ctx, Path, "--version") + cmd := exec.CommandContext(ctx, ProbePath(), "--version") versionBytes, cmdErr := cmd.Output() if cmdErr != nil { return "", cmdErr @@ -275,7 +289,7 @@ func New(ctx context.Context, rawURL string, options Options) (result Result, er func infoFromURL(ctx context.Context, rawURL string, options Options) (info Info, rawJSON []byte, err error) { cmd := exec.CommandContext( ctx, - Path, + ProbePath(), // see comment below about ignoring errors for playlists "--ignore-errors", "--no-call-home", @@ -503,7 +517,7 @@ func (result Result) DownloadWithOptions(ctx context.Context, options DownloadOp cmd := exec.CommandContext( ctx, - Path, + ProbePath(), "--no-call-home", "--no-cache-dir", "--ignore-errors", @@ -556,7 +570,7 @@ func (result Result) DownloadWithOptions(ctx context.Context, options DownloadOp if result.Options.Downloader != "" { cmd.Args = append(cmd.Args, "--downloader", result.Options.Downloader) } - + if result.Options.DownloadSections != "" { cmd.Args = append(cmd.Args, "--download-sections", result.Options.DownloadSections) } diff --git a/goutubedl_test.go b/goutubedl_test.go index ce9f3b5..49cd334 100644 --- a/goutubedl_test.go +++ b/goutubedl_test.go @@ -21,11 +21,6 @@ import ( "github.com/wader/osleaktest" ) -func init() { - // we're using yt-dlp at the moment - goutubedl.Path = "yt-dlp" -} - const testVideoRawURL = "https://www.youtube.com/watch?v=C0DPdy98e4c" const playlistRawURL = "https://soundcloud.com/mattheis/sets/kindred-phenomena" const subtitlesTestVideoRawURL = "https://www.youtube.com/watch?v=QRS8MkLhQmM" @@ -307,7 +302,6 @@ func TestDownloadSections(t *testing.T) { DownloadSections: fmt.Sprintf("*0:0-0:%d", duration), }) - if ydlResult.Options.DownloadSections != "*0:0-0:5" { t.Errorf("failed to setup --download-sections") }