youtube-dl:如何使用选项并将输出获取到变量

youtube-dl: how to use the options and get the output to a variable

我一直在尝试使用 youtube-dl 使用 python 而不是控制台。我正在尝试获取视频的一些信息,例如可用的视频格式、标题、视频持续时间等。 None 选项有效,但 listformats 除外。所以我尝试了文档中描述的选项的不同组合。不幸的是,没有任何效果。我只看到来自 listformats 的视频格式列表。此外,输出仅显示在控制台中,但我需要变量中的输出信息,例如 video_details.

from __future__ import unicode_literals
import youtube_dl

ydl_opts = {
    '-v': True,
    'format': '-f',
    'print_json': True,
    'listformats': '--list-formats',
    'getfilename': '--get-filename',
    '--get-filename': True,
    '-e': True,
}

ydl = youtube_dl.YoutubeDL(ydl_opts)
video_details = ydl.download(['https://www.youtube.com/watch?v=BaW_jenozKc'])

您想提取信息。 使用 extract_info 方法而不是 download,它将起作用!