python 使用 youtube_dl 检索自动字幕并转换为文字记录

2024-03-31

从youtube中提取的自动英文字幕包含不可读形式的信息和重复的文本信息。

welcome<00:00:01.790><c> my</c><00:00:02.790><c> name</c><c.colorCCCCCC><00:00:02.820><c> is</c><00:00:03.210><c> Helga</c></c><c.colorE5E5E5><00:00:03.449><c> Vieira</c><00:00:03.929><c> and</c><00:00:04.080><c> this</c></c>

00:00:04.670 --> 00:00:04.680 align:start position:0%
welcome my name<c.colorCCCCCC> is Helga</c><c.colorE5E5E5> Vieira and this
 </c>

My code:

def captions_test02(url):
    ydl = youtube_dl.YoutubeDL({'writesubtitles': True, 'allsubtitles': True, 'writeautomaticsub': True})
    res = ydl.extract_info(url, download=False)
    if res['requested_subtitles'] and res['requested_subtitles']['en']:
        print('Grabbing vtt file from ' + res['requested_subtitles']['en']['url'])
        response = requests.get(res['requested_subtitles']['en']['url'], stream=True)
        f1 = open("testfile01.txt", "w")
        f1.write(response.text)
        f1.close()
        if len(res['subtitles']) > 0:
            print('manual captions')
        else:
            print('automatic_captions')
    else:
        print('Youtube Video does not have any english captions')

if __name__ == '__main__':
    captions_test02("https://www.youtube.com/watch?v=tCTqNZW0wIk&t=2s")

对于获得正确的成绩单有什么建议吗?初始点:https://shkspr.mobi/blog/2018/09/convert-webvtt-to-a-transcript-using-python/ https://shkspr.mobi/blog/2018/09/convert-webvtt-to-a-transcript-using-python/


要消除时间戳并获得更好的成绩单,您可以使用正则表达式:

def captions_test02(url):
    ydl = youtube_dl.YoutubeDL({'writesubtitles': True, 'allsubtitles': True, 'writeautomaticsub': True})
    res = ydl.extract_info(url, download=False)
    if res['requested_subtitles'] and res['requested_subtitles']['en']:
        print('Grabbing vtt file from ' + res['requested_subtitles']['en']['url'])
        response = requests.get(res['requested_subtitles']['en']['url'], stream=True)
        f1 = open("testfile01.txt", "w")
        new = re.sub(r'\d{2}\W\d{2}\W\d{2}\W\d{3}\s\W{3}\s\d{2}\W\d{2}\W\d{2}\W\d{3}','',response.text)
        f1.write(new)
        f1.close()
        if len(res['subtitles']) > 0:
            print('manual captions')
        else:
            print('automatic_captions')
    else:
        print('Youtube Video does not have any english captions')

if __name__ == '__main__':
    captions_test02("https://www.youtube.com/watch?v=d1CDP6sMuLA") 
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

python 使用 youtube_dl 检索自动字幕并转换为文字记录 的相关文章

  • 上传时的 Google Drive API——这些额外的空行从何而来?

    总结一下该程序 我从我的 Google 云端硬盘下载一个文件 然后在本地计算机中打开并读取一个文件 file a txt 然后在我的计算机中打开另一个文件 file b txt 处于附加模式 并且在使用这个新的 file b 更新我的 Go
  • 如何使用 colorchecker 在 opencv 中进行颜色校准?

    我有数码相机获取的色彩检查器图像 我如何使用它来使用 opencv 校准图像 按照以下颜色检查器图像操作 您是想问如何进行颜色校准或如何使用 OpenCV 进行校准 为了进行颜色校准 您可以使用校准板的最后一行 灰色调 以下是您应该逐步进行
  • TensorFlow:带有轴选项的 bincount

    在 TensorFlow 中 我可以使用 tf bincount 获取数组中每个元素的计数 x tf placeholder tf int32 None freq tf bincount x tf Session run freq feed
  • NumPy linalg.eig

    我有这个烦人的问题 但我还没有弄清楚 我有一个矩阵 我想找到特征向量 所以我写 val vec np linalg eig mymatrix 然后我得到了 vec 我的问题是 当我小组中的其他人对相同的矩阵 mymatrix 做同样的事情时
  • 在 python 3 中使用子进程

    我使用 subprocess 模块在 python 3 中运行 shell 命令 这是我的代码 import subprocess filename somename py in practical i m using a real fil
  • Python 使用 Gstreamer 访问 USB 麦克风时遇到问题,以便在 Raspberry Pi 上使用 Pocketsphinx 执行语音识别

    所以Python的表现就好像它根本听不到我的麦克风发出的任何声音 问题就在这里 我有一个Python 2 7 假设使用的脚本Gstreamer通过以下方式访问我的麦克风并为我进行语音识别口袋狮身人面像 我在用着脉冲音频我的设备是树莓派 我的
  • “一旦获取切片就无法更新查询”。最佳实践?

    由于我的项目的性质 我发现自己不断地从查询集中取出切片 如下所示 Thread objects filter board requested board id order by updatedate 10 但这给我带来了实际对我选择的元素进
  • PyTorch 给出 cuda 运行时错误

    我对我的代码做了一些小小的修改 以便它不使用 DataParallel and DistributedDataParallel 代码如下 import argparse import os import shutil import time
  • 根据 Pandas 中的列表对多列进行排序

    感谢有关如何根据 pandas 中的倍数列表对给定多列进行排序的任何提示 如下所示 import pandas as pd sort a a d e sort b s1 s3 s6 sort c t1 t2 t3 df pd DataFra
  • Pandas:如何将数据框插入 Clickhouse

    我正在尝试将 Pandas 数据框插入 Clickhouse 这是我的代码 import pandas import sqlalchemy as sa uri clickhouse default localhost default ch
  • 如何从 JSON 响应重定向?

    所以我尝试使用 Flask 和 Javascript 上传器 Dropzone 上传文件并在上传完成后重定向 文件上传正常 但在烧瓶中使用传统的重定向 return redirect http somesite com 不执行任何操作 页面
  • Python Django-如何从输入文件标签读取文件?

    我不想将文件保存在我的服务器上 我只想在下一页中读取并打印该文件 现在我有这个 index html
  • Python:我不明白 sum() 的完整用法

    当然 我明白你使用 sum 与几个数字 然后它总结所有 但我正在查看它的文档 我发现了这一点 sum iterable start 第二个参数 start 的作用是什么 这太尴尬了 但我似乎无法通过谷歌找到任何示例 并且对于尝试学习该语言的
  • 为什么我应该使用 WSGI?

    使用 mod python 一段时间了 我读了越来越多关于 WSGI 有多好的文章 但没有真正理解为什么 那么我为什么要切换到它呢 有什么好处 这很难吗 学习曲线值得吗 为了用 Python 开发复杂的 Web 应用程序 您可能会使用更全面
  • `pyqt5'错误`元数据生成失败`

    我正在尝试安装pyqt5使用带有 M1 芯片和 Python 3 9 12 的 mac 操作系统 我怀疑M1芯片可能是原因 我收到一个错误metadata generation failed 最小工作示例 directly in the t
  • 带有 RotatingFileHandler 的 Python 3 记录器超出 maxBytes 限制

    我使用以下代码来限制日志文件的大小 最小示例 import logging from logging handlers import RotatingFileHandler Set up logfile and message loggin
  • OSX 上的 locale.getlocale() 问题

    我需要获取系统区域设置来执行许多操作 最终我想使用 gettext 翻译我的应用程序 我打算在 Linux 和 OSX 上分发它 但我在 OSX Snow Leopard 上遇到了问题 python Python 2 5 2 r252 60
  • 导入错误:无法导入名称“时间戳”

    我使用以下代码在 python 3 6 3 中成功安装了 ggplot conda install c conda forge ggplot 但是当我使用下面的代码将其导入笔记本时 出现错误 from ggplot import Impor
  • bs4 `next_sibling` VS `find_next_sibling`

    我在使用时遇到困难next sibling 并且类似地与next element 如果用作属性 我不会得到任何返回 但如果用作find next sibling or find next 然后就可以了 来自doc https www cru
  • tkinter:打开一个带有按钮提示的新窗口[关闭]

    Closed 这个问题需要调试细节 help minimal reproducible example 目前不接受答案 用户如何按下 tkinter GUI 中的按钮来打开新窗口 我只需要非常简单的解决方案 如果代码也能被解释那就太好了 这

随机推荐