VITS音频数据自动处理程序
最近在玩自定义音色的模型,然后就找到了用的最多的这个项目,VITS。不过原本的,或者说最早的VITS程序已经没人维护了,设置原本的不支持中文,当初在尝试的时候改了好久后来实在是改不动就去网上找了找其他的。然后用的别人整合的So-VITS这个挺好用的然后还有其他版本都挺多的。但是不管什么版本作为最开始用来训练的音频数据还是需要自己来准备的。所以我这里就自己做了一个程序可以通过录制好的视频自动把视频内的语音拆分然后生成对应可以直接使用的音频数据。好接下来进行介绍。
1.视频的准备
虽说可以对视频进行处理和识别。但是对于视频内的东西也还是有一定要求的。我这里以鸣潮内的长离作为例子。在其他的比如原神、明日方舟之类的游戏都可以尝试。
好开始说要求。
1.视频画质没有要求
随便什么画质的视频都可以尽可能低一点会好因为这样不怎么占用存储空间,读取起来也会快一点
2.声音要求
要求基本在这里,录制视频时要保证关闭游戏的其他音乐,不要出现先除了角色语音以外的其他声音。录制时不要开启麦克风录制这点很重要,一定要注意不要开麦克风。尽可能保证视频内的声音只有角色说话的声音。所以视频开始录制和结束录制的时间要找好。不然就后续剪辑一下。然后想角色语音比较多的就是。大概的地方就是像下面这样。
然后录制的时候记住一定一定一定要保证把那些背景音乐关闭!!!!一定要!!!
2.代码的实现
代码的话不算长,但是也有些东西要提前准备,因为会使用到语音自动识别,所以会需要先准备好一个语音模型。我这里使用的是openai的whisper模型,最开始使用的是VOSK的模型,但是简单点的还行长的识别出来的太奇怪了。openai的就很方便。使用效果很好。
对应模型对应电脑要求
CPU 运行
-
最低配置
(适用于 tiny/base 模型):
- 处理器: 双核处理器(如 Intel Core i3 或 AMD Ryzen 3)。
- 内存 (RAM): 4 GB(tiny 模型)到 8 GB(base 模型)。
- 存储: 至少 1-2 GB 空闲空间(用于模型文件和缓存)。
- 性能: 处理速度较慢,尤其是长音频文件,可能需要几倍于音频时长的处理时间。
-
推荐配置
(适用于 small/medium 模型):
- 处理器: 四核处理器(如 Intel Core i5 或 AMD Ryzen 5)。
- 内存: 16 GB。
- 存储: 5-10 GB(模型文件大小从几百 MB 到几 GB 不等)。
- 性能: 处理中等长度音频(10-30 分钟)时较合理,但仍较慢。
-
大型模型(如 large-v3):
- 处理器: 高性能多核 CPU(如 Intel Core i7/i9 或 AMD Ryzen 7/9)。
- 内存: 32 GB 或更高。
- 存储: 10 GB+。
- 性能: 仅靠 CPU 运行 large 模型会非常慢,推荐使用 GPU。
GPU 运行(推荐)
- 显卡:
- NVIDIA GPU(支持 CUDA),如 GTX 1060、RTX 2060 或更高。
- 显存 (VRAM):
- tiny: 2 GB。
- base: 4 GB。
- small: 6 GB。
- medium: 8-10 GB。
- large-v3: 10-12 GB(推荐 16 GB 以确保流畅运行)。
- 内存: 16-32 GB(与 CPU 共享)。
- 存储: 同 CPU 配置。
- 性能: GPU 加速可将处理速度提高 10 倍以上,适合实时或批量处理。
模型文件大小
-
tiny: ~75 MB
-
base: ~142 MB
-
small: ~466 MB
-
medium: ~1.5 GB
-
large-v3: ~3.06 GB
存储空间需预留这些大小,外加输入音频和输出文件空间。
模型下载链接
{
"tiny.en": "https://openaipublic.azureedge.net/main/whisper/models/d3dd57d32accea0b295c96e26691aa14d8822fac7d9d27d5dc00b4ca2826dd03/tiny.en.pt",
"tiny": "https://openaipublic.azureedge.net/main/whisper/models/65147644a518d12f04e32d6f3b26facc3f8dd46e5390956a9424a650c0ce22b9/tiny.pt",
"base.en": "https://openaipublic.azureedge.net/main/whisper/models/25a8566e1d0c1e2231d1c762132cd20e0f96a85d16145c3a00adf5d1ac670ead/base.en.pt",
"base": "https://openaipublic.azureedge.net/main/whisper/models/ed3a0b6b1c0edf879ad9b11b1af5a0e6ab5db9205f891f668f8b0e6c6326e34e/base.pt",
"small.en": "https://openaipublic.azureedge.net/main/whisper/models/f953ad0fd29cacd07d5a9eda5624af0f6bcf2258be67c92b79389873d91e0872/small.en.pt",
"small": "https://openaipublic.azureedge.net/main/whisper/models/9ecf779972d90ba49c06d968637d720dd632c55bbf19d441fb42bf17a411e794/small.pt",
"medium.en": "https://openaipublic.azureedge.net/main/whisper/models/d7440d1dc186f76616474e0ff0b3b6b879abc9d1a4926b7adfa41db2d497ab4f/medium.en.pt",
"medium": "https://openaipublic.azureedge.net/main/whisper/models/345ae4da62f9b3d59415adc60127b97c714f32e89e936602e85993674d08dcb1/medium.pt",
"large-v1": "https://openaipublic.azureedge.net/main/whisper/models/e4b87e7e0bf463eb8e6956e646f1e277e901512310def2c24bf0e11bd3c28e9a/large-v1.pt",
"large-v2": "https://openaipublic.azureedge.net/main/whisper/models/81f7c96c852ee8fc832187b0132e569d6c3065a3252ed18e56effd0b6a73e524/large-v2.pt",
"large-v3": "https://openaipublic.azureedge.net/main/whisper/models/e5b1a55b89c1367dacf97e3e19bfd829a01529dbfdeefa8caeb59b3f1b81dadb/large-v3.pt",
"large": "https://openaipublic.azureedge.net/main/whisper/models/e5b1a55b89c1367dacf97e3e19bfd829a01529dbfdeefa8caeb59b3f1b81dadb/large-v3.pt",
"large-v3-turbo": "https://openaipublic.azureedge.net/main/whisper/models/aff26ae408abcba5fbf8813c21e62b0941638c5f6eebfb145be0c9839262a19a/large-v3-turbo.pt",
"turbo": "https://openaipublic.azureedge.net/main/whisper/models/aff26ae408abcba5fbf8813c21e62b0941638c5f6eebfb145be0c9839262a19a/large-v3-turbo.pt",
}
这些下载链接在whisper库的__init__.py文件内都有可以自己查看。
完整代码
好的也不分析代码了我们来看看完整代码吧。代码进行过ai精简和分析愿意看的可以好好研究一下,不愿意的复制过去自己运行差了啥对应的补就好。
# 导入必要的库
import os # 操作系统路径处理
from moviepy.editor import VideoFileClip # 视频处理库
import whisper # OpenAI的语音识别库
import opencc # 简繁体转换工具
from pydub import AudioSegment # 音频处理库
def extract_audio(video_path, output_wav="temp_audio.wav"):
"""从视频文件中提取音频并保存为WAV格式"""
video = VideoFileClip(video_path) # 加载视频文件
audio = video.audio # 提取音频轨道
audio.write_audiofile(output_wav, fps=22050) # 以22050Hz采样率保存音频
return output_wav # 返回生成的音频文件路径
def split_audio(audio_path,
output_dir="output4",
model_path="./model/medium.pt",
audio_prefix="changli",
path_prefix="data/changli/"):
"""核心处理函数:语音识别、音频分割和文件生成"""
# 路径格式标准化处理
path_prefix = path_prefix.rstrip("/") + "/" # 确保路径前缀以斜杠结尾
os.makedirs(output_dir, exist_ok=True) # 创建输出目录(如果不存在)
# 初始化模型和工具
model = whisper.load_model(model_path) # 加载Whisper语音识别模型
converter = opencc.OpenCC('t2s') # 创建繁简转换器(繁体转简体)
# 语音识别处理
result = model.transcribe(audio_path, language="zh", verbose=True) # 识别中文语音
segments = result["segments"] # 获取时间分段信息
# 准备音频处理
audio = AudioSegment.from_wav(audio_path) # 加载WAV音频文件
file_list = [] # 初始化文件列表
file_index = 1 # 文件编号计数器
print("开始处理音频分割和识别...")
# 遍历每个识别片段
for segment in segments:
start = segment["start"] * 1000 # 开始时间(秒转毫秒)
end = segment["end"] * 1000 # 结束时间(秒转毫秒)
text = segment["text"] # 原始识别文本
simplified_text = converter.convert(text) # 转换为简体中文
# 计算持续时间(秒)
duration = (end - start) / 1000
if duration >= 1.0: # 过滤短于1秒的片段
# 生成文件名和路径
output_filename = f"{audio_prefix}_{file_index:03d}.wav" # 格式化为三位数编号
output_path = os.path.join(output_dir, output_filename) # 完整输出路径
# 音频分割处理
segment_audio = audio[start:end] # 截取音频片段
segment_audio.export(output_path, format="wav") # 保存为WAV文件
# 控制台输出进度
print(f"片段 {file_index:03d} ({os.path.basename(output_path)}): {simplified_text}")
# 构建文件列表条目
formatted_path = f"{path_prefix}{output_filename}" # 组合前缀和文件名
file_list.append(f"{formatted_path}|{simplified_text}") # 按格式添加到列表
file_index += 1 # 递增文件编号
# 写入文件列表
with open(os.path.join(output_dir, "filelist.txt"), "w", encoding="utf-8") as f:
f.write("\n".join(file_list)) # 将列表转换为换行分隔的文本
return file_list # 返回生成的文件列表
if __name__ == "__main__":
# ====================== 用户可配置参数 ======================
video_path = "mp4flie/changli2_1.mp4" # 原始视频文件路径
model_path = "./model/medium.pt" # Whisper模型文件路径
output_dir = "output5" # 输出文件保存目录
audio_prefix = "changli" # 输出音频文件名的前缀
path_prefix = "data/changli/" # filelist中显示的路径前缀
# ==========================================================
# 执行处理流程
audio_path = extract_audio(video_path) # 第一步:提取音频
split_audio( # 第二步:处理音频
audio_path,
output_dir=output_dir,
model_path=model_path,
audio_prefix=audio_prefix,
path_prefix=path_prefix
)
print(f"处理完成!分割后的音频和文件列表已保存在 {output_dir}")
然后运行过程中会出现识别结果类似这样
可以看到识别出来的文本为繁体,这个是因为我使用的模型的原因,使用更好的模型理论上可以解决这个问题,但是性能消耗也高了。所以有使用opencc这个库进行繁体转简体就可以有如下结果
可以看的还是不错的。但是一些专用词,或者说同音字就不太准,但是不影响训练。应该。