如何让 pytube 打印所有可用的分辨率和用户输入一个

How to make pytube print all available resolutions and the user inputs one

简介: 所以我正在尝试制作一个 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,如果您愿意,可以将其改回输入。在将 link 分配给 url 变量后,我将 url 传递给一个名为 sort_resolutions(url) 的函数,其中将使用 link 并提取我们将要使用的所有内容需要。我使用了这个函数,因为它只是让代码更有条理。

sort_resolution 函数通知中,我创建了两个列表对象...video_resolutionsvideosvideo_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() 仍然有效。

再次提醒,决议列表中可能存在重复的决议。所以也许你可以微调一下。