From ad9066c4f426944cc0fa84a0c63819570d6f0459 Mon Sep 17 00:00:00 2001 From: Galiley Date: Tue, 15 Aug 2023 20:28:52 +0200 Subject: [PATCH] add options to handle downloading one entry from a playlist --- goutubedl.go | 42 +++++++++++++++++++++++++++++++++--------- 1 file changed, 33 insertions(+), 9 deletions(-) diff --git a/goutubedl.go b/goutubedl.go index 213b71f..7ee38f7 100644 --- a/goutubedl.go +++ b/goutubedl.go @@ -270,8 +270,8 @@ func infoFromURL(ctx context.Context, rawURL string, options Options) (info Info if options.Downloader != "" { cmd.Args = append(cmd.Args, "--downloader", options.Downloader) } - - if options.Type == TypePlaylist { + switch options.Type { + case TypePlaylist: cmd.Args = append(cmd.Args, "--yes-playlist") if options.PlaylistStart > 0 { @@ -284,7 +284,7 @@ func infoFromURL(ctx context.Context, rawURL string, options Options) (info Info "--playlist-end", strconv.Itoa(int(options.PlaylistEnd)), ) } - } else { + case TypeSingle: if options.DownloadSubtitles { cmd.Args = append(cmd.Args, "--all-subs", @@ -293,6 +293,10 @@ func infoFromURL(ctx context.Context, rawURL string, options Options) (info Info cmd.Args = append(cmd.Args, "--no-playlist", ) + case TypeAny: + break + default: + return Info{}, nil, fmt.Errorf("Unhandle options type value: %d", options.Type) } tempPath, _ := ioutil.TempDir("", "ydls") @@ -427,13 +431,29 @@ type DownloadResult struct { waitCh chan struct{} } -// Download format matched by filter (usually a format id or quality designator). -// If filter is empty, then youtube-dl will use its default format selector. +// Download is a shortcut of DownloadOptions where the options use the default value func (result Result) Download(ctx context.Context, filter string) (*DownloadResult, error) { + return result.DownloadWithOptions(ctx, DownloadOptions{ + Filter: filter, + PlaylistIndex: -1, + }) +} + +type DownloadOptions struct { + // Download format matched by filter (usually a format id or quality designator). + // If filter is empty, then youtube-dl will use its default format selector. + Filter string + // The index of the entry to download from the playlist that would be + // passed to youtube-dl wia --playlist-items. + // The index value starts at 1 + PlaylistIndex int +} + +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" { - return nil, fmt.Errorf("can't download a playlist") + 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") @@ -463,8 +483,12 @@ func (result Result) Download(ctx context.Context, filter string) (*DownloadResu ) // 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 && filter != "" { - cmd.Args = append(cmd.Args, "-f", filter) + if !result.Info.Direct && options.Filter != "" { + cmd.Args = append(cmd.Args, "-f", options.Filter) + } + + if options.PlaylistIndex >= 0 { + cmd.Args = append(cmd.Args, "--playlist-items", fmt.Sprint(options.PlaylistIndex)) } if result.Options.ProxyUrl != "" {