add options to handle downloading one entry from a playlist
This commit is contained in:
42
goutubedl.go
42
goutubedl.go
@ -270,8 +270,8 @@ func infoFromURL(ctx context.Context, rawURL string, options Options) (info Info
|
|||||||
if options.Downloader != "" {
|
if options.Downloader != "" {
|
||||||
cmd.Args = append(cmd.Args, "--downloader", options.Downloader)
|
cmd.Args = append(cmd.Args, "--downloader", options.Downloader)
|
||||||
}
|
}
|
||||||
|
switch options.Type {
|
||||||
if options.Type == TypePlaylist {
|
case TypePlaylist:
|
||||||
cmd.Args = append(cmd.Args, "--yes-playlist")
|
cmd.Args = append(cmd.Args, "--yes-playlist")
|
||||||
|
|
||||||
if options.PlaylistStart > 0 {
|
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)),
|
"--playlist-end", strconv.Itoa(int(options.PlaylistEnd)),
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
} else {
|
case TypeSingle:
|
||||||
if options.DownloadSubtitles {
|
if options.DownloadSubtitles {
|
||||||
cmd.Args = append(cmd.Args,
|
cmd.Args = append(cmd.Args,
|
||||||
"--all-subs",
|
"--all-subs",
|
||||||
@ -293,6 +293,10 @@ func infoFromURL(ctx context.Context, rawURL string, options Options) (info Info
|
|||||||
cmd.Args = append(cmd.Args,
|
cmd.Args = append(cmd.Args,
|
||||||
"--no-playlist",
|
"--no-playlist",
|
||||||
)
|
)
|
||||||
|
case TypeAny:
|
||||||
|
break
|
||||||
|
default:
|
||||||
|
return Info{}, nil, fmt.Errorf("Unhandle options type value: %d", options.Type)
|
||||||
}
|
}
|
||||||
|
|
||||||
tempPath, _ := ioutil.TempDir("", "ydls")
|
tempPath, _ := ioutil.TempDir("", "ydls")
|
||||||
@ -427,13 +431,29 @@ type DownloadResult struct {
|
|||||||
waitCh chan struct{}
|
waitCh chan struct{}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Download format matched by filter (usually a format id or quality designator).
|
// Download is a shortcut of DownloadOptions where the options use the default value
|
||||||
// If filter is empty, then youtube-dl will use its default format selector.
|
|
||||||
func (result Result) Download(ctx context.Context, filter string) (*DownloadResult, error) {
|
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
|
debugLog := result.Options.DebugLog
|
||||||
|
|
||||||
if result.Info.Type == "playlist" || result.Info.Type == "multi_video" {
|
if (result.Info.Type == "playlist" || result.Info.Type == "multi_video") && options.PlaylistIndex < 0 {
|
||||||
return nil, fmt.Errorf("can't download a playlist")
|
return nil, fmt.Errorf("can't download a playlist when the playlist index options is not set")
|
||||||
}
|
}
|
||||||
|
|
||||||
tempPath, tempErr := ioutil.TempDir("", "ydls")
|
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
|
// don't need to specify if direct as there is only one
|
||||||
// also seems to be issues when using filter with generic extractor
|
// also seems to be issues when using filter with generic extractor
|
||||||
if !result.Info.Direct && filter != "" {
|
if !result.Info.Direct && options.Filter != "" {
|
||||||
cmd.Args = append(cmd.Args, "-f", 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 != "" {
|
if result.Options.ProxyUrl != "" {
|
||||||
|
Reference in New Issue
Block a user