工具分享:
最近项目有接触到音频神经网络训练,但是准备训练数据实在是太难受了,所以用python+ffmpeg写了个自动分割wav文件的小工具,使用前需要配置ffmpeg环境哦
工具代码
需要引入三个模块
import os
import re
import subprocess
获取音频总长度
def get_wav_duration(wav_path):
duration = 0
cmd = "ffmpeg -i "+wav_path
result = subprocess.getstatusoutput(cmd)
if result[0] == 1:
regexDuration ="Duration: (.*?):(.*?):(.*?)\.(.*?),"
pat = re.compile(regexDuration)
res = pat.findall(result[1])
if len(res[0]) == 4:
hour = int(res[0][0])
minute = int(res[0][1])
seconds = int(res[0][2])
millisecond = int(res[0][3])*10
duration += hour*3600000
duration += minute*60000
duration += seconds*1000
duration += millisecond
else:
print("未匹配到相关信息,请检查ffmpeg版本")
else :
print("请检查ffmpeg是否可用")
return duration
分割函数
def audio_cut(audio_in_path, audio_out_path, start_time, duration_time):
result = subprocess.getstatusoutput("ffmpeg -i {audio_in_path} -vn -acodec copy -ss {start_time} -t {duration_time} {audio_out_path}".format(audio_in_path = audio_in_path,
audio_out_path = audio_out_path, start_time = start_time, duration_time = duration_time))
return result[0]
主函数
if __name__ == "__main__":
audio_in_path = r'D:/test/test.wav'
audio_time = get_wav_duration(audio_in_path)
if audio_time == 0:
print("无法获取文件播放长度")
audio_time_seconds = audio_time/1000
#切割成4秒一个文件
split_seconds = 4
audio_out_dir = r'D:/test/out'
#是否保存切割到最后长度不足的文件
is_save_last_file = False
start_time = 0
end_time = 0
internal = audio_time_seconds - end_time
index = 0
while internal>0:
index += 1
if internal < split_seconds:
if is_save_last_file:
if index<=9:
audio_out_path = audio_out_dir +'/test_' +'0'+ str(index) + '.wav'
else:
audio_out_path = audio_out_dir +'/test_' + str(index) + '.wav'
audio_cut(audio_in_path, audio_out_path, start_time, split_seconds)
end_time += split_seconds
internal = audio_time_seconds - end_time
else:
if index<=9:
audio_out_path = audio_out_dir +'/test_' +'0'+ str(index) + '.wav'
else:
audio_out_path = audio_out_dir +'/test_' + str(index) + '.wav'
end_time += split_seconds
audio_cut(audio_in_path, audio_out_path, start_time, split_seconds)
start_time += split_seconds
internal = audio_time_seconds - end_time
这就是切割好后的样子了