如何在将 youtube-dl 用作 python 模块时列出视频分辨率?
How can I list video resolutions in youtube-dl while using it as a python module?
好吧,我可以在终端中使用它直接获取视频格式 -
$ 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)
但我想在 python 中使用 youtube-dl 作为模块时使用相同的选项。
现在我必须猜测并指定下载选项为:
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)
我不知道哪种格式可用。
但是如果我能列出可用的格式,那么我就可以相应地设置这些选项。
有什么方法可以在 python 中使用那个“-F”开关吗?
如果您使用 listformats
选项将 table 打印到标准输出,则不会下载视频。例如:
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'])
您可以尝试先提取信息,重新使用您的代码:
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
看看 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)))
那么通过这种方式获取可用格式列表是不是很简单
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'])
好吧,我可以在终端中使用它直接获取视频格式 -
$ 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)
但我想在 python 中使用 youtube-dl 作为模块时使用相同的选项。
现在我必须猜测并指定下载选项为:
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)
我不知道哪种格式可用。 但是如果我能列出可用的格式,那么我就可以相应地设置这些选项。
有什么方法可以在 python 中使用那个“-F”开关吗?
如果您使用 listformats
选项将 table 打印到标准输出,则不会下载视频。例如:
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'])
您可以尝试先提取信息,重新使用您的代码:
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
看看 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)))
那么通过这种方式获取可用格式列表是不是很简单
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'])