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.
This commit is contained in:
Nonoo
2023-08-17 11:18:47 +02:00
parent a46b2837fc
commit ddaf5ad2fa

View File

@ -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 != "" {