工作涉及,写了个根据音频静音段切割音频的小脚本,理论上可以做到按句按段分割长语音
[Asm] 纯文本查看 复制代码 from pydub import AudioSegment
from pydub.silence import split_on_silence
import os
# 设置分割参数
min_silence_len = 400 # 最小静音长度
silence_thresh = -60 # 静音阈值,越小越严格
keep_silence = 200 # 保留静音长度
all_dur_sec = 0.0
max_dur_sec = 15.0
long_dir = "D:/剪辑前"
save_dir = "D:/剪辑后"
if not os.path.exists(save_dir):
os.system(f"mkdir -p {save_dir}")
with open(os.path.join(save_dir, "meta.info"), mode='w', encoding='utf-8') as fW:
for fname in os.listdir(long_dir):
if 'wav' not in fname:
continue
ffname = fname.split(".")[0]
print(f"current file {fname} ... ")
# 读取音频文件
audio = AudioSegment.from_file(
os.path.join(long_dir, fname), format="wav")
segments = split_on_silence(audio, min_silence_len=min_silence_len,
silence_thresh=silence_thresh, keep_silence=keep_silence)
print(f"Len: {len(segments)}")
for i in range(0, len(segments)):
sp_wav = f"{ffname}_{str(i).zfill(4)}.wav"
segments[i].export(
os.path.join(save_dir, sp_wav), format="wav")
fW.write(f"{sp_wav}\t{segments[i].duration_seconds}\n")
all_dur_sec += segments[i].duration_seconds
if segments[i].duration_seconds > max_dur_sec:
print(f"{sp_wav}\t{segments[i].duration_seconds}")
print(f"all dur sec : {all_dur_sec/60.0} min")
# 计算分割数量
# num_segments = int(audio.duration_seconds/60/3) # 每个音频大概三分钟,计算歌曲数量
# # 分割音频文件
# for i in range(-10, 0):
# if len(segments) <= num_segments:
# print(f"分割成功,共分割出 {len(segments)} 段")
# break
# else:
# print(f"当前阈值为 {i},分割出 {len(segments)} 段,继续尝试")
|