赞
踩
非常抱歉,这个程序就是个雏形,非常不完善,输入需要手动编辑,凑活着可以用,请自己完善吧。
输入:油管英文视频的视频号 例如 “04j_yB4CZPM”
输出:两个srt文件
核心思想是用到了开源的MIT License的 youtube_transcript_api 这个库来为我们服务。其中,用了YouTubeTranscriptApi, 用于获取字幕。
也用到了 其中 formatters 子库里面的 SRTFormatter,用于将字幕转化为srt格式。
https://github.com/jdepoix/youtube-transcript-api
(来源: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()
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()
我承认,有很多需要完善的地方,但是对我日常工作而言,已经够用了。抛砖引玉,请大家继续努力吧。
加油好运!
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。