网站模板 瀑布流,DW做的网页用网站打不开,我男同同性做视频网站,网站建设的需求是什么前提条件
非常抱歉#xff0c;这个程序就是个雏形#xff0c;非常不完善#xff0c;输入需要手动编辑#xff0c;凑活着可以用#xff0c;请自己完善吧。
开源声明#xff1a;此文代码引用了一个开源MIT License的Python库#xff0c;其他代码是本人自写自用。你可以随…前提条件
非常抱歉这个程序就是个雏形非常不完善输入需要手动编辑凑活着可以用请自己完善吧。
开源声明此文代码引用了一个开源MIT License的Python库其他代码是本人自写自用。你可以随便用但是本人不负责有关代码的任何责任。
概述
输入油管英文视频的视频号 例如 “04j_yB4CZPM” 输出两个srt文件
手动英文没有则是自动英文)srt 字幕即eng.srt自动中文简体翻译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 onestranscript 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(reng.srt, w, encodingutf-8) as fen:for sfen in srt_formatted_en:fen.write(sfen)fen.close() 中文字幕保存为文件
with open(rzh.srt, w, encodingutf-8) as f:for sf in srt_formatted:f.write(sf)f.close() 我承认有很多需要完善的地方但是对我日常工作而言已经够用了。抛砖引玉请大家继续努力吧。
加油好运!