如下所示:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
|
'''''
add Head Infomation for pcm file
'''
import sys
import struct
import os
__author__ = 'bob_hu, hewitt924@gmail.com'
__date__ = 'Dec 19,2011'
__update__ = 'Dec 19,2011'
def geneHeadInfo(sampleRate,bits,sampleNum):
'''''
生成头信息,需要采样率,每个采样的位数,和整个wav的采样的字节数
'''
rHeadInfo = '\x52\x49\x46\x46'
fileLength = struct.pack( 'i' ,sampleNum + 36 )
rHeadInfo + = fileLength
rHeadInfo + = '\x57\x41\x56\x45\x66\x6D\x74\x20\x10\x00\x00\x00\x01\x00\x01\x00'
rHeadInfo + = struct.pack( 'i' ,sampleRate)
rHeadInfo + = struct.pack( 'i' ,sampleRate * bits / 8 )
rHeadInfo + = '\x02\x00'
rHeadInfo + = struct.pack( 'H' ,bits)
rHeadInfo + = '\x64\x61\x74\x61'
rHeadInfo + = struct.pack( 'i' ,sampleNum)
return rHeadInfo
if __name__ = = '__main__' :
if len (sys.argv) ! = 5 :
print "usage: python %s inFile sampleRate bits outFile" % sys.argv[ 0 ]
sys.exit( 1 )
fout = open (sys.argv[ 4 ], 'wb' ) #用二进制的写入模式
#fout.write(struct.pack('4s','\x66\x6D\x74\x20'))
#写入一个长度为4的串,这个串的二进制内容为 66 6D 74 20
#Riff_flag,afd,fad,afdd, = struct.unpack('4c',fin.read(4))
#读入四个字节,每一个都解析成一个字母
#open(sys.argv[4],'wb').write(struct.pack('4s','fmt '))
#将字符串解析成二进制后再写入
#open(sys.argv[4],'wb').write('\x3C\x9C\x00\x00\x57')
#直接写入二进制内容:3C 9C 00 00 57
#fout.write(struct.pack('i',6000)) #写入6000的二进制形式
#check whether inFile has head-Info
fin = open (sys.argv[ 1 ], 'rb' )
Riff_flag, = struct.unpack( '4s' ,fin.read( 4 ))
if Riff_flag = = 'RIFF' :
print "%s 有头信息" % sys.argv[ 1 ]
fin.close()
sys.exit( 0 )
else :
print "%s 没有头信息" % sys.argv[ 1 ]
fin.close()
#采样率
sampleRate = int (sys.argv[ 2 ])
#bit位
bits = int (sys.argv[ 3 ])
fin = open (sys.argv[ 1 ], 'rb' )
startPos = fin.tell()
fin.seek( 0 ,os.SEEK_END)
endPos = fin.tell()
sampleNum = (endPos - startPos)
print sampleNum
headInfo = geneHeadInfo(sampleRate,bits,sampleNum)
fout.write(headInfo)
fin.seek(os.SEEK_SET)
fout.write(fin.read())
fin.close()
fout.close()
|
以上这篇python pcm音频添加头转成Wav格式文件的方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持服务器之家。
原文链接:https://blog.csdn.net/benhuo931115/article/details/53762556