10000 Fixed KeyError on generate_srt_captions command by MYusufY · Pull Request #1959 · pytube/pytube · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Fixed KeyError on generate_srt_captions command #1959

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

8000
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 40 additions & 24 deletions pytube/captions.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,31 +76,47 @@ def float_to_srt_time_format(d: float) -> str:
return time_fmt + ms

def xml_caption_to_srt(self, xml_captions: str) -> str:
"""Convert xml caption tracks to "SubRip Subtitle (srt)".
"""Convert xml caption tracks to "SubRip Subtitle (srt)".

:param str xml_captions:
XML formatted caption tracks.
"""
segments = []
try:
root = ElementTree.fromstring(xml_captions)
except ElementTree.ParseError as e:
print(f"Warning: Failed to parse the XML captions. Error: {e}")
return "" # Return an empty string if parsing fails

try:
for i, child in enumerate(list(root[0])): # Assuming the first child is the correct element
text = child.text or ""
caption = unescape(text.replace("\n", " ").replace(" ", " "),)
try:
duration = float(child.attrib["d"]) / 1000.0
except KeyError:
duration = 0.0
try:
start = float(child.attrib["t"]) / 1000.0
except KeyError:
start = 0.0
end = start + duration
sequence_number = i + 1 # convert from 0-indexed to 1.
line = "{seq}\n{start} --> {end}\n{text}\n".format(
seq=sequence_number,
start=self.float_to_srt_time_format(start),
end=self.float_to_srt_time_format(end),
text=caption,
)
segments.append(line)

except IndexError as e:
print(f"Warning: The XML structure does not contain the expected elements. Error: {e}")
except Exception as e:
print(f"An unexpected error occurred: {e}")

return "\n".join(segments).strip()

:param str xml_captions:
XML formatted caption tracks.
"""
segments = []
root = ElementTree.fromstring(xml_captions)
for i, child in enumerate(list(root)):
text = child.text or ""
caption = unescape(text.replace("\n", " ").replace(" ", " "),)
try:
duration = float(child.attrib["dur"])
except KeyError:
duration = 0.0
start = float(child.attrib["start"])
end = start + duration
sequence_number = i + 1 # convert from 0-indexed to 1.
line = "{seq}\n{start} --> {end}\n{text}\n".format(
seq=sequence_number,
start=self.float_to_srt_time_format(start),
end=self.float_to_srt_time_format(end),
text=caption,
)
segments.append(line)
return "\n".join(segments).strip()

def download(
self,
Expand Down
0