github开源推荐,基于whisper的字幕生成和字幕翻译工具——再也没有看不懂

[复制链接]
查看930 | 回复0 | 2023-8-14 14:05:09 | 显示全部楼层 |阅读模式


1.简介

GitHub - qinL-cdy/auto_ai_subtitle
github上新开源的一款字幕生成和字幕翻译的整合工具,可以根据视频中提取到的音频来转换成字幕,再根据需要将字幕进行翻译

2.效果



3.使用

1)安装ffmpeg
安装ffmpeg的教程比较多,就不详细介绍了,Windows上安装完成后记得添加环境变量,最后在cmd中输入"ffmpeg –version",有相应打印即可
2)拉取代码
使用git拉取代码即可,没有git的可以参考网上资料安装一下
  1. git clone https://github.com/qinL-cdy/auto_ai_subtitle.git
复制代码
3)安装python依赖
使用pip安装相关依赖,当然前提是已经安装好python环境了
进入git下来的工程目录,可以看到有一个requirements.txt
在目录下执行
  1. pip install -r requirements.txt
复制代码
 这样pip就会自动安装所有需要的依赖了
4)填写配置信息
打开当前目录下的config.yaml文件,根据提示填写对应的信息,例如:
  1. #输入的视频文件
  2. input: D:\download\ChainsawMan-03.mp4
  3. #中间过程会生成的音频文件
  4. output: D:\download\ChainsawMan-03.mp3
  5. #生成的原始字幕文件
  6. srt_path: D:\download\ChainsawMan-03.srt
  7. #生成的翻译后的字幕文件
  8. srt_translate_path: D:\download\ChainsawMan-03-zh.srt
  9. #翻译时开启多少线程
  10. translate_threads: 10
  11. #翻译源语言
  12. from: ja
  13. #翻译目标语言
  14. to: zh
复制代码
5)执行程序
最后一步,使用python命令执行程序即可 
  1. python main.py
复制代码
6)其他用法
观察main.py文件:
  1. import yaml
  2. from script import translate_tool, audio_tool, whisper_tool
  3. if __name__ == '__main__':
  4.     with open('config.yaml', encoding='utf-8') as f:
  5.         config = yaml.load(f.read(), Loader=yaml.FullLoader)
  6.     print("audio extract begin")
  7.     audio_tool.audio_extract(config['input'], config['output'])
  8.     print("audio extract success")
  9.     print("whisper begin")
  10.     whisper_tool.do_whisper(config['output'], config['srt_path'])
  11.     print("whisper success")
  12.     print("translate begin")
  13.     translate_tool.do_translate(config['srt_path'], config['srt_translate_path'], config['from'], config['to'],
  14.                                 config['translate_threads'])
  15.     print("translate success")
  16.     print("success")
复制代码
可以看到脚本是由多个独立的调用步骤组合而成的,所以也可以根据自己的需要调整来自定义执行某一个或多个功能
例如,只执行音频提取和字幕生成,但不进行翻译:
  1. import yaml
  2. from script import translate_tool, audio_tool, whisper_tool
  3. if __name__ == '__main__':
  4.     with open('config.yaml', encoding='utf-8') as f:
  5.         config = yaml.load(f.read(), Loader=yaml.FullLoader)
  6.     print("audio extract begin")
  7.     audio_tool.audio_extract(config['input'], config['output'])
  8.     print("audio extract success")
  9.     print("whisper begin")
  10.     whisper_tool.do_whisper(config['output'], config['srt_path'])
  11.     print("whisper success")
  12.     #print("translate begin")
  13.     #translate_tool.do_translate(config['srt_path'], config['srt_translate_path'], config['from'], config['to'],config['translate_threads'])
  14.     #print("translate success")
  15.     print("success")
复制代码
4.原理

1)音频提取
  1. import ffmpeg
  2. def audio_extract(input, output):
  3.     ffmpeg.input(input, vn=None).output(output).run()
复制代码
 使用了ffmpeg的能力,其中vn=None代表忽略视频,所以执行后只会输出对应的音频
2)字幕提取
字幕生成使用了openai开源的whisper
  1. def do_whisper(audio, srt_path):
  2.     model = whisper.load_model("base")
  3.     print("whisper working...")
  4.     result = model.transcribe(audio)
  5.     print("whisper execute success")
  6.     print("writing srt file...")
  7.     write_srt(result['segments'], srt_path)
  8.     print("write srt success")
复制代码
 这里只是用了最基本的模型,所以在精度上可能不够高,使用者可以基于whisper开源的模型做进一步优化
3)字幕翻译
字幕翻译使用了常用的开源库translate,就不做进一步介绍了,感兴趣可以查看相关资料


来源:https://blog.csdn.net/qq_37913898/article/details/131144796
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则