从 python3 中的 .wav 文件中删除文件头
Remove file header from a .wav file in python3
我一直在做一个使用 RSA 算法加密 .wav 文件的项目,为此,我需要删除文件头才能正确读取文件。我需要声音数据作为一个 numpy 数组。现在我在网上搜索了这个,但不明白什么是文件头以及如何在 python3 中删除它。期待建议。谢谢。
binarySound = bytearray()
binaryHeader = bytearray()
with open("a2002011001-e02.wav",'rb') as f:
binaryHeader = f.read(44)
binarySound = f.read()
这应该是您要查找的内容。这会将前 44 个字节(假设为 header)读入 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)
编辑:将编辑包含在 OP 中以满足 numpy 数组的需要:
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())
我一直在做一个使用 RSA 算法加密 .wav 文件的项目,为此,我需要删除文件头才能正确读取文件。我需要声音数据作为一个 numpy 数组。现在我在网上搜索了这个,但不明白什么是文件头以及如何在 python3 中删除它。期待建议。谢谢。
binarySound = bytearray()
binaryHeader = bytearray()
with open("a2002011001-e02.wav",'rb') as f:
binaryHeader = f.read(44)
binarySound = f.read()
这应该是您要查找的内容。这会将前 44 个字节(假设为 header)读入 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)
编辑:将编辑包含在 OP 中以满足 numpy 数组的需要:
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())