首页 > 解决方案 > 如何让 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()

标签: pythonpytube

解决方案


流对象有一个按分辨率进行的属性。例如

你有:

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_resolutionsvideos​​, 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()仍然有效。

另请注意,分辨率列表可能包含重复的分辨率。所以也许你可以微调它。


推荐阅读