Search code examples
pythonpytube

Issue trying to download with pytube


Since 8 months,

I've been using a self-made Youtube video and playlist downloader using the pytube package.

But now, it doesn't work anymore and shows an error everytime I try to run it.

It's important to say that I have the SAME issue trying to dowload playlists and videos.

Here is my code:

#importing packages
from pytube import YouTube
import os
from pytube import Playlist
import time

time_1 = 0
time_2 = 0
times = []
moyenne = 0

def playlist():
    p = Playlist(input('Enter Playlist name  \n>> '))

    for url in p.video_urls:
        time_1 = time.time()
        print(time_1)
        yt = YouTube(url)
        print(yt)

        # extract only audio

        audio = yt.streams.filter(only_audio=True).first()

        video = yt.streams.filter(only_video=True).first()

        # download the file
        out_file_audio = audio.download(output_path="C:/Users/Eleve/Downloads/BAM")
        out_file_video = video.download(output_path="C:/Users/Eleve/Downloads/video")

        # save the file
        base, ext = os.path.splitext(out_file_audio)
        new_file = base + '.mp3'
        os.rename(out_file_audio, new_file)

        base, ext = os.path.splitext(out_file_video)
        new_file = base + '.mp4'
        os.rename(out_file_video, new_file)

        # result if success
        print(yt.title + " has been successfully downloaded.")
        time_2 = time.time()
        print("time : ", time_2 - time_1)
        times.append(time_2 - time_1)
        moyenne = sum(times)/len(times)
        print("average time : ", moyenne)
    print("Playlist downloaded !")


def video(n):
    yt = YouTube(input("Enter video url \n >> "))

    if n == 1:  #Only video

        video = yt.streams.filter(only_video=True).first()

        out_file_video = video.download(output_path="C:/Users/Eleve/Downloads/video")

        base, ext = os.path.splitext(out_file_video)
        new_file = base + '.mp4'
        os.rename(out_file_video, new_file)

    if n == 2: #only audio

        audio = yt.streams.filter(only_audio=True).first()

        out_file_audio = audio.download(output_path="C:/Users/Eleve/Downloads/audio")

        base, ext = os.path.splitext(out_file_audio)
        new_file = base + '.mp3'
        os.rename(out_file_audio, new_file)

    if n == 3:

        video = yt.streams.filter(only_video=True).first()
        audio = yt.streams.filter(only_audio=True).first()

        out_file_video = video.download(output_path="C:/Users/Eleve/Downloads/video")
        out_file_audio = audio.download(output_path="C:/Users/Eleve/Downloads/audio")

        base, ext = os.path.splitext(out_file_video)
        new_file = base + '.mp4'
        os.rename(out_file_video, new_file)

        base, ext = os.path.splitext(out_file_audio)
        new_file = base + '.mp3'
        os.rename(out_file_audio, new_file)


    print(yt.title + " has been successfully downloaded.")


type_ = input("Playlist (p) or video (v) ? \n >> ")


if type_ == "p":
    playlist()
elif type_ == "v":
    n = int(input(": "))
    video(n)
else:
    print("ValueError")

Here is the error I get when trying to download the playlists:

Traceback (most recent call last):

  File "C:\Users\Eleve\YouTubeDownloader2\main.py", line 97, in <module>

    playlist()

  File "C:\Users\Eleve\YouTubeDownloader2\main.py", line 23, in playlist

    audio = yt.streams.filter(only_audio=True).first()

  File "C:\Users\Eleve\YouTubeDownloader2\venv\lib\site-packages\pytube\__main__.py", line 
            296, in streams
    return StreamQuery(self.fmt_streams)

  File "C:\Users\Eleve\YouTubeDownloader2\venv\lib\site-packages\pytube\__main__.py", line 

176, in fmt_streams
    stream_manifest = extract.apply_descrambler(self.streaming_data)

  File "C:\Users\Eleve\YouTubeDownloader2\venv\lib\site-packages\pytube\__main__.py", line    
     161, in streaming_data

    return self.vid_info['streamingData']
KeyError: 'streamingData'

And here is the error I get when trying to download a video:

Traceback (most recent call last):

  File "C:\Users\Eleve\YouTubeDownloader2\main.py", line 100, in <module>

    video(n)
  File "C:\Users\Eleve\YouTubeDownloader2\main.py", line 55, in video

    video = yt.streams.filter(only_video=True).first()

  File "C:\Users\Eleve\YouTubeDownloader2\venv\lib\site-packages\pytube\__main__.py", line 

296, in streams

    return StreamQuery(self.fmt_streams)

  File "C:\Users\Eleve\YouTubeDownloader2\venv\lib\site-packages\pytube\__main__.py", line 

176, in fmt_streams

    stream_manifest = extract.apply_descrambler(self.streaming_data)

  File "C:\Users\Eleve\YouTubeDownloader2\venv\lib\site-packages\pytube\__main__.py", line 

161, in streaming_data

    return self.vid_info['streamingData']

KeyError: 'streamingData'

Hope someone could help to figure out where's the issue

I try to copy-paste another python youtube video downloader from a

tutorial site in a new project, but even this time, I got the same error

(I checked it, and the packages are installed, so it's not the issue).

I'm suprised, because 8 months ago, when I try to copy-paste the same code from the Internet to a new project,

the code worked and didn't show any error worked.


Solution

  • Use pip list to determine what version of pytube you have installed. If if is not the very lastest version use pip uninstall pytube to remove it and then use pip install pytube to install the latest version.

    See this link for more info: Exception in Tkinter callback Traceback (most recent call last) error