Skip to content
Advertisement

Using Pytube to download videos of a playlist of specific length

I am trying to download the videos of a YouTube playlist using the PyTube – library. Since the playlists I need have a few thousand videos I want to add the condition that just videos of the length 10s to 1 hour should be downloaded. So far I can download all videos of a playlist with the following code:

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()

Can someone help?

Advertisement

Answer

You can get the length of a video from 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()
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement