Pytube: Pytube length sometimes missing causes crash

Created on 7 Aug 2019  路  6Comments  路  Source: pytube/pytube

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'
stale

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 ;-)

All 6 comments

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.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

xenonstride picture xenonstride  路  19Comments

kpister picture kpister  路  23Comments

RONNCC picture RONNCC  路  20Comments

Ikebani picture Ikebani  路  36Comments

harindu95 picture harindu95  路  20Comments