Wave Audio transcoding Test Python script

Source: Internet
Author: User
Tags python script

#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 + Bytea Rray (b ' \x12\x00\x00\x00 ')
Head = head + struct.pack ('

Head = head + struct.pack ('

Head = head + struct.pack (' <l ', bytepersec)
Head = head + Struc T.pack ('

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:dropo Ut_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 Audio transcoding Test Python script

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.