pytube
pytube copied to clipboard
[BUG] Getting the video title from video object throws exception
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
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
Thank you for contributing to PyTube. Please remember to reference Contributing.md
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".
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?
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.
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.
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?
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.
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.
I have the same problem. It takes about 100 - 150 tries (While True) to "find" the video.
#1575 works like a charm. Fixes the issue without performance hits.