gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 01:05:15 24 4
gpt4 key购买 nike

我一直在做一个使用 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())

关于python - 从 python3 中的 .wav 文件中删除文件头,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55420292/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com