Merge pull request #170 from wader/path-probe

By default probe for youtube-dl or yt-dlp binary
This commit is contained in:
Mattias Wadman
2024-01-13 18:11:34 +01:00
committed by GitHub
3 changed files with 29 additions and 20 deletions

View File

@ -1,18 +1,19 @@
## goutubedl ## 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 Go wrapper for
developed using yt-dlp. [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). 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 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` goutubedl default uses `PATH` to find `youtube-dl` or `yt-dlp` (in that order) but can be configured with the
variable. Default is currently `youtube-dl` for backwards compability. If your using yt-dlp you `goutubedl.Path` variable.
probably want to set it to `yt-dlp`.
Due to the nature and frequent updates of youtube-dl only the latest version Due to the nature of and frequent updates of yt-dl only the latest version is tested.
is tested. But it seems to work well with older versions also. But it seems to work well with older versions also.
### Usage ### Usage

View File

@ -18,8 +18,22 @@ import (
"strings" "strings"
) )
// Path to youtube-dl binary. Default look for "youtube-dl" in PATH. // Path to youtube-dl binary. If not set look for "youtube-dl" then "yt-dlp" in PATH.
var Path = "youtube-dl" 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 // Printer is something that can print
type Printer interface { type Printer interface {
@ -223,7 +237,7 @@ type Options struct {
// Version of youtube-dl. // Version of youtube-dl.
// Might be a good idea to call at start to assert that youtube-dl can be found. // Might be a good idea to call at start to assert that youtube-dl can be found.
func Version(ctx context.Context) (string, error) { func Version(ctx context.Context) (string, error) {
cmd := exec.CommandContext(ctx, Path, "--version") cmd := exec.CommandContext(ctx, ProbePath(), "--version")
versionBytes, cmdErr := cmd.Output() versionBytes, cmdErr := cmd.Output()
if cmdErr != nil { if cmdErr != nil {
return "", cmdErr 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) { func infoFromURL(ctx context.Context, rawURL string, options Options) (info Info, rawJSON []byte, err error) {
cmd := exec.CommandContext( cmd := exec.CommandContext(
ctx, ctx,
Path, ProbePath(),
// see comment below about ignoring errors for playlists // see comment below about ignoring errors for playlists
"--ignore-errors", "--ignore-errors",
"--no-call-home", "--no-call-home",
@ -503,7 +517,7 @@ func (result Result) DownloadWithOptions(ctx context.Context, options DownloadOp
cmd := exec.CommandContext( cmd := exec.CommandContext(
ctx, ctx,
Path, ProbePath(),
"--no-call-home", "--no-call-home",
"--no-cache-dir", "--no-cache-dir",
"--ignore-errors", "--ignore-errors",
@ -556,7 +570,7 @@ func (result Result) DownloadWithOptions(ctx context.Context, options DownloadOp
if result.Options.Downloader != "" { if result.Options.Downloader != "" {
cmd.Args = append(cmd.Args, "--downloader", result.Options.Downloader) cmd.Args = append(cmd.Args, "--downloader", result.Options.Downloader)
} }
if result.Options.DownloadSections != "" { if result.Options.DownloadSections != "" {
cmd.Args = append(cmd.Args, "--download-sections", result.Options.DownloadSections) cmd.Args = append(cmd.Args, "--download-sections", result.Options.DownloadSections)
} }

View File

@ -21,11 +21,6 @@ import (
"github.com/wader/osleaktest" "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 testVideoRawURL = "https://www.youtube.com/watch?v=C0DPdy98e4c"
const playlistRawURL = "https://soundcloud.com/mattheis/sets/kindred-phenomena" const playlistRawURL = "https://soundcloud.com/mattheis/sets/kindred-phenomena"
const subtitlesTestVideoRawURL = "https://www.youtube.com/watch?v=QRS8MkLhQmM" 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), DownloadSections: fmt.Sprintf("*0:0-0:%d", duration),
}) })
if ydlResult.Options.DownloadSections != "*0:0-0:5" { if ydlResult.Options.DownloadSections != "*0:0-0:5" {
t.Errorf("failed to setup --download-sections") t.Errorf("failed to setup --download-sections")
} }