调用 Python 开源库,获取油管英文视频的手动或自动英文srt字幕,以及自动中文简体翻译srt字幕
创始人
2024-11-05 03:10:18
0

前提条件

非常抱歉,这个程序就是个雏形,非常不完善,输入需要手动编辑,凑活着可以用,请自己完善吧。

开源声明:此文代码引用了一个开源MIT License的Python库,其他代码是本人自写自用。你可以随便用,但是本人不负责有关代码的任何责任。

概述

输入:油管英文视频的视频号 例如 “04j_yB4CZPM”
输出:两个srt文件

  1. 手动英文(没有则是自动英文)srt 字幕,即eng.srt
  2. 自动中文简体翻译srt字幕,即zh.srt

核心开源库 (开源的MIT License授权,不存在版权问题,请审核放行!!!

核心思想是用到了开源的MIT License的 youtube_transcript_api 这个库来为我们服务。其中,用了YouTubeTranscriptApi, 用于获取字幕。
也用到了 其中 formatters 子库里面的 SRTFormatter,用于将字幕转化为srt格式。

https://github.com/jdepoix/youtube-transcript-api

如果你引用 youtube_transcript_api ,请在代码中 增加一个 MIT License 声明文件 MIT_LICENSE.TXT

(来源:https://opensource.org/license/MIT)

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

代码正文开始,先引用开源库

from youtube_transcript_api import YouTubeTranscriptApi   # the base class to inherit from when creating your own formatter. from youtube_transcript_api.formatters import Formatter  # some provided subclasses, each outputs a different string format. from youtube_transcript_api.formatters import JSONFormatter from youtube_transcript_api.formatters import TextFormatter from youtube_transcript_api.formatters import WebVTTFormatter from youtube_transcript_api.formatters import SRTFormatter 

设定油管视频号(你需要改成你的视频号)

vid = "04j_yB4CZPM" #think twice before drink milk``` 

获取字幕列表

transcript_list = YouTubeTranscriptApi.list_transcripts(vid) 

尝试提取手动英文字幕,没有则提取自动英文字幕,因为手动字幕一般来说,质量更高

try:     transcript = transcript_list.find_manually_created_transcript(['en'])   except:      try:         # or automatically generated ones         transcript = transcript_list.find_generated_transcript(['en'])      except:         print("cannot find auto en")          ten = transcript.fetch()        

提取自动翻译的中文字幕。注意:这里有完善的机会,你可以优先获取手动中文字幕,如果没有,再获取翻译中文简体字幕。因为在我的使用场景,很少有手动中文字幕,所以,这里我就直接先照顾我自己的使用场景了。

translated_transcript = transcript.translate('zh-Hans') tzh = translated_transcript.fetch()  

把英文、中文字幕转化为srt格式

formatter = SRTFormatter() srt_formatted_en = formatter.format_transcript(ten) srt_formatted = formatter.format_transcript(tzh) 

英文字幕保存为文件

with open(r"eng.srt", "w+", encoding='utf-8') as fen:     for sfen in srt_formatted_en:         fen.write(sfen)     fen.close()     

中文字幕保存为文件

with open(r"zh.srt", "w+", encoding='utf-8') as f:      for sf in srt_formatted:         f.write(sf)      f.close()       

我承认,有很多需要完善的地方,但是对我日常工作而言,已经够用了。抛砖引玉,请大家继续努力吧。

加油好运!

相关内容

热门资讯

透视辅助!aapoker怎么拿... 透视辅助!aapoker怎么拿好牌,aapoker免费透视脚本,存在挂教程(有挂黑科技);1、透视辅...
小程序广东雀神智能插件免费,开... 小程序广东雀神智能插件免费,开心泉州小程序辅助免费下载,白金岛手游的作弊码细节1、小程序广东雀神智能...
透视数据!wepoker怎么买... 透视数据!wepoker怎么买辅助(透视)一直真的是有挂(玩家教程)1、每一步都需要思考,不同水平的...
透视了解!aapoker怎么控... 透视了解!aapoker怎么控制牌,aapoker辅助软件合法吗,解密教程(有挂辅助)1、任何aap...
广东麻雀有什么技巧,来趣广西辅... 广东麻雀有什么技巧,来趣广西辅助,白金岛辅助有用吗解说;1、来趣广西辅助系统规律教程、来趣广西辅助辅...
透视了解!wepoker私人局... 透视了解!wepoker私人局俱乐部(透视)果然真的有挂(新2025教程);1、操作简单,无需注册,...
透视私人局!aapoker透视... 透视私人局!aapoker透视脚本,aapoker怎么控制牌,透视教程(有挂插件);在进入aapok...
广东雀神智能ios插件安装包,... 广东雀神智能ios插件安装包,欢乐联盟辅助软件,闲逸透视app为什么免费介绍;1)欢乐联盟辅助软件辅...
透视能赢!wepoker可以免... 透视能赢!wepoker可以免费玩吗(透视)总是真的是有挂(微扑克教程)1、上手简单,内置详细流程视...
透视挂透视!aapoker透视... 透视挂透视!aapoker透视脚本,aapoker辅助器怎么用,科技教程(有挂插件)在进入aapok...