如何通过Python中的二进制文件操纵WAV文件的数据

发布于 2025-02-05 15:46:53 字数 626 浏览 4 评论 0原文

我正在尝试通过使用其二进制结构或其他用语更改其二进制结构来操纵WAV文件的数据(更改评论部分中放入URL的元数据)。这是我尝试这样做的尝试:

with open('test.wav', 'r+b') as f:
    b = f.read()
    l = b.find(b'\x00ICMT') + 1 # Find the position after ICMT
    v = b.find(b'\x00ICRD') - 1 # Find the position before ICRD
    f = bytearray(b) # Make f into a bytearray
    f[l:v] = b'URL: RandomURL' # Use l and v to get the section I want to rewrite (comment section)
  

在上面的代码中,我尝试将WAV文件转换为字节数组,然后在ICMT之后找到位置(元数据中的注释部分)以及ICMT部分结束时的位置(之前ICRD的部分)。然后,我使用此信息访问字节数组中的评论部分,并将其放入具有URL的字节中。但是,每当我尝试一下时,我实际上并没有更改WAV文件的内容。我的方法有什么问题,或者有一种更简单的方法来操纵WAV文件的二进制内容以获取我想要的东西?

I'm trying to manipulate a wav file's data (change the metadata to put in a url in the comment section) by using its binary structure, or in other terms change its binary structure. Here is my attempt at trying to do so:

with open('test.wav', 'r+b') as f:
    b = f.read()
    l = b.find(b'\x00ICMT') + 1 # Find the position after ICMT
    v = b.find(b'\x00ICRD') - 1 # Find the position before ICRD
    f = bytearray(b) # Make f into a bytearray
    f[l:v] = b'URL: RandomURL' # Use l and v to get the section I want to rewrite (comment section)
  

In the code above I'm trying to convert the wav file into a byte array and then find the position after ICMT (comment section in the metadata) and the position when ICMT's section ends (before ICRD's section). I then use this information to access the comment section within the byte array and make that into a byte that has the url. However, whenever I try this I don't actually change the contents of the wav file, it seems. What is wrong with my approach or is there an easier way to manipulate the binary contents of a wav file to get what I want?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文