如何使pytube打印所有可用的分辨率,并由用户输入一个

2024-04-18 22:27:13 发布

您现在位置:Python中文网/ 问答频道 /正文

简介:所以我正在尝试制作一个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()

Tags: oftheimporturlforinputstreamyoutube
1条回答
网友
1楼 · 发布于 2024-04-18 22:27:13

流对象有一个按分辨率变化的属性。比如说

你有:

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

再次通知,解决方案列表可能包含解决方案的副本。也许你可以调整一下

相关问题 更多 >