1. 程式人生 > >wave音頻轉解碼測試python腳本

wave音頻轉解碼測試python腳本

size input mat python source dsi seek file format)

#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腳本