python对批量WAV音频进行等长分割的方法实现
作者:养只小刺猬吧 时间:2023-01-29 18:21:21
对批量WAV音频进行等长分割
对WAV格式的音频以相同长度进行分割。
import os
import wave
import numpy as np
import pylab as plt
import librosa
path = r"D:\cutaudio\test"
files = os.listdir(path)
files = [path + "\\" + f for f in files if f.endswith('.wav')]
CutTime = 4 #单位长度4s
def CutAudios():
for i in range(len(files)):
FileName = files[i]
f = wave.open(r"" + FileName, 'rb')
params = f.getparams() #读取音频文件信息
nchannels, sampwidth, framerate, nframes = params[:4] #声道数, 量化位数, 采样频率, 采样点数
str_data = f.readframes(nframes)
f.close()
wave_data = np.frombuffer(str_data, dtype=np.short)
#根据声道数对音频进行转换
if nchannels > 1:
wave_data.shape = -1, 2
wave_data = wave_data.T
temp_data = wave_data.T
else:
wave_data = wave_data.T
temp_data = wave_data.T
CutFrameNum = framerate * CutTime
Cutnum =nframes/CutFrameNum #音频片段数
StepNum = int(CutFrameNum)
StepTotalNum = 0
for j in range(int(Cutnum)):
FileName = r"D:\cutaudio\11\\" + files[i][-17:-4] +"-"+ str(j) + ".wav"
temp_dataTemp = temp_data[StepNum * (j):StepNum * (j + 1)]
StepTotalNum = (j + 1) * StepNum
temp_dataTemp.shape = 1, -1
temp_dataTemp = temp_dataTemp.astype(np.short)# 打开WAV文档
f = wave.open(FileName, 'wb')
# 配置声道数、量化位数和取样频率
f.setnchannels(nchannels)
f.setsampwidth(sampwidth)
f.setframerate(framerate)
f.writeframes(temp_dataTemp.tostring()) # 将wav_data转换为二进制数据写入文件
f.close()
if __name__ == '__main__' :
CutAudios()
来源:https://blog.csdn.net/vitA_gsm/article/details/99756032
标签:python,WAV,音频,等长分割
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
浅谈pytorch、cuda、python的版本对齐问题
2022-12-03 13:04:54
Python中 map()函数的用法详解
2021-03-25 08:16:44
![](https://img.aspxhome.com/file/2023/0/73000_0s.png)
如何将yolov5中的PANet层改为BiFPN详析
2023-08-12 18:08:38
![](https://img.aspxhome.com/file/2023/9/64349_0s.jpg)
详解python实现简单区块链结构
2023-01-05 22:47:14
![](https://img.aspxhome.com/file/2023/4/112144_0s.jpg)
Python Pillow Image Invert
2023-10-02 12:33:30
python抓取需要扫微信登陆页面
2022-03-01 16:15:32
![](https://img.aspxhome.com/file/2023/4/105024_0s.jpg)
mysql如何跨时区迁移数据
2010-03-25 10:26:00
聊聊Python中关于a=[[]]*3的反思
2021-09-08 05:12:46
![](https://img.aspxhome.com/file/2023/7/99257_0s.png)
Mysql数据库清理binlog日志命令详解
2024-01-14 09:04:07
![](https://img.aspxhome.com/file/2023/4/96494_0s.jpg)
python文件操作的简单方法总结
2023-11-20 06:31:02
python查看矩阵的行列号以及维数方式
2021-03-25 20:24:58
![](https://img.aspxhome.com/file/2023/6/115756_0s.jpg)
解决pycharm安装第三方库失败的问题
2023-07-13 19:32:12
![](https://img.aspxhome.com/file/2023/8/120278_0s.jpg)
python使用opencv驱动摄像头的方法
2023-08-26 17:00:49
使用python 打开文件并做匹配处理的实例
2022-01-15 16:27:30
Navicat远程连接SQL Server并转换成MySQL步骤详解
2024-01-14 20:58:48
![](https://img.aspxhome.com/file/2023/4/72504_0s.png)
Python决策树分类算法学习
2023-01-28 17:27:05
![](https://img.aspxhome.com/file/2023/2/79492_0s.png)
读"设计的3个C"之构图
2008-12-24 13:25:00
python采集博客中上传的QQ截图文件
2021-03-03 16:45:25
GOLANG使用Context实现传值、超时和取消的方法
2024-05-22 10:13:13
Python实现提取XML内容并保存到Excel中的方法
2022-03-14 19:06:43
![](https://img.aspxhome.com/file/2023/7/90317_0s.png)