[BUG] Retrieving video length runs into a Type Error
I have not installed pytube from source, but instead looked at the source code in the master branch and seen that the relevant line is still in there.
Bug description I run into an error when trying to determine the length of a video: https://github.com/pytube/pytube/blob/d3d18691b3e99b2d3b4d620446b088a1c32896c6/pytube/main.py#L383
I do not quite understand for what reason there is no videoDetails to get; but I do run into the default {} , such that the second get returns None, and then the cast to int raises a TypeError.
This does not give me any information about what went wrong or what would need to be done differently.
To Reproduce
- The video or playlist url that is causing the error: https://youtu.be/QyiRT0T8IQY
- The code where the problem is occurring: https://github.com/pytube/pytube/blob/d3d18691b3e99b2d3b4d620446b088a1c32896c6/pytube/main.py#L377-L383
Also, my code calling this function:
yt_video = YouTube(yt_video_url)
try:
yt_video.check_availability()
except PytubeError as err:
msg = f"Error during handling youtube video {yt_video_url}. Error was: \n{err}"
logger.error(msg)
raise VideoAvailabilityError(msg) from err
return int(yt_video.length)
Expected behavior I would expect the function to return an int or to raise a meaningful exception, which has some useful information about why the video length could not be determined.
Output It is in a pytest, which returns:
self = <pytube.__main__.YouTube object: videoId=QyiRT0T8IQY>
@property
def length(self) -> int:
"""Get the video length in seconds.
:rtype: int
"""
> return int(self.vid_info.get('videoDetails', {}).get('lengthSeconds'))
E TypeError: int() argument must be a string, a bytes-like object or a number, not 'NoneType'
venv/lib/python3.8/site-packages/pytube/__main__.py:383: TypeError
System information
- Python version 3.8.13
- Pytube version 12.1.2
- Command used to install pytube: pip install pytube==12.1.2
Thank you for contributing to PyTube. Please remember to reference Contributing.md
I checked #1493 and the root cause is the same; the handling in the code is different but I would say both ways are not good.
I tried this with python 3.10.7 and I could not reproduce this issue, my pytube version is also 12.1.2
Sorry, forgot to mention: the error is not occurring every time I run the snippet.
If you allow some speculation I would guess that sometimes a request sent to the youtube server gets an adequate response, but sometimes the information just does not arrive.
Same here it's so annoying. It does not happen every time. It's def a bug but I don't think that the devs can fix it because it doesn't occure every time.
Running into the same problem
running into the same problem
Looks like updating to pytube 15.0.0 solved the issue!
15.0.0 seem to fix this, recommend doing an update!