pytube icon indicating copy to clipboard operation
pytube copied to clipboard

[BUG] Getting the video title from video object throws exception

Open Cortlando opened this issue 2 years ago • 10 comments

Before creating an issue

Describe the bug A clear and concise description of what the bug is.

When looping through the video entries of a playlist object and printing the title of each video, it will throw an exception for video in playlist.videos: print(video.title)

It's happened on several different videos in the playlist, below are examples

  • https://youtube.com/watch?v=mLiGOk5b5Lo
  • https://youtube.com/watch?v=RhcrqbzaClY
  • https://youtube.com/watch?v=vwqpjLfmjeQ
  • https://youtube.com/watch?v=I4xf6S8Kqpg
  • https://youtube.com/watch?v=HOAKXWTtIuE

To Reproduce Please provide the following information: from pytube import YouTube, Playlist p = Playlist('https://www.youtube.com/playlist?list=PLS6SQWK_3TqK_WXM3Na89dYyLKlWOPg5H') def generatePlaylistFile(playlist): for video in playlist.videos: print(video.title)

Expected behavior A clear and concise description of what you expected to happen. Expected to print the title of each video in the playlist

Output

Traceback (most recent call last): File "C:\Python311\Lib\site-packages\pytube_main_.py", line 341, in title self._title = self.vid_info['videoDetails']['title'] ~~~~~~~~~~~~~^^^^^^^^^^^^^^^^ KeyError: 'videoDetails'

During handling of the above exception, another exception occurred:

Traceback (most recent call last): File "D:\PythonGang\Youtube Playlist Archiver\main.py", line 23, in main() File "D:\PythonGang\Youtube Playlist Archiver\main.py", line 17, in main youtubefuncs.generatePlaylistFile(playlist) File "D:\PythonGang\Youtube Playlist Archiver\youtubefuncs\youtubefuncs.py", line 14, in generatePlaylistFile playlistfile.write(f'{video.title}\n') ^^^^^^^^^^^ File "C:\Python311\Lib\site-packages\pytube_main_.py", line 346, in title raise exceptions.PytubeError( pytube.exceptions.PytubeError: Exception while accessing title of https://youtube.com/watch?v=mLiGOk5b5Lo. Please file a bug report at https://github.com/pytube/pytube

System information Please provide the following information:

  • Python version (run python --version) Python 3.11.1
  • Pytube version (run print(pytube.__version__) in python)
  • 12.1.2
  • Command used to install pytube python -m pip install git+https://github.com/pytube/pytube

Cortlando avatar Jan 24 '23 00:01 Cortlando

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

github-actions[bot] avatar Jan 24 '23 00:01 github-actions[bot]

I've seen the same error, but it appears at random. Looking in main.py where the exception occurs, when I examine self.vid_info, playabilityStatus shows status = "ERROR" and reason = "This video is unavailable".

meen6thi avatar Jan 24 '23 04:01 meen6thi

So I checked what happens when I run my script and I also see that in self.vid_info, playabilityStatus shows status = "ERROR" and reason = "This video is unavailable". I took the same video that the exception was thrown on using this code yt = YouTube('https://www.youtube.com/watch?v=t5OPuC-HpaE') and I see that playableStatus = 'Ok'. So I wonder if the issue has something to do with how the playlist object works/ looping through the playlist object?

Cortlando avatar Jan 24 '23 21:01 Cortlando

I should also note that when I have run my script again after seeing this error while iterating a Playlist or Channel object, that it has always successfully retrieved the title for the video where I previously saw the error.

meen6thi avatar Jan 25 '23 00:01 meen6thi

This seems to happen randomly, and isn't a rate-limiting thing - I've added sleeps between fetches, and run the script multiple times. Each time it falls over on a different video.

andyskipper avatar Jan 25 '23 17:01 andyskipper

I've done a bit more digging on this - the issue seems to be that YouTube is running random tests for the prevention of media plays on third-party applications. That means that, randomly, it'll reply with "The following content is not available on this app". My assumption is that this is because every instantiation of a pytube.YouTube object goes through the steps to enable streaming or downloading, even if what you need is only metadata and not the stream itself.

Is there a way to force the YouTube object to only retrieve metadata, without preparing the object for streaming/downloading?

andyskipper avatar Jan 26 '23 16:01 andyskipper

Bumping this. I have tried at multiple times for downloading - using a script that has worked in the past; I see the same behavior where this is randomly not working depending on how YouTube renders it I am guess as @andyskipper calls out. Curious if anyone has any ideas or suggestions on a fix? Thanks.

bahree avatar Feb 05 '23 00:02 bahree

I have tried retry mechanisms as referenced in #1473 but to no avail...

Furthermore after implementing retry with sleep, it's seems Youtube has throttled me longer term - now I cannot fetch any videos.

If anyone has any ideas, do let us know.

Also #1466 and #1473 should likely be merged.

alexerhardt avatar Apr 27 '23 08:04 alexerhardt

I have the same problem. It takes about 100 - 150 tries (While True) to "find" the video.

aceuna avatar Apr 27 '23 09:04 aceuna

#1575 works like a charm. Fixes the issue without performance hits.

alexerhardt avatar May 05 '23 07:05 alexerhardt