On certain urls trying to get the length of a youtube video crashes the program due to a key error in the object itself.
Ex:
https://www.youtube.com/watch?v=4VR-6AS0-l4
Traceback (most recent call last):
if end_time >= yt.length:
File "/opt/python/run/venv/lib/python2.7/site-packages/pytube/__main__.py", line 286, in length
return self.player_config_args['length_seconds']
KeyError: 'length_seconds'
i've got the same error with all the videos i tried :-(
hey i figured out what is happening !
Go to line 286 and change the only line with
return (
self.player_config_args
.get('player_response', {})
.get('videoDetails', {})
.get('lengthSeconds')
)
It works really well for me ;-)
@marcounetB nailed it!
However, if you don't want to edit the source code... considering you have your object like current_video = YouTube(video_url), you just need to use:
current_video.player_config_args.get('player_response').get('videoDetails').get('lengthSeconds')
or
current_video.player_config_args['player_response']['videoDetails']['lengthSeconds']
Happy downloading :)
I have the same problem, I'm using v9.5.2. I see there's a fix here: https://github.com/nficano/pytube/pull/443. Hopefully it'll be patched soon meanwhile I'm using @ricardodeazambuja solution
@chris-saunders @marcounetB @ricardodeazambuja @princelySid fixed in my Python 3 only fork: https://github.com/hbmartin/pytube3
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.
Most helpful comment
hey i figured out what is happening !
Go to line 286 and change the only line with
return ( self.player_config_args .get('player_response', {}) .get('videoDetails', {}) .get('lengthSeconds') )It works really well for me ;-)