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:
60
goutubedl.go
60
goutubedl.go
@ -213,6 +213,10 @@ type Options struct {
|
|||||||
HTTPClient *http.Client // Client for download thumbnail and subtitles (nil use http.DefaultClient)
|
HTTPClient *http.Client // Client for download thumbnail and subtitles (nil use http.DefaultClient)
|
||||||
MergeOutputFormat string // --merge-output-format
|
MergeOutputFormat string // --merge-output-format
|
||||||
SortingFormat string // --format-sort
|
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.
|
// Version of youtube-dl.
|
||||||
@ -233,6 +237,13 @@ func New(ctx context.Context, rawURL string, options Options) (result Result, er
|
|||||||
options.DebugLog = nopPrinter{}
|
options.DebugLog = nopPrinter{}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if options.NoInfoDownload {
|
||||||
|
return Result{
|
||||||
|
RawURL: rawURL,
|
||||||
|
Options: options,
|
||||||
|
}, nil
|
||||||
|
}
|
||||||
|
|
||||||
info, rawJSON, err := infoFromURL(ctx, rawURL, options)
|
info, rawJSON, err := infoFromURL(ctx, rawURL, options)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return Result{}, err
|
return Result{}, err
|
||||||
@ -243,6 +254,7 @@ func New(ctx context.Context, rawURL string, options Options) (result Result, er
|
|||||||
|
|
||||||
return Result{
|
return Result{
|
||||||
Info: info,
|
Info: info,
|
||||||
|
RawURL: rawURL,
|
||||||
RawJSON: rawJSONCopy,
|
RawJSON: rawJSONCopy,
|
||||||
Options: options,
|
Options: options,
|
||||||
}, nil
|
}, nil
|
||||||
@ -421,6 +433,7 @@ func infoFromURL(ctx context.Context, rawURL string, options Options) (info Info
|
|||||||
// Result metadata for a URL
|
// Result metadata for a URL
|
||||||
type Result struct {
|
type Result struct {
|
||||||
Info Info
|
Info Info
|
||||||
|
RawURL string
|
||||||
RawJSON []byte // saved raw JSON. Used later when downloading
|
RawJSON []byte // saved raw JSON. Used later when downloading
|
||||||
Options Options // options passed to New
|
Options Options // options passed to New
|
||||||
}
|
}
|
||||||
@ -452,18 +465,24 @@ type DownloadOptions struct {
|
|||||||
func (result Result) DownloadWithOptions(ctx context.Context, options DownloadOptions) (*DownloadResult, error) {
|
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") && options.PlaylistIndex == 0 {
|
if !result.Options.NoInfoDownload {
|
||||||
return nil, fmt.Errorf("can't download a playlist when the playlist index options is not set")
|
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")
|
tempPath, tempErr := ioutil.TempDir("", "ydls")
|
||||||
if tempErr != nil {
|
if tempErr != nil {
|
||||||
return nil, tempErr
|
return nil, tempErr
|
||||||
}
|
}
|
||||||
jsonTempPath := path.Join(tempPath, "info.json")
|
|
||||||
if err := ioutil.WriteFile(jsonTempPath, result.RawJSON, 0600); err != nil {
|
var jsonTempPath string
|
||||||
os.RemoveAll(tempPath)
|
if !result.Options.NoInfoDownload {
|
||||||
return nil, err
|
jsonTempPath = path.Join(tempPath, "info.json")
|
||||||
|
if err := ioutil.WriteFile(jsonTempPath, result.RawJSON, 0600); err != nil {
|
||||||
|
os.RemoveAll(tempPath)
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
dr := &DownloadResult{
|
dr := &DownloadResult{
|
||||||
@ -478,9 +497,36 @@ func (result Result) DownloadWithOptions(ctx context.Context, options DownloadOp
|
|||||||
"--ignore-errors",
|
"--ignore-errors",
|
||||||
"--newline",
|
"--newline",
|
||||||
"--restrict-filenames",
|
"--restrict-filenames",
|
||||||
"--load-info", jsonTempPath,
|
|
||||||
"-o", "-",
|
"-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
|
// 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 && options.Filter != "" {
|
if !result.Info.Direct && options.Filter != "" {
|
||||||
|
Reference in New Issue
Block a user