【问题标题】:How to generate valid timestamps from YouTube subtitles, downloaded with wrong timestamps? (using pytube)如何从使用错误时间戳下载的 YouTube 字幕生成有效时间戳? (使用 pytube)
【发布时间】:2022-11-19 05:46:40
【问题描述】:

使用 pytube,我正在尝试下载 YouTube 视频,翻译字幕并将翻译后的字幕嵌入到视频中,然后将其下载到我的电脑上。

这是我的代码的一部分,经过更改以便于理解。

from pytube import YouTube as YT
yt = YT("https://www.youtube.com/watch?v=ZFGAz6vZx1E")

caption_code = ''
try:
  captions = yt.captions['en']
  caption_code = 'en'
except:
  try:
    captions = yt.captions['a.en']
    caption_code = 'a.en'
  except Exception as e:
    raise e

captions = yt.captions.get_by_language_code(caption_code)
test_captions(captions)

### just a function to test how's the cations are structured.
def test_captions(captions):
  caption_list = []
  index = 0

  for line in str(captions.generate_srt_captions()).split('\n'):
    if index == 0:
      caption_list.append({})

    if index in (1, 2):
      caption_list[len(caption_list)-1][('time', 'caption')[index-1]] = line
      index += 1

    if line == '':
      index = 0

  for dic in caption_list:
    print('{} : {}'.format(dic['time'], dic['caption']))

在原始 YouTube 视频中,字幕从第一秒开始(应该在 00:00:01,000 左右)

首先下载带有时间戳的字幕句子: “00:01:20,000 --> 00:52:00,000:伙计们 john elder 是怎么回事”

从控制台可以看出,根据 SRT 时间戳约定,字幕的时间戳有误。

(SRT 时间戳约定在 https://www.3playmedia.com/ 解释)

1st timestamp 基本上是说这句话应该从1sr分20秒,到52分显示,这明显是错误的。

是否可以修复它,如果不能,我该如何规范字幕的时间戳以适应有效的 SRT 时间戳格式?

【问题讨论】:

    标签: python caption pytube srt


    【解决方案1】:

    当您将标题生成为 xml 时,您会注意到由于某种原因时间乘以 1000

    Time after "t=" is the when text starts to appear in seconds, "d=" is when it ends

    所以我只是浪费了时间,将其除以 1000,将其设为“小时:分钟:秒”,获取文本并将所有内容放入我的 file.srt

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-01-17
      • 2010-11-15
      • 2019-12-29
      • 1970-01-01
      • 1970-01-01
      • 2021-02-14
      • 2023-04-07
      • 1970-01-01
      相关资源
      最近更新 更多