使用python+ffmpeg开发的wav分割小工具

工具分享:

最近项目有接触到音频神经网络训练,但是准备训练数据实在是太难受了,所以用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 

这就是切割好后的样子了
运行结果

  • 1
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值