5

I am trying to make a code which can download the entire playlist from YouTube. It worked for some playlist but not working for few playlists. One of the playlist I have shown in my code below. Also feel free to add more features on this code. If there is already a code to download the playlist so please share the link with me

`

from bs4 import BeautifulSoup
from pytube import YouTube
import urllib.request
import time
import os


## list of link parsed by bs4
s = []


## to name and save the playlist folder and download path respectively 
directory = 'Hacker101'
savePath = "G:/Download/video/"
path = os.path.join(savePath, directory)


## link parser
past_link_here = "https://www.youtube.com/playlist?list=PLxhvVyxYRviZd1oEA9nmnilY3PhVrt4nj"
html_page = urllib.request.urlopen(past_link_here)
x = html_page.read()
soup = BeautifulSoup(x, 'html.parser')
for link in soup.findAll('a'):
    k = link.get('href')
    if 'watch' in k:
        s.append(k)
    else:
        pass


## to create playlist folder
def create_project_dir(x):
    if not os.path.exists(x):
        print('Creating directory ' + x)
        os.makedirs(x)
create_project_dir(path)


## downloading videos by using links from list s = []
for x in set(s):
    link="https://www.youtube.com" + x
    yt = YouTube(link)
    k = yt.title
    file_path = path + '\\' + k + '.mp4'
    try:
        if os.path.exists(file_path):
            print(k + ' is \n' + "already downloaded")
        else:
            j = yt.streams.filter(progressive=True).all()
            l = yt.streams.first()
            print(k + ' is downloading....')
            l.download(path)
            time.sleep(1)
            print('downloading compleat')

##    except Exception:
##        print('error')

    except KeyError as e:
        print('KeyError') % str(e)

`

enter image description here

vardhan.negi
  • 362
  • 1
  • 4
  • 15

3 Answers3

5

Your issue appears to be related to a bug that was fixed today by giacaglia . Based on the Github Commit the solution to the bug can be fixed by modifying your mixins.py as detailed in the link. Your playlists should work without running into the KeyError: 'url_encoded_fmt_stream_map' issue you had above.

R.Zane
  • 320
  • 4
  • 16
1

I have asked this question before the release of new version of pytube this problem is solved in pytube3 you just need to install it by using pip cmd i.e pip install pytube3

vardhan.negi
  • 362
  • 1
  • 4
  • 15
0

kindly check out this doc pytube3 ive used the methods in it and it has actually worked.. the first step is basically to upgrade the pytube library : pip3 install pytube3 --upgrade the load your code next... for the normal youtube video downlaods:

from pytube import YouTube
url = input("Paste the URL here -->>")
yt = YouTube(url)
YouTube(url).streams[0].download()

for the whole playlist

from pytube import Playlist

url = input("Paste the URL here -->>")
playlist = Playlist(url)
for my_videos in playlist:
     my_videos.streams.get_highest_resolution().download()

have Fun make something cool!!!

Jay R
  • 7
  • 2