pytube icon indicating copy to clipboard operation
pytube copied to clipboard

[BUG] Retrieving video length runs into a Type Error

Open olbermann opened this issue 3 years ago • 10 comments

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

olbermann avatar Mar 10 '23 08:03 olbermann

Thank you for contributing to PyTube. Please remember to reference Contributing.md

github-actions[bot] avatar Mar 10 '23 08:03 github-actions[bot]

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.

olbermann avatar Mar 10 '23 08:03 olbermann

I tried this with python 3.10.7 and I could not reproduce this issue, my pytube version is also 12.1.2

andyjphu avatar Mar 16 '23 16:03 andyjphu

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.

olbermann avatar Mar 17 '23 07:03 olbermann

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.

MediaEU avatar Apr 26 '23 19:04 MediaEU

Running into the same problem

stonymf avatar May 06 '23 00:05 stonymf

running into the same problem

gary-mu avatar May 10 '23 20:05 gary-mu

Looks like updating to pytube 15.0.0 solved the issue!

gary-mu avatar May 15 '23 15:05 gary-mu

15.0.0 seem to fix this, recommend doing an update!

StandardV avatar May 31 '23 02:05 StandardV