我正在编写一个从 youtube 下载视频和音频的脚本,目前,我的视频下载脚本有问题,就是:
def VideoDownload_mp4(url, res):
try:
yt = pytube.YouTube(url)
folder = "YouTube Videos"
video = yt.streams.get_by_resolution(resolution=res)
video.download(folder)
print("downloaded.")
except Exception as e:
print(f"Error: {e}")
示例条目:
url =“https://youtu.be/YEqWLCBrgic?si=Z245EINTenbGqasr”
分辨率=“720p”
我有另一个脚本来获取视频的分辨率:
def GetVideoQuality(url):
yt = pytube.YouTube(url)
base_list = []
for stream in yt.streams:
resolution = stream.resolution
bitrate = stream.bitrate
base_list.append({'resolution':resolution, 'bitrate':bitrate})
new_dict = {}
fixed_list = []
for diccionario in base_list:
resolution = diccionario['resolution']
bitrate = diccionario['bitrate']
if resolution is not None:
if resolution not in new_dict:
new_dict[resolution] = bitrate
fixed_list.append(diccionario.copy())
else:
if bitrate > new_dict[resolution]:
new_dict[resolution] = bitrate
fixed_list.remove([d for d in fixed_list if d['resolution'] == resolution][0])
fixed_list.append(diccionario.copy())
return fixed_list
所以,假设视频有 ['1080p'、'720p'、'480p'],我从主脚本中选择其中之一并将其发送到 VideoDownload_mp4()。 问题是其中一些分辨率不允许我下载它,从而出现错误:'NoneType'对象没有属性'download'。 这很奇怪,例如,我可以毫无问题地下载 1080p,但不要让我下载 480p。 我在下载脚本中遗漏了一些东西?
抱歉,如果我拼写错误,我的英语不好。
我希望能够下载任何分辨率的 X 视频,但其中一些分辨率不允许我下载。
意图:
def VideoDownload_mp4(url:str, res:str, folder:str="YouTube Videos"):
try:
video = YouTube(url)
video.streams.filter(res=res).first().download(folder)
print(f"Downloaded {video.title}.mp4")
except Exception as e:
print(f"Error: {e}")
完成全部代码:
from pytube import YouTube
def VideoDownload_mp4(url:str, res:str, folder:str="YouTube Videos"):
try:
video = YouTube(url)
video.streams.filter(res=res).first().download(folder)
print(f"Downloaded {video.title}.mp4")
except Exception as e:
print(f"Error: {e}")
VideoDownload_mp4("https://youtu.be/YEqWLCBrgic?si=Z245EINTenbGqasr", "480p")
Debería funcionarte bien, suerte con eso.