wave音频转解码测试python脚本

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了wave音频转解码测试python脚本相关的知识,希望对你有一定的参考价值。

#encoding:utf-8
import csv
import os
import binascii
import struct

inDir = ‘d:/in/‘
outDir = ‘d:/out/‘
#测试录音
sourceVoicePath = inDir + ‘12560469.vox‘
def outVoiceHead(size,format,channel,sampleRate,bytePerSec,blockAlign,bitPerSample):
head = bytearray(b‘\x52\x49\x46\x46‘)
head = head + struct.pack(‘<L‘, size)
head = head + bytearray(b‘\x57\x41\x56\x45\x66\x6D\x74\x20‘)
head = head + bytearray(b‘\x12\x00\x00\x00‘)
head = head + struct.pack(‘<H‘, format)
head = head + struct.pack(‘<H‘, channel)
head = head + struct.pack(‘<L‘, sampleRate)
head = head + struct.pack(‘<L‘, bytePerSec)
head = head + struct.pack(‘<H‘, blockAlign)
head = head + struct.pack(‘<L‘, bitPerSample)
head = head + bytearray(b‘\x64\x61\x74\x61‘)
head = head + struct.pack(‘<L‘, size-38)
return head;

def testMono():
count =1
sourceFile = open(sourceVoicePath,‘rb‘)
size = os.path.getsize(sourceVoicePath)
#vox源录音头部信息大小
sourceHeadSize = 178
dataSize = size - sourceHeadSize
while count<132:
changeHeadVoicePath = inDir + str(count) + ‘.wav‘
resultVoicePath = outDir + str(count) + ‘.wav‘
changeHeadVoiceFile = open(changeHeadVoicePath,‘wb‘)
changeHeadVoiceFile.write(outVoiceHead(dataSize + 46 - 8,count,1,8000,8000,4,8))
sourceFile.seek(sourceHeadSize)
changeHeadVoiceFile.write(sourceFile.read())
ffmStr = ‘ffmpeg -y -i ‘ + changeHeadVoicePath +‘ -ar 8000 ‘ + resultVoicePath
os.system(ffmStr)
count = count + 1
changeHeadVoiceFile.close()
if os.path.exists(changeHeadVoicePath):
os.remove(changeHeadVoicePath)

    if os.path.getsize(resultVoicePath)<10240:
        os.remove(resultVoicePath)
sourceFile.close()

def testStereo():
count =1
sourceFile = open(sourceVoicePath,‘rb‘)
size = os.path.getsize(sourceVoicePath)
#vox源录音头部信息大小
sourceHeadSize = 32
dataSize = size - sourceHeadSize
while count<132:
leftPath = inDir + str(count) + ‘left.wav‘
rightPath = inDir + str(count) + ‘right.wav‘
resultVoicePath = outDir + str(count) + ‘.wav‘
leftPathVoiceFile = open(leftPath,‘wb‘)
rightPathVoiceFile = open(rightPath,‘wb‘)
leftPathVoiceFile.write(outVoiceHead(dataSize//2 + 46 - 8,count,1,8000,8000,2,8))
rightPathVoiceFile.write(outVoiceHead(dataSize//2 + 46 - 8,count,1,8000,8000,2,8))
sourceFile.seek(sourceHeadSize)
leftPathVoiceFile.write(sourceFile.read(dataSize//2))
sourceFile.seek(sourceHeadSize+dataSize//2)
rightPathVoiceFile.write(sourceFile.read(dataSize//2))
#ffmpeg -v error -y -i D:\in\suiyi\1600003432800055-n0-alaw.wav -i D:\in\suiyi\1600003432800055-n1-alaw.wav -filter_complex amix=inputs=2:duration=longest:dropout_transition=2 -ac 1 -ar 8000 D:\in\suiyi\dc.wav
ffmStr = ‘ffmpeg -v error -y -i ‘ + leftPath + ‘ -i ‘ + rightPath + ‘ -filter_complex amix=inputs=2:duration=longest:dropout_transition=2 -ac 1 -ar 8000 ‘ + resultVoicePath
os.system(ffmStr)
count = count + 1
leftPathVoiceFile.close()
rightPathVoiceFile.close()
if os.path.exists(leftPath):
os.remove(leftPath)

    if os.path.exists(rightPath):
        os.remove(rightPath)

    if os.path.getsize(resultVoicePath)<10240:
        os.remove(resultVoicePath)
sourceFile.close()

#选择测试函数
testMono()

以上是关于wave音频转解码测试python脚本的主要内容,如果未能解决你的问题,请参考以下文章

ffmpeg 获取音频文件PCM切片

如何使用python实现wave音频文件回放

Python绘制wav文件音频图(静态)[matplotlib/wave]

WAV 文件 (.wav) 和 WAVE 音频文件 (.wave) 有啥区别?

Python中的音频

Python 从零开始制作自己的声音 - wave模块读写wav文件详解