python - 如何让 pytube 打印所有可用的分辨率并且用户输入一个
问题描述
简介:所以我正在尝试制作一个 pytube 项目,但我被困在这一步,
问题:我不知道如何让 pytube 列出所有可用的分辨率
from pytube import YouTube
# import the package
print("Please Paste The URL of the youtube video")
url = input()
# URL (user input)
my_video = YouTube(url)
print(my_video.title)
# Title of The Video
#Now for the Thumbnail Image
print("Thumbnail URL")
print(my_video.thumbnail_url)
#To Download the video with the users Choice of resolution
print("Choose A Resolution Please")
for stream in my_video.stream:
print(stream)
#command for downloading the video
my_video.download()
解决方案
流对象有一个按分辨率进行的属性。例如
你有:
for stream in my_video.stream:
print(stream)
但是既然要显示每个流对象的分辨率,可以试试:
for stream in my_video.stream:
print(stream.resolution)
我花时间写了一个脚本来测试我的想法。
from pytube import YouTube
def download(video_resolutions, videos):
while True:
# Looping through the video_resolutions list to be displayed on the screen for user selection...
i = 1
for resolution in video_resolutions:
print(f'{i}. {resolution}')
i += 1
# To Download the video with the users Choice of resolution
choice = int(input('\nChoose A Resolution Please: '))
# To validate if the user enters a number displayed on the screen...
if 1 <= choice < i:
resolution_to_download = video_resolutions[choice - 1]
print(f"You're now downloading the video with resolution {resolution_to_download}...")
# command for downloading the video
videos[choice - 1].download()
print("\nVideo was successfully downloaded!")
break
else:
print("Invalid choice!!\n\n")
def sort_resolutions(url):
# URL (user input)
my_video = YouTube(url)
print(my_video.title)
# Title of The Video
# Now for the Thumbnail Image
print("Thumbnail URL")
print(my_video.thumbnail_url)
video_resolutions = []
videos = []
for stream in my_video.streams.order_by('resolution'):
# print(stream)
video_resolutions.append(stream.resolution)
videos.append(stream)
# print(video_resolutions)
return video_resolutions, videos
print("Please Paste The URL of the youtube video")
url = "https://youtu.be/o9aaoiyJlcM"
video_resolutions, videos = sort_resolutions(url)
download(video_resolutions, videos)
这url = "https://youtu.be/o9aaoiyJlcM"
只是一条静态行,我不必重新输入 url,如果您愿意,可以将其更改回输入。在将链接分配给url
变量之后,我将其传递url
给一个名为的函数,该函数将sort_resolutions(url)
在其中使用链接并提取我们需要的所有内容。我使用了这个函数,因为它只是让代码更有条理。
在sort_resolution
函数通知中,我创建了两个列表对象...video_resolutions
和videos
, video_resolutions = []
, videos = []
,我用流对象填充了这些对象。
for stream in my_video.streams.order_by('resolution'):
video_resolutions.append(stream.resolution) # Populating the resolution list
videos.append(stream) # Populating the video list
my_video.streams.order_by('resolution')
这只是按分辨率对流对象进行排序。
return video_resolutions, videos
只是返回已填充的列表,video_resolutions, videos = sort_resolutions(url)
.
返回的值现在将传递给download
函数download(video_resolutions, videos)
。请注意,在此功能中while loop
,将在屏幕上显示所有可下载的可用分辨率的菜单。如果用户选择了一个有效数字,该choice
变量会收集该值,然后我们将使用choice - 1
来查找所需分辨率的索引resolution_to_download = video_resolutions[choice - 1]
,但这只会找到分辨率。要下载与视频列表中相同索引号匹配的视频,您必须videos[choice - 1].download()
. 换句话说,videos[choice - 1]
是一个流对象,所以通过调用下载方法videos[choice - 1].download()
仍然有效。
另请注意,分辨率列表可能包含重复的分辨率。所以也许你可以微调它。
推荐阅读
- linux - 午夜指挥官中的 Chmod 递归
- python - 在 SQL Server 中存储 numpy 数组
- floating-point - python3中的除法
- error-handling - 接受结果是惯用的 Rust
作为函数参数? - c++ - InsertRecord 返回 false 但似乎没有发生错误
- c++ - 问:Apache Arrow 在 C++ 中将 PARQUET 转换为 JSON?
- android - 在反应原生应用程序的发布版本日志中获取“D8:”文本
- debian - AdoptOpenJDK apt-get 更新失败并出现错误“未知日期格式错误标头数据”
- python - 提交按钮在 Django 表单中不起作用
- r - 多变点分析:惩罚和意义