有没有一种方法可以从音频文件中删除/编辑名为“tag”的元数据条目而不安装任何其他内容?

2024-09-28 01:25:50 发布

您现在位置:Python中文网/ 问答频道 /正文

我想删除音频文件中的一段数据,例如iTunes用来在歌曲列表中显示的数据。它还使用这段数据对它们进行排序。我相信这段数据是文件元数据的一部分,称为tag。你知道吗

我想用一个自编的Python脚本来实现这一点。我不想手动或通过更改计算机上的任何内容来执行此操作。你知道吗

我发现使用stat模块没有办法做到这一点。我正在使用Python 2.7.10。你知道吗

我现在对两种文件格式感兴趣:mp3和mp4。我在这里读了一些关于mp3格式的书:https://en.wikipedia.org/wiki/ID3。上面写着:“ID3v1标记占用128字节,从文件末尾的字符串标记128字节开始。”。你知道吗

多亏了马蒂诺,我改进了剧本:

import os
import sys

# had to use a different file which really contains a tag
vPath = "/Users/klausdorsch/Desktop/wegdamit/Armada.01.mp3" 
with open(vPath, "r+b") as vFile:  # Open existing file for reading and writing.
    vFile.seek(-128, os.SEEK_END)  # Seek 128 bytes before end of file.
    # Verify ID3v1 tag header starts there.
    tag = vFile.read(3)
    if tag == 'TAG':
        print 'found a tag!'
        # had to add brackets in the line below to avoid TypeError: 
        # unsupported operand type(s) for -: 'str' and 'int'
        vFile.write('\x00' * (128-3))  # Zero-out tag's contents.

sys.exit()

当我第一次运行它时,它会输出:“找到一个标记!”,因此找到了一个标记。当我第二次运行它时,没有这样的输出,所以标签应该被删除。但当我用iTunes打开文件时,标签的内容仍然存在。你知道吗


Tags: 文件to数据标记import内容字节os
1条回答
网友
1楼 · 发布于 2024-09-28 01:25:50

我认为主要的问题是你打开文件的方式会首先截断它。你所使用的方式和位置似乎也不正确——它只需要做一次,因为对文件的读写会自动推进其内部当前位置。你知道吗

试着这样做,这样文件的其余部分就完好无损了,如果找到ID3v1标记的内容,那么只会将其归零。你知道吗

import os
import sys

vPath = "/Users/klausdorsch/Desktop/wegdamit/08ICanMakeYouAManReprise.mp3"
with open(vPath, "r+b") as vFile:  # Open existing file for reading and writing.
    vFile.seek(-128, os.SEEK_END)  # Seek 128 bytes before end of file.
    # Verify ID3v1 tag header starts there.
    tag = vFile.read(3)
    if tag == 'TAG':
        vFile.write('\x00' * 128-3)  # Zero-out tag's contents.

sys.exit()

相关问题 更多 >

    热门问题