Pytube Keyerror 'Streamdata' While Downloading a Video

I was writing a code to see available streams of the video by writing below code

from pytube import Playlist
from pytube import YouTube as YT
import threading as th
import time

plist=input('Enter the playlist: ')

videos=list(Playlist(plist))
i=videos[0]
video=YT(i)
strm=video.streams.filter(res="720p")
print(strm)

from above code I got error like this

Traceback (most recent call last):
  File "D:\Practicals\Python\ML\youtube\temp.py", line 11, in <module>
    strm=video.streams.filter(res="720p")
         ^^^^^^^^^^^^^
  File "D:\Python311\Lib\site-packages\pytube\__main__.py", line 296, in streams
    return StreamQuery(self.fmt_streams)
                       ^^^^^^^^^^^^^^^^
  File "D:\Python311\Lib\site-packages\pytube\__main__.py", line 176, in fmt_streams
    stream_manifest = extract.apply_descrambler(self.streaming_data)
                                                ^^^^^^^^^^^^^^^^^^^
  File "D:\Python311\Lib\site-packages\pytube\__main__.py", line 161, in streaming_data
    return self.vid_info['streamingData']
           ~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^
KeyError: 'streamingData'
1

I had the same problem and fixed it by adding the use_oauth and allow_oauth_cache attributes to YT(i).

from pytube import Playlist
from pytube import YouTube as YT
import threading as th
import time

plist=input('Enter the playlist: ')

videos=list(Playlist(plist))
i=videos[0]
video=YT(i, use_oauth=True, allow_oauth_cache=True)
strm=video.streams.filter(res="720p")
print(strm)

This will require you to connect to youtube via your browser once, but then it will allow you to download the videos.

1

Got the same error while using pytube 12.1.2.

video=YT(i, use_oauth=True, allow_oauth_cache=True)

did not work for me.

After upgrading pytube (to 15.0.0), the error went away.

This should work...

from pytube import Playlist
from pytube import YouTube as YT
import threading as th
import time

DOWNLOAD_DIR = 'C:\\download\\folder'

plist=input('Enter a playlist URL: ')

videos=list(Playlist(plist))

for i in range(len(videos)):
    print(i,'-',videos[i])

    video   = YT(videos[i], use_oauth=True, allow_oauth_cache=True)
    stream  = video.streams.get_by_itag(140)
    stream.download(output_path=DOWNLOAD_DIR)
    
print('----------done---------')
3

Add this in the begining of the script.
This has done the job for me [even after a long time].

import os
os.system('cmd /c "pip install --upgrade pytube"')
1

First locate pytube library location

Click on "innertube.py" and edit the code on line 78 replacing client='ANDROID' with client='WEB'

It worked pretty well for me

Marcus Vance

Marcus Vance

Cybersecurity & Digital Privacy Researcher

Marcus Vance is a cybersecurity auditor and technology writer dedicated to educating the public about online safety, data privacy regulations, enterprise security, and emerging cyber threats.

Share this article
Twitter Facebook Pinterest