【问题标题】:Using Pytube to download videos of a playlist of specific length使用 Pytube 下载特定长度的播放列表的视频
【发布时间】:2021-09-04 22:30:36
【问题描述】:

我正在尝试使用 PyTube - 库下载 YouTube 播放列表的视频。由于我需要的播放列表有几千个视频,我想添加一个条件,即只下载长度为 10 到 1 小时的视频。 到目前为止,我可以使用以下代码下载播放列表的所有视频:

from pytube import Playlist

url = 
# url is the url of the YouTube playlist
play_list = Playlist('url')
for video in play_list.videos:
    video.streams.first().download()

有人可以帮忙吗?

【问题讨论】:

    标签: python pytube


    【解决方案1】:

    您可以从pytube.YouTube.length获取视频长度

    https://pytube.io/en/latest/api.html#pytube.YouTube.length

    from pytube import Playlist
    
    url = 'https://www.youtube.com/playlist?list=PLoROMvodv4rOhcuXMZkNm7j3fVwBBY42z'
    play_list = Playlist(url)
    for video in play_list.videos:
        if video.length>60*60:  # video.length is in seconds
            continue
        video.streams.first().download()
    

    【讨论】:

    • if video.length>60*60: 行确保只下载时长超过 1 小时的视频?
    • 不,如果条件为真,则继续循环/迭代中的下一个元素——即,跳过循环中的其余代码。如果您可以接受此解决方案,请不要忘记接受答案。
    • 谢谢,继续命令对我来说是新的 :)
    猜你喜欢
    • 1970-01-01
    • 2020-12-15
    • 2018-09-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-05-05
    相关资源
    最近更新 更多