好吧,我可以在终端中直接使用它来获取视频格式-
$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)
我不知道哪种格式可用.
但是如果我可以列出可用的格式,那么我可以相应地设置这些选项.
有什么办法可以在python内部使用“ -F”开关吗?
解决方法:
如果您使用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'])