8

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'
TylerH
  • 20,799
  • 66
  • 75
  • 101
Aaryan R Soni
  • 91
  • 1
  • 1
  • 4

4 Answers4

10

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.

linomassaro
  • 101
  • 3
  • 1
    This worked for me. Thanks so much! However, how can we get that response instead of it being printed to the console. The issue is I am using it to a tkinter app, hence the user cannot see the prompt and instruction as it is printed in the console. Thanks again – syncster31 May 02 '23 at 03:57
5

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.

1

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---------')
RazzieE
  • 21
  • 1
1

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"')
moken
  • 3,227
  • 8
  • 13
  • 23
  • 1
    Answer needs supporting information Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](https://stackoverflow.com/help/how-to-answer). – moken Aug 03 '23 at 07:31