Search code examples
pythonpython-3.xpytube

How to download youtube playlist video using pytube?


I am using Python 3.8.5.

#pip freeze
    beautifulsoup4==4.9.1
    bs4==0.0.1
    packaging==20.4
    pyparsing==2.4.7
    PyQt5==5.15.0
    PyQt5-sip==12.8.0
    PyQtWebEngine==5.15.0
    pytube3==9.6.4
    sip==5.3.0
    six==1.15.0
    soupsieve==2.0.1
    toml==0.10.1
    typing-extensions==3.7.4.2

CODE

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

WARNING

Number of videos in playlist: 0 playlist.py:24: DeprecationWarning: Call to deprecated function download_all (This function will be removed in the future. Please iterate through .videos).
playlist.download_all() /media/hophoet/Nouveau nom/Projects/python/workspace/automates/autovideo/venv/lib/python3.8/site-packages/pytube/contrib/playlist.py:216: DeprecationWarning: Call to deprecated function _path_num_prefix_generator (This function will be removed in the future.).


Solution

  • Based on some research it seems that there is problem between pytube3==9.6.4 and YouTube's HTML code. The issue is related to how pytube3 reads a YouTube URL. The code below solves this issue by using a regex, which matches YouTube's updated HTML code.

    import re
    from pytube import Playlist
    
    playlist = Playlist("https://www.youtube.com/playlist?list=PL6gx4Cwl9DGCkg2uj3PxUWhMDuTw3VKjM")
    playlist._video_regex = re.compile(r"\"url\":\"(/watch\?v=[\w-]*)")
    print('Number of videos in playlist: %s' % len(playlist.video_urls))
    for url in playlist.video_urls:
       print(url)
       
       ###############################
       OUTPUT
       ###############################
       Number of videos in playlist: 23
       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 did note that pytube3 is currently not being supported and that someone forked it to pytubeX. I'm trying to figure out the download, because I cannot get that piece to work with pytube3 or pytubex. I will keep looking at this issue.