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

时间:2016-02-16 08:43:13

标签: python youtube youtube-dl

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

$ youtube-dl -F "some youtube url"

输出:

[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)

但我希望在使用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)

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

有没有办法使用它" -F"在python里面切换?

3 个答案:

答案 0 :(得分:2)

如果您使用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'])

答案 1 :(得分:2)

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

import turtle

windowcolor = input("window color: ")
tesscolor = input("tess color: ")

# Set up window

window = turtle.Screen()
window.bgcolor(windowcolor)
window.title("Hello, Tess!")

# Set up turtle

tess = turtle.Turtle()
tess.color(tesscolor)
tess.pensize(3)

# Draw things

tess.forward(50)
tess.left(120)
tess.forward(50)

# Enter window loop

window.mainloop()

以这种方式列出可用格式是否很简单

 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)))

答案 2 :(得分:0)

您可以先尝试提取信息,然后重复使用代码:

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
}

with youtube_dl.YoutubeDL(options) as ydl:
    result = ydl.extract_info(url, download = False)
if 'entries' in result: # in case it is a playlist
    video = result['entries'][0]
else: video = result

for k, v in video.iteritems(): # print out the info
    print k, "|", v