'pytube Exception Handle

I having problems with the pytube exception handler...

it will not work for me :( I get no messages from the pytube exception handler... and stop with Traceback Error

(python 3.9.5 / pytube 10.8.1)

import pytube
from pytube import YouTube


mp4URL = "https://www.youtube.com/watch?v=21X5lGlDOfg"

try:
    yt = YouTube(mp4URL)
except pytube.exceptions.PytubeError as e:
    print (e)
except pytube.exceptions.HTMLParseError as e:
    print (e)
except pytube.exceptions.LiveStreamError as e:
    print (f'Video {mp4URL} ist ein LiveStream, skipping. ',e)
except pytube.exceptions.VideoPrivate as e:
    print (f'Video {mp4URL} ist ein privates Video, skipping. ',e)
except pytube.exceptions.VideoUnavailable as e:
    print (f'Video {mp4URL} ist nicht verfügbar, skipping. ',e)
else:
    print(f'Downloading video: {mp4URL}')
    try:
        stream = yt.streams.filter(mime_type='video/mp4',progressive=True,file_extension='mp4').order_by('resolution').desc().first()
    except pytube.exceptions.PytubeError as e:
        print (e)
    except pytube.exceptions.HTMLParseError as e:
        print (e)
    except pytube.exceptions.LiveStreamError as e:
        print (f'Video {mp4URL} ist ein LiveStream, skipping. ',e)
    except pytube.exceptions.VideoPrivate as e:
        print (f'Video {mp4URL} ist ein privates Video, skipping. ',e)
    except pytube.exceptions.VideoUnavailable as e:
        print (f'Video {mp4URL} ist nicht verfügbar, skipping. ',e)
    else:
        stream.download()

What i doing wrong???

Error:

Downloading video: https://www.youtube.com/watch?v=TGijtGSUEJs
Traceback (most recent call last):
  File "C:\Python37\Scripts\test.py", line 34, in <module>
    stream = yt.streams.filter(mime_type='video/mp4',progressive=True,file_extension='mp4').order_by('resolution').desc().first()
  File "C:\Python37\lib\site-packages\pytube\__main__.py", line 308, in streams
    return StreamQuery(self.fmt_streams)
  File "C:\Python37\lib\site-packages\pytube\__main__.py", line 220, in fmt_streams
    extract.apply_descrambler(self.player_config_args, fmt)
  File "C:\Python37\lib\site-packages\pytube\extract.py", line 486, in apply_descrambler
    streaming_data = stream_data["player_response"]["streamingData"]
KeyError: 'streamingData'

if i put except Exception as e: print(e) in the Code, i get the ErrorMsg 'stream Data'.

The YouTube Url is a LiveStream, so i should not to be able to download this video



Solution 1:[1]

just delete first exception becouse it will run each time or delete all except first one

Solution 2:[2]

From your code example you had forgotten to put the line stream = yt.streams.filter(mime_type='video/mp4',progressive=True,file_extension='mp4').order_by('resolution').desc().first() into the exception block, if you did

try:
    all the pytube code

and then add the list of exceptions, python will be able to catch the exception.

Of course this is all from the implication that you had not put the line into the try and except block, as it was not in the question.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 mohammedy
Solution 2 eroc1234