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:
JavaScript
x
8
1
from pytube import Playlist
2
3
url =
4
# url is the url of the YouTube playlist
5
play_list = Playlist('url')
6
for video in play_list.videos:
7
video.streams.first().download()
8
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
JavaScript
1
9
1
from pytube import Playlist
2
3
url = 'https://www.youtube.com/playlist?list=PLoROMvodv4rOhcuXMZkNm7j3fVwBBY42z'
4
play_list = Playlist(url)
5
for video in play_list.videos:
6
if video.length>60*60: # video.length is in seconds
7
continue
8
video.streams.first().download()
9