WAV format audio capture

【Function】
Usually we will get the disputed audio in WAV format, such as the full version of a certain singer's concert, and we want to split the audio or some parts of it are the audio segments we actually want. For this We need to design a program that can intercept according to the specified time, minutes and seconds.
The functions required to be implemented are as follows:
1. According to the specified start and end time, minutes and seconds, intercept the WAV audio corresponding to the start and end period;
2. Set the audio name, album name, singer name and other information for each intercepted audio
3. Can be in Batch setting in a parameter table

[Required installation packages/dependencies]
1.WAV
2.pandas
3.sox: The installation location needs to be consistent with the location set in the code. In this example, place sox.exe in sox_path="D:\sox\sox- 14-4-2\sox.exe"

[Folder]
Put different files in different folders to distinguish them
1.f_0='./Original audio/'
2.f_k='./Interception parameters/'
3.f_s='./Running results/'
4. f_p='./audiobuffer/'

【Complete code】

import wave
import os
import time
import pandas as pd

f_0='./原始音频/'   #原始音频存放文件夹
f_p='./音频缓存/'    #转化音频存放文件夹
f_k='./截取参数/'
f_s='./运行结果/'    #截断音频存放文件夹
sox_path="D:\sox\sox-14-4-2\sox.exe"
abs_path=os.path.dirname(os.path.abspath(__file__))
cmd='cd /d {}{} && for %i in (*.wav) do {} %i -b 16 -e signed-integer {}{}%i'.format(abs_path,f_0.replace('.',''),sox_path,abs_path,f_p.replace('.',''))  #shell脚本,解决wave初始非pcm格式问题
os.system(cmd)
t_0=time.time()
x_0=os.listdir(f_0)   #获取音频文件名称列表,已含后缀
t_p=[f_p+i for i in x_0]  #生成转化音频文件夹内文件的完整路径列表
df=pd.read_excel(f_k+os.listdir(f_k)[0],sheet_name='参数表',header=0,keep_default_na=False)
name_list=[list(df['Music_Name'])[i] +'-'+list(df['Artist'])[i]+'_'+list(df['Album'])[i] for i in range(len(df))]

#转化读取到的时分秒数据
def get_s_time(h,m,s):
    s_time=h*3600+m*60+s
    return s_time

#主函数
def cut_voice(stream,name,t_begin,t_end):
    wf = wave.open(stream, 'rb')
    nchannels, sampwidth, framerate, nframes = wf.getparams()[:4]
    strData = wf.readframes(nframes)  # 读取音频,字符串格式
    t_s = nframes / framerate
    cut_data = strData[int(t_begin * len(strData)/t_s):int(t_end * len(strData)/t_s)]
    wf.close()

    outfile = f_s + name+'.wav'  # 定义存储路径以及文件名
    outwave = wave.open(outfile, 'wb')
    comptype = "NONE"
    compname = "not compressed"
    outwave.setparams((nchannels, sampwidth, framerate, nframes==int(len(cut_data)),comptype, compname))
    outwave.writeframes(cut_data)
    outwave.close()

if __name__ == '__main__':
    for i in range(len(df)):
        t_begin=get_s_time(list(df['h_begin'])[i],list(df['m_begin'])[i],list(df['s_begin'])[i])
        t_end = get_s_time(list(df['h_end'])[i], list(df['m_end'])[i], list(df['s_end'])[i])
        cut_voice(t_p[0],name_list[i],t_begin,t_end)
    t_1 = time.time()
    print('\n全程耗时:%.2fs' % (t_1 - t_0))

[Parameter table description]
Parameter configuration table
h/m/s means hours, minutes and seconds, you only need to input the start and end hours, minutes and seconds corresponding to each audio (split input), and the corresponding audio clips can be output

[Notes]
1. The installation location of sox.exe must be the same as the sox-path in the code, and it is an absolute path; sox_path="D:\sox\sox-14-4-2\sox.exe" 2. Intercept
parameters After setting the table, please save and close.
3. The corresponding folder needs to be created in advance
Folder name

Guess you like

Origin blog.csdn.net/Apollo_Guang/article/details/127728689