5

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 link\n')
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.

BlackLotus
  • 545
  • 3
  • 19
Anish Mazumdar
  • 115
  • 1
  • 1
  • 7

5 Answers5

12

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

BlackLotus
  • 545
  • 3
  • 19
  • 2
    The `playlist.video_urls` list is populated by the `populate_video_urls()` method, which is called by `playlist.download_all()`. Calling `populate_video_urls()` does seem to fill the list as expected. – Eli Zupke May 08 '19 at 04:23
  • 2
    playlist.download_all() is now deprecated and does not work. – Jean-Pierre Schnyder Aug 03 '20 at 15:11
10

The solutions above no longer work. Here's a code which downloads the sound stream of the videos referenced in a Youtube playlist. Pytube3 is used, not pytube. Note that the playlist must be public for the download to succeed. Also, if you want to download the full video instead of the sound track only, you have to modify the value of the Youtube tag constant. The empty Playlist.videos list fix was taken from this Stackoverflow post:PyTube3 Playlist returns empty list

import re
from pytube import Playlist

YOUTUBE_STREAM_AUDIO = '140' # modify the value to download a different stream
DOWNLOAD_DIR = 'D:\\Users\\Jean-Pierre\\Downloads'

playlist = Playlist('https://www.youtube.com/playlist?list=PLzwWSJNcZTMSW-v1x6MhHFKkwrGaEgQ-L')

# this fixes the empty playlist.videos list
playlist._video_regex = re.compile(r"\"url\":\"(/watch\?v=[\w-]*)")

print(len(playlist.video_urls))

for url in playlist.video_urls:
    print(url)

# physically downloading the audio track
for video in playlist.videos:
    audioStream = video.streams.get_by_itag(YOUTUBE_STREAM_AUDIO)
    audioStream.download(output_path=DOWNLOAD_DIR)
Jean-Pierre Schnyder
  • 1,025
  • 1
  • 12
  • 22
4
from pytube import Playlist
playlist = Playlist('https://www.youtube.com/playlist?list=PL6gx4Cwl9DGCkg2uj3PxUWhMDuTw3VKjM')
print('Number of videos in playlist: %s' % len(playlist.video_urls))
for video_url in playlist.video_urls:
    print(video_url)
playlist.download_all()
https://www.youtube.com/watch?v=HjuHHI60s44
https://www.youtube.com/watch?v=Z40N7b9NHTE
https://www.youtube.com/watch?v=FvziRqkLrEU
https://www.youtube.com/watch?v=XN2-87haa8k
https://www.youtube.com/watch?v=VgI4UKyL0Lc
https://www.youtube.com/watch?v=BvPIgm2SMG8
https://www.youtube.com/watch?v=DpdmUmglPBA
https://www.youtube.com/watch?v=BmVmJi5dR9c
https://www.youtube.com/watch?v=pYNuKXjcriM
https://www.youtube.com/watch?v=EWONqLqSxYc
https://www.youtube.com/watch?v=EKmLXiA4zaQ
https://www.youtube.com/watch?v=-DHCm9AlXvo
https://www.youtube.com/watch?v=7cRaGaIZQlo
https://www.youtube.com/watch?v=ZkcEB96iMFk
https://www.youtube.com/watch?v=5Fcf-8LPvws
https://www.youtube.com/watch?v=xWLgdSgsBFo
https://www.youtube.com/watch?v=QcKYFEgfV-I
https://www.youtube.com/watch?v=BtSQIxDPnLc
https://www.youtube.com/watch?v=O5kh_-6e4kk
https://www.youtube.com/watch?v=RuWVDz-48-o
https://www.youtube.com/watch?v=-yjc5Y7Wbmw
https://www.youtube.com/watch?v=C5T59WsrNCU
https://www.youtube.com/watch?v=MWldNGdX9zE

I'm using pytube3 9.6.4, not pytube.

Now Playlist.video_urls works well.

And Playlist.populate_video_urls() function was deprecated.

Green Tree
  • 159
  • 1
  • 3
  • The 2 preceding examples don't work for me. I get the message : ```Number of videos in playlist: 0 C:\Users\...\test.py:4: DeprecationWarning: Call to deprecated function download_all (This function will be removed in the future. Please iterate through .videos). playlist.download_all('out/') C:\ProgramData\Anaconda3\lib\site-packages\pytube\contrib\playlist.py:216: DeprecationWarning: Call to deprecated function _path_num_prefix_generator (This function will be removed in the future.). prefix_gen = self._path_num_prefix_generator(reverse_numbering)``` – Fred Jul 18 '20 at 10:26
  • I add that I checked that the url of examples has really more than 0 videos (and I tried with some other playlist url, with no effects). And I'm using pytube3 9.6.4 – Fred Jul 18 '20 at 10:32
  • 1
    The answer to my problem was found here : https://stackoverflow.com/questions/62661930/pytube3-playlist-returns-empty-list – Fred Jul 18 '20 at 11:39
2

if one needs to download the highest quality video of each item in a playlist

playlist = Playlist('https://www.youtube.com/watch?v=VZclsCzhzt4&list=PLk-w4cD8sJ6N6ffzp5A4PQaD76RvdpHLP')

for video in playlist.videos:
    print('downloading : {} with url : {}'.format(video.title, video.watch_url))
    video.streams.\
        filter(type='video', progressive=True, file_extension='mp4').\
        order_by('resolution').\
        desc().\
        first().\
        download(cur_dir)
user702846
  • 4,286
  • 4
  • 34
  • 59
2

this code allows you to download a playlist to your assigned folder

import re
from pytube import Playlist
playlist = Playlist('https://www.youtube.com/playlist?list=Pd5k1hvD2apA0DwI3XMiSDqp')   
DOWNLOAD_DIR = 'D:\Video'
playlist._video_regex = re.compile(r"\"url\":\"(/watch\?v=[\w-]*)")    
print(len(playlist.video_urls))    
for url in playlist.video_urls:
    print(url)    
for video in playlist.videos:
    print('downloading : {} with url : {}'.format(video.title, video.watch_url))
    video.streams.\
        filter(type='video', progressive=True, file_extension='mp4').\
        order_by('resolution').\
        desc().\
        first().\
        download(DOWNLOAD_DIR)