Skip to content
Advertisement

Using Pytube to download playlist from YouTube

I am looking to download a YouTube playlist using the PyTube library. Currently, I am able to download a single video at a time. I cannot download more than one video at once.

Currently, my implimentation is

import pytube

link = input('Please enter a url linkn')
yt = pytube.YouTube(link)
stream = yt.streams.first()
finished = stream.download()
print('Download is complete')

This results in the following output

>> Download is complete

And the YouTube file is downloaded. When I try this with a playlist link (An example) only the first video is downloaded. There is no error outputted.

I would like to be able to download an entire playlist without re-prompting the user.

Advertisement

Answer

You can import Playlist to achieve this. There is no reference to Playlist in the redoc, though there is a section in the GitHub repo found here. The source of the script is in the repo here.

from pytube import Playlist

playlist = Playlist('https://www.youtube.com/watch?v=58PpYacL-VQ&list=UUd6MoB9NC6uYN2grvUNT-Zg')
print('Number of videos in playlist: %s' % len(playlist.video_urls))
playlist.download_all()

NOTE: I’ve found the supporting method Playlist.video_urls does not work. The videos are still downloaded however, as evidenced here

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement