从python3中的.wav文件中删除文件头

时间:2019-03-29 15:03:54

标签: python audio wav wave file-header

我一直在做一个使用RSA算法加密.wav文件的项目,为此,我需要删除文件头以正确读取文件。我需要声音数据作为一个numpy数组。现在,我已经在网上搜索了此信息,但不了解文件头是什么以及如何在python3中删除它。期待提出建议。谢谢。

1 个答案:

答案 0 :(得分:1)

binarySound = bytearray()
binaryHeader = bytearray()

with open("a2002011001-e02.wav",'rb') as f:
        binaryHeader = f.read(44)
        binarySound = f.read()

这应该是您要寻找的。这会将前44个字节(应该是标头)读入binaryHeader变量,其余声音数据读入binarySound变量。

要恢复音乐文件,您只需将这两个文件重新添加在一起

song = bytearray()

with open("header.bin","rb") as h:
        song = h.read()
        with open("data.bin","rb") as d:
                song += d.read()

with open("new.wav","wb") as f:
        f.write(song)

编辑:要在numpy数组中包含对OP的编辑,

import numpy

binarySound = {}
binaryHeader = {}

song = {}

with open("a2002011001-e02.wav",'rb') as f:
        buffer = f.read(44)
        binaryHeader = numpy.frombuffer(buffer,dtype=numpy.uint8)
        buffer = f.read()
        binarySound = numpy.frombuffer(buffer,dtype=numpy.uint8)

with open("header.bin","wb") as f:
        f.write(binaryHeader)

with open("data.bin","wb") as f:
        f.write(binarySound)

with open("header.bin","rb") as h:
        song = h.read()
        with open("data.bin","rb") as d:
                song += d.read()

with open("new.wav","wb") as f:
        song = numpy.array(song)
        f.write(song.tobytes())