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