我最近尝试使用 pytube 库下载 YouTube 视频,如下所示:
from pytube import YouTube
def download_video(url, resolution):
try:
yt = YouTube(url)
streams = yt.streams.filter(res=f'{resolution}p', progressive=True).first()
if streams:
print(f"Downloading {yt.title} in {resolution}p resolution...")
streams.download()
print("Download completed successfully!")
else:
print(f"No {resolution}p resolution available for this video.")
except Exception as e:
print("An error occurred:", str(e))
if __name__ == "__main__":
url = input("Enter the YouTube video link: ")
resolution = input("Enter the desired resolution (e.g., 144, 240, 360, 480, 720, 1080): ")
download_video(url, resolution)
但是当我指定分辨率时,它给我带来了这个错误:
An error occurred: __init__: could not find match for ^\w+\W
我在互联网上搜索任何解决方案并找到这个:
C:\Users#####\AppData\Local\Programs\Python\Python311\Lib\site-packages\pytub