pytube: [BUG] KeyError 'start' when getting captions from a video
I keep getting KeyError: 'start'
when I try to get a caption from a video in a playlist.
To Reproduce Here is the code I am trying to test:
import pytube
from pytube import Playlist, YouTube
url = "https://www.youtube.com/watch?v=vKA4w2O61Xo&list=PLkahZjV5wKe8WFEwvs69V7JO-Cx57rZ8W"
p = Playlist(url)
for v in p.videos[:3]:
print("trying to get captions for:", v.title)
print(v.captions["a.en"].generate_srt_captions())
This code used to print the caption before updating pytube, but now it breaks with the following trace:
KeyError Traceback (most recent call last)
~\test_pytube.py in <module>
10 for v in p.videos[:3]:
11 print("trying to get captions for:", v.title)
---> 12 print(v.captions["a.en"].generate_srt_captions())
~\AppData\Roaming\Python\Python38\site-packages\pytube\captions.py in generate_srt_captions(s
elf)
49 recompiles them into the "SubRip Subtitle" format.
50 """
---> 51 return self.xml_caption_to_srt(self.xml_captions)
52
53 @staticmethod
~\AppData\Roaming\Python\Python38\site-packages\pytube\captions.py in xml_caption_to_srt(self
, xml_captions)
81 except KeyError:
82 duration = 0.0
---> 83 start = float(child.attrib["start"])
84 end = start + duration
85 sequence_number = i + 1 # convert from 0-indexed to 1.
KeyError: 'start'
System information Please provide the following information:
- Python version: Python 3.8.5
- Pytube version: 11.0.0
- Command used to install pytube: pip install -U pytube
About this issue
- Original URL
- State: open
- Created 3 years ago
- Reactions: 10
- Comments: 19 (1 by maintainers)
Commits related to this issue
- close issue: #1085 — committed to beancookie/pytube by beancookie 3 years ago
- [Fix] Issue: pytube#1085 — committed to JavDomGom/pytube by JavDomGom 2 years ago
- [Fix] Issue: pytube#1085 — committed to JavDomGom/pytube by JavDomGom 2 years ago
- [Fix] Issue: pytube#1085 in 12.1.0 version. — committed to JavDomGom/pytube by JavDomGom 2 years ago
- Update captions.py This PR fixes the bug #1085 — committed to AdventurousSam/pytube_captions_fix by AdventurousSam 6 months ago
- Update captions.py This PR fixes the error mentioned in #1085 — committed to AdventurousSam/pytube_captions_fix by AdventurousSam 6 months ago
Apparently YouTube changed their captions format.
Here’s my version of the function for the function in captions.py:
@maksimbolonkin I changed a bit your code, so now it works for me as well 👌 The issue I’ve found was caused by the fact that some captions are located inside of
<p>
tagSo to fix this bug we can just replace
xml_caption_to_srt
inside ofpytube/captions.py/Caption class
with current code. Hope it’s gonna work for everyone 👍I was trying to download videos from here: https://www.youtube.com/watch?v=gqaHkPEZAew&list=PLoROMvodv4rOSH4v6133s9LFPRHjEmbmJ
The code snippet above didn’t work out for me. So I had to modify it a little. Here’s the code that worked for me just in case anyone needs it:
I have the same problem. Any solutions yet?
@maksimbolonkin thanks for the suggestion, I am using it.
I edited the code very minimally to make it work. This seems to work for me. Please Check for any issues. -> Added .find(“body”) -> Changed the keys from “dur” to “d” and “start” to “t” -> Divided the time by 1000. (to get seconds from miliseconds.)
邮件已收到,通常我在1~2天内回复。如有急事,请直接电话联系 ----我的微信 haijun-data
Thx… works for me