在将它用作python模块时,如何在youtube-dl中列出视频分辨率?

Sau*_*hoo 5 python youtube youtube-dl

好吧,我可以在终端中直接使用它来获取视频格式-

$ youtube-dl -F "some youtube url"
Run Code Online (Sandbox Code Playgroud)

输出:

[youtube] Setting language
[youtube] P9pzm5b6FFY: Downloading webpage
[youtube] P9pzm5b6FFY: Downloading video info webpage
[youtube] P9pzm5b6FFY: Extracting video information
[info] Available formats for P9pzm5b6FFY:
format code extension resolution  note 
140         m4a       audio only  DASH audio , audio@128k (worst)
160         mp4       144p        DASH video , video only
133         mp4       240p        DASH video , video only
134         mp4       360p        DASH video , video only
135         mp4       480p        DASH video , video only
136         mp4       720p        DASH video , video only
17          3gp       176x144     
36          3gp       320x240     
5           flv       400x240     
43          webm      640x360     
18          mp4       640x360     
22          mp4       1280x720    (best)
Run Code Online (Sandbox Code Playgroud)

但我想在将youtube-dl用作python中的模块时使用相同的选项。

现在,我不得不猜测并指定下载选项为:

import youtube_dl

options = {
'format': 'bestaudio/best', # choice of quality
'extractaudio' : True,      # only keep the audio
'audioformat' : "mp3",      # convert to mp3 
'outtmpl': '%(id)s',        # name the file the ID of the video
'noplaylist' : True,        # only download single song, not playlist
}

with youtube_dl.YoutubeDL(options) as ydl:
    ydl.download(url)
Run Code Online (Sandbox Code Playgroud)

我不知道哪种格式可用。但是如果我可以列出可用的格式,那么我可以相应地设置这些选项。

有什么办法可以在python内部使用“ -F”开关吗?

Le *_*hau 6

查看youtube-dl的源代码,我看到他们如何列出视频格式

 def list_formats(self, info_dict):
    formats = info_dict.get('formats', [info_dict])
    table = [
        [f['format_id'], f['ext'], self.format_resolution(f), self._format_note(f)]
        for f in formats
        if f.get('preference') is None or f['preference'] >= -1000]
    if len(formats) > 1:
        table[-1][-1] += (' ' if table[-1][-1] else '') + '(best)'

    header_line = ['format code', 'extension', 'resolution', 'note']
    self.to_screen(
        '[info] Available formats for %s:\n%s' %
        (info_dict['id'], render_table(header_line, table)))
Run Code Online (Sandbox Code Playgroud)

那么通过这种方式列出可用格式是否非常简单

ydl_opts = {}
with youtube_dl.YoutubeDL(ydl_opts) as ydl:
    meta = ydl.extract_info(
        'https://www.youtube.com/watch?v=9bZkp7q19f0', download=False) 
    formats = meta.get('formats', [meta])
for f in formats:
    print(f['ext'])
Run Code Online (Sandbox Code Playgroud)


小智 5

如果使用该listformats选项将表格打印到标准输出,它将不会下载视频。例如:

import youtube_dl

options = {
    'format': 'bestaudio/best',  # choice of quality
    'extractaudio': True,        # only keep the audio
    'audioformat': "mp3",        # convert to mp3
    'outtmpl': '%(id)s',         # name the file the ID of the video
    'noplaylist': True,          # only download single song, not playlist
    'listformats': True,         # print a list of the formats to stdout and exit
}

with youtube_dl.YoutubeDL(options) as ydl:
    ydl.download(['http://www.youtube.com/watch?v=BaW_jenozKc'])
Run Code Online (Sandbox Code Playgroud)