WAV文件格式

起始地址

占用空间

本地址数字的含义

00H

4byte

“RIFF”,资源交换文件标志。

04H

4byte

从下一个地址开始到文件尾的总字节数。高位字节在后面,加上之前的8byte刚好是文件大小。

08H

4byte

“WAVE”,代表wav文件格式。

0CH

4byte

“fmt” ,波形格式标志

10H

4byte

PCMWAVEFORMAT的大小

14H

2byte

是否是PCMWAVEFORMAT

16H

2byte

声道,1为单声道,2为双声道。

18H

4byte

采样频率

1CH

4byte

Byte速率=采样频率*音频通道数*每次采样得到的样本位数/8

20H

2byte

块长度=通道数*每次采样得到的样本位数/8

22H

2byte

PCM宽

24H

4byte

“data”标志

28H

4byte

此后的data大小,再加上2CH就正好是整个文件的大小。

2CH

不定

正主。



用Python写了个简单的功能,把WAVE文件HEADER每个元素分别显示出来:

# -*- coding: cp936 -*-
# 解析WAV格式的文件-Leo
import os
import binascii

# 定义一个字典用来存储WAV文件的HEADER
WAV_HEADER = {
	"flag_RIFF":		0,
	"length_File":		0,
	"flag_WAVE":		0,
	"flag_FMT":			0,
	"sizeof_PCMWAVEFORMAT":		0,
	"PCMWAVEFORMAT":	0,
	"Channel":			0,
	"Frequency":		0,
	"Rate":				0,
	"length_DATA":		0,
	"wide_PCM":			0,
	"flag_DATA":		0,
	"sizeof_DATA":		0
}

file_path = raw_input("input WAV file path for analyse:")

with open(file_path, 'rb') as input_file:

    flag_RIFF = input_file.read(4)

    input_file.seek(4,1)

    flag_WAVE = input_file.read(4)

# 判断文件是否是wav文件
    if cmp(flag_RIFF, 'RIFF') or cmp(flag_WAVE, 'WAVE'):

        print "input file is not a WAV file!"

        exit(0)

    input_file.seek(0,0)
    
    WAV_HEADER["flag_RIFF"] = input_file.read(4).decode()

    print "flag_RIFF:", WAV_HEADER["flag_RIFF"]

    WAV_HEADER["length_File"] = input_file.read(4)

    print "length_File:", binascii.hexlify(WAV_HEADER["length_File"])

    WAV_HEADER["flag_WAVE"] = input_file.read(4).decode()

    print "flag_WAVE:", WAV_HEADER["flag_WAVE"]

    WAV_HEADER["flag_FMT"] = input_file.read(4).decode()

    print "flag_FMT:", WAV_HEADER["flag_FMT"]

    WAV_HEADER["sizeof_PCMWAVEFORMAT"] = input_file.read(4)

    print "sizeof_PCMWAVEFORMAT:", binascii.hexlify(WAV_HEADER["sizeof_PCMWAVEFORMAT"])

    WAV_HEADER["PCMWAVEFORMAT"] = input_file.read(2)

    print "PCMWAVEFORMAT:", binascii.hexlify(WAV_HEADER["PCMWAVEFORMAT"])

    WAV_HEADER["Channel"] = input_file.read(2)

    print "Channel:", binascii.hexlify(WAV_HEADER["Channel"])

    WAV_HEADER["Frequency"] = input_file.read(4)

    print "Frequency:", binascii.hexlify(WAV_HEADER["Frequency"])

    WAV_HEADER["Rate"] = input_file.read(4)

    print "Rate:", binascii.hexlify(WAV_HEADER["Rate"])

    WAV_HEADER["length_DATA"] = input_file.read(2)

    print "length_DATA:", binascii.hexlify(WAV_HEADER["length_DATA"])

    WAV_HEADER["wide_PCM"] = input_file.read(2)

    print "wide_PCM:", binascii.hexlify(WAV_HEADER["wide_PCM"])

    WAV_HEADER["flag_DATA"] = input_file.read(4)

    print "flag_DATA:", binascii.hexlify(WAV_HEADER["flag_DATA"])

    WAV_HEADER["sizeof_DATA"] = input_file.read(4)

    print "sizeof_DATA:", binascii.hexlify(WAV_HEADER["sizeof_DATA"])

    os.system('pause')


你可能感兴趣的:(windows平台)