我在使用pytube.caption时发现了'start'的关键错误

问题描述 投票:0回答:2

我正在尝试通过 pytube 下载 Youtube 字幕。

一切都很好,我成功地通过 xml_captions 下载了视频及其标题。

但是,当我尝试将其转换为 .srt 格式时,出现了关键错误。

---> 83             start = float(child.attrib["start"])  
KeyError: 'start'

我想知道出了什么问题。 我的代码是

pip install pytube
from pytube import YouTube
# misc``
import os
import shutil
import math
import datetime
video=YouTube('https://www.youtube.com/watch?v=xxydY73V9bQ')
caption = video.captions['a.en']
caption.xml_captions
srt_format = caption.xml_caption_to_srt(caption.xml_captions)
python keyerror pytube
2个回答
1
投票

如果仍然相关:YouTube 似乎已经改变了字幕的处理方式。 这里有可能的解决方案的讨论。


0
投票
您需要替换一些代码

标题.py

--- captions.py.orig 2023-04-01 03:12:55.176218471 +0200 +++ captions.py 2023-04-01 03:12:59.320187087 +0200 @@ -82,14 +82,14 @@ segments = [] root = ElementTree.fromstring(xml_captions) - for i, child in enumerate(list(root)): + for i, child in enumerate(list(root[0])): text = child.text or "" caption = unescape(text.replace("\n", " ").replace(" ", " "),) try: - duration = float(child.attrib["dur"]) + duration = float(child.attrib["d"]) / 1000.0 except KeyError: duration = 0.0 - start = float(child.attrib["start"]) + start = float(child.attrib["t"]) / 1000.0 end = start + duration sequence_number = i + 1 # convert from 0-indexed to 1. line = "{seq}\n{start} --> {end}\n{text}\n".format(
不是我在 

https://github.com/pytube/pytube/issues/1477 上找到的

© www.soinside.com 2019 - 2024. All rights reserved.