AI 秒变中文字幕!繁体/简体全搞定(OpenAI-Whisper教学)

语音转文字并生成字幕的软件如剪映、Filmora、Davinci等,需要充值、有次数限制,而免费版有导出限制和水印、额度有限、限制导出清晰度 。

开源项目Whisper:

优点: 1、支持语言非常多,包括中文 2、字幕格式可生成 .srt、.vtt、.txt等 3、免费开源、准确率高

缺点:需配置 Python 环境、命令行工具,对新手不太友好。


一、下载安装Anaconda和ffmpeg:

Anaconda下载地址: https://repo.anaconda.com/miniconda/Miniconda3-latest-Windows-x86_64.exe

ffmpeg下载地址: https://www.gyan.dev/ffmpeg/builds/ffmpeg-release-essentials.7z

安装python虚拟环境: conda create -n my_env python=3.9 #这里的my_env可根据个人情况输入,python根据项目需要的版本号进行输入。

激活python虚拟环境: conda activate my_env #my_env就是上面创建的虚拟环境的名称。

二、 Whisper(OpenAI)本地部署安装(可离线处理)

项目地址: https://github.com/openai/whisper

▶ 安装方式:

1
pip install git+https://github.com/openai/whisper.git

▶ 字幕生成命令:

1
whisper your_video.mp4 --language Chinese --task transcribe --output_format srt

▶ 推荐增强版字幕生成命令

1
whisper your_video.mp4 --language Chinese --task transcribe --model medium --output_format srt --fp16 False

参数说明:
–model medium:默认是 small,改用 medium 精度更高(还有 base、large 等)

–fp16 False:如果你用的是 CPU(没有 CUDA),必须加这个,否则会报错

–output_dir ./subs:可以指定输出路径(可选)

–verbose True:输出详细进度信息(可选)

▶ 输出文件名说明

执行后会自动生成:

your_video.srt:标准字幕文件

your_video.txt:纯文本逐句转写结果

也可以生成 .vtt 或 .tsv,取决于设置

三、使用 Whisper + 简转繁/繁转简工具

先用 Whisper 提取出字幕,然后用下面任一种方式将 .srt 或 .txt 文件从简体转换为繁体或从繁体转换为简体。

安装 Python 简转繁/繁转简模块 opencc-python

1
pip install opencc

脚本示例(假设你的字幕是 jianti.srt):

1
2
3
4
5
6
7
8
9
10
from opencc import OpenCC

cc = OpenCC('s2t') # 简体转繁体,如繁体转简体则为 cc=OpenCC('t2s')
with open('jianti.srt', 'r', encoding='utf-8') as f:
content = f.read()

converted = cc.convert(content)

with open('fanti.srt', 'w', encoding='utf-8') as f:
f.write(converted)

四、如果电脑有Nvidia显卡,可以安装CUDA版的torch库,来加快语音识别速度

先卸载原来的torch:

1
pip uninstall torch

再安装对应的CUDA版本的torch:

1
pip install torch --index-url https://download.pytorch.org/whl/cu118

这里的cu118要改成适配的版本,如果适配CUDA12.4则改为cu124,依次类推。