From ddaf5ad2fa131a7d481a6a9e2619598100374720 Mon Sep 17 00:00:00 2001 From: Nonoo Date: Thu, 17 Aug 2023 11:18:47 +0200 Subject: [PATCH 1/3] Add the NoInfoDownload option Set it to true if you don't want to use the result.Info structure. If it is set to true, then the New() call won't call youtube-dl to request info about the source in JSON format, and result.Info structure will not be populated. The given URL will be downloaded in a single pass in the Download() call. --- goutubedl.go | 60 ++++++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 53 insertions(+), 7 deletions(-) diff --git a/goutubedl.go b/goutubedl.go index 6a14b65..7b24a68 100644 --- a/goutubedl.go +++ b/goutubedl.go @@ -213,6 +213,10 @@ type Options struct { HTTPClient *http.Client // Client for download thumbnail and subtitles (nil use http.DefaultClient) MergeOutputFormat string // --merge-output-format SortingFormat string // --format-sort + + // Set to true if you don't want to use the result.Info structure after the goutubedl.New() call, + // so the given URL will be downloaded in a single pass in the Download() call. + NoInfoDownload bool } // Version of youtube-dl. @@ -233,6 +237,13 @@ func New(ctx context.Context, rawURL string, options Options) (result Result, er options.DebugLog = nopPrinter{} } + if options.NoInfoDownload { + return Result{ + RawURL: rawURL, + Options: options, + }, nil + } + info, rawJSON, err := infoFromURL(ctx, rawURL, options) if err != nil { return Result{}, err @@ -243,6 +254,7 @@ func New(ctx context.Context, rawURL string, options Options) (result Result, er return Result{ Info: info, + RawURL: rawURL, RawJSON: rawJSONCopy, Options: options, }, nil @@ -421,6 +433,7 @@ func infoFromURL(ctx context.Context, rawURL string, options Options) (info Info // Result metadata for a URL type Result struct { Info Info + RawURL string RawJSON []byte // saved raw JSON. Used later when downloading Options Options // options passed to New } @@ -452,18 +465,24 @@ type DownloadOptions struct { func (result Result) DownloadWithOptions(ctx context.Context, options DownloadOptions) (*DownloadResult, error) { debugLog := result.Options.DebugLog - if (result.Info.Type == "playlist" || result.Info.Type == "multi_video") && options.PlaylistIndex == 0 { - return nil, fmt.Errorf("can't download a playlist when the playlist index options is not set") + if !result.Options.NoInfoDownload { + if (result.Info.Type == "playlist" || result.Info.Type == "multi_video") && options.PlaylistIndex == 0 { + return nil, fmt.Errorf("can't download a playlist when the playlist index options is not set") + } } tempPath, tempErr := ioutil.TempDir("", "ydls") if tempErr != nil { return nil, tempErr } - jsonTempPath := path.Join(tempPath, "info.json") - if err := ioutil.WriteFile(jsonTempPath, result.RawJSON, 0600); err != nil { - os.RemoveAll(tempPath) - return nil, err + + var jsonTempPath string + if !result.Options.NoInfoDownload { + jsonTempPath = path.Join(tempPath, "info.json") + if err := ioutil.WriteFile(jsonTempPath, result.RawJSON, 0600); err != nil { + os.RemoveAll(tempPath) + return nil, err + } } dr := &DownloadResult{ @@ -478,9 +497,36 @@ func (result Result) DownloadWithOptions(ctx context.Context, options DownloadOp "--ignore-errors", "--newline", "--restrict-filenames", - "--load-info", jsonTempPath, "-o", "-", ) + + if result.Options.NoInfoDownload { + // provide URL via stdin for security, youtube-dl has some run command args + cmd.Args = append(cmd.Args, "--batch-file", "-") + cmd.Stdin = bytes.NewBufferString(result.RawURL + "\n") + + if result.Options.Type == TypePlaylist { + cmd.Args = append(cmd.Args, "--yes-playlist") + + if result.Options.PlaylistStart > 0 { + cmd.Args = append(cmd.Args, + "--playlist-start", strconv.Itoa(int(result.Options.PlaylistStart)), + ) + } + if result.Options.PlaylistEnd > 0 { + cmd.Args = append(cmd.Args, + "--playlist-end", strconv.Itoa(int(result.Options.PlaylistEnd)), + ) + } + } else { + cmd.Args = append(cmd.Args, + "--no-playlist", + ) + } + } else { + cmd.Args = append(cmd.Args, "--load-info", jsonTempPath) + } + // don't need to specify if direct as there is only one // also seems to be issues when using filter with generic extractor if !result.Info.Direct && options.Filter != "" { From a230313e476c6209eda763d10c0e9bd9547b205e Mon Sep 17 00:00:00 2001 From: Nonoo Date: Thu, 17 Aug 2023 11:11:27 +0200 Subject: [PATCH 2/3] Add main Download() function and its test // It downloads given URL using the given options and filter (usually a format id or quality designator). If filter is empty, then youtube-dl will use its default format selector. --- goutubedl.go | 11 +++++++++++ goutubedl_test.go | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 43 insertions(+) diff --git a/goutubedl.go b/goutubedl.go index 7b24a68..5e27457 100644 --- a/goutubedl.go +++ b/goutubedl.go @@ -231,6 +231,17 @@ func Version(ctx context.Context) (string, error) { return strings.TrimSpace(string(versionBytes)), nil } +// Downloads given URL using the given options and filter (usually a format id or quality designator). +// If filter is empty, then youtube-dl will use its default format selector. +func Download(ctx context.Context, rawURL string, options Options, filter string) (*DownloadResult, error) { + options.NoInfoDownload = true + d, err := New(ctx, rawURL, options) + if err != nil { + return nil, err + } + return d.Download(ctx, filter) +} + // New downloads metadata for URL func New(ctx context.Context, rawURL string, options Options) (result Result, err error) { if options.DebugLog == nil { diff --git a/goutubedl_test.go b/goutubedl_test.go index 54adab9..b5dbd95 100644 --- a/goutubedl_test.go +++ b/goutubedl_test.go @@ -99,6 +99,38 @@ func TestDownload(t *testing.T) { } } +func TestDownloadWithoutInfo(t *testing.T) { + defer leakChecks(t)() + + stderrBuf := &bytes.Buffer{} + dr, err := goutubedl.Download(context.Background(), testVideoRawURL, goutubedl.Options{ + StderrFn: func(cmd *exec.Cmd) io.Writer { + return stderrBuf + }, + }, "") + if err != nil { + t.Fatal(err) + } + downloadBuf := &bytes.Buffer{} + n, err := io.Copy(downloadBuf, dr) + if err != nil { + t.Fatal(err) + } + dr.Close() + + if n != int64(downloadBuf.Len()) { + t.Errorf("copy n not equal to download buffer: %d!=%d", n, downloadBuf.Len()) + } + + if n < 10000 { + t.Errorf("should have copied at least 10000 bytes: %d", n) + } + + if !strings.Contains(stderrBuf.String(), "Destination") { + t.Errorf("did not find expected log message on stderr: %q", stderrBuf.String()) + } +} + func TestParseInfo(t *testing.T) { for _, c := range []struct { url string From b6c1793051ceac677b18b428f93bce96763558fd Mon Sep 17 00:00:00 2001 From: Nonoo Date: Thu, 17 Aug 2023 11:47:20 +0200 Subject: [PATCH 3/3] Unexport the Options.noInfoDownload field There is a package Download() function now which handle downloading without using the Info struct so this field no longer needs to be exported. --- goutubedl.go | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/goutubedl.go b/goutubedl.go index 5e27457..1631ba1 100644 --- a/goutubedl.go +++ b/goutubedl.go @@ -215,8 +215,8 @@ type Options struct { SortingFormat string // --format-sort // Set to true if you don't want to use the result.Info structure after the goutubedl.New() call, - // so the given URL will be downloaded in a single pass in the Download() call. - NoInfoDownload bool + // so the given URL will be downloaded in a single pass in the DownloadResult.Download() call. + noInfoDownload bool } // Version of youtube-dl. @@ -234,7 +234,7 @@ func Version(ctx context.Context) (string, error) { // Downloads given URL using the given options and filter (usually a format id or quality designator). // If filter is empty, then youtube-dl will use its default format selector. func Download(ctx context.Context, rawURL string, options Options, filter string) (*DownloadResult, error) { - options.NoInfoDownload = true + options.noInfoDownload = true d, err := New(ctx, rawURL, options) if err != nil { return nil, err @@ -248,7 +248,7 @@ func New(ctx context.Context, rawURL string, options Options) (result Result, er options.DebugLog = nopPrinter{} } - if options.NoInfoDownload { + if options.noInfoDownload { return Result{ RawURL: rawURL, Options: options, @@ -476,7 +476,7 @@ type DownloadOptions struct { func (result Result) DownloadWithOptions(ctx context.Context, options DownloadOptions) (*DownloadResult, error) { debugLog := result.Options.DebugLog - if !result.Options.NoInfoDownload { + if !result.Options.noInfoDownload { if (result.Info.Type == "playlist" || result.Info.Type == "multi_video") && options.PlaylistIndex == 0 { return nil, fmt.Errorf("can't download a playlist when the playlist index options is not set") } @@ -488,7 +488,7 @@ func (result Result) DownloadWithOptions(ctx context.Context, options DownloadOp } var jsonTempPath string - if !result.Options.NoInfoDownload { + if !result.Options.noInfoDownload { jsonTempPath = path.Join(tempPath, "info.json") if err := ioutil.WriteFile(jsonTempPath, result.RawJSON, 0600); err != nil { os.RemoveAll(tempPath) @@ -511,7 +511,7 @@ func (result Result) DownloadWithOptions(ctx context.Context, options DownloadOp "-o", "-", ) - if result.Options.NoInfoDownload { + if result.Options.noInfoDownload { // provide URL via stdin for security, youtube-dl has some run command args cmd.Args = append(cmd.Args, "--batch-file", "-") cmd.Stdin = bytes.NewBufferString(result.RawURL + "\n")