【发布时间】:2016-08-07 19:55:21
【问题描述】:
我正在尝试让 Kaitai Struct 解析 MP3 的 ID3v1 标签格式。根据standard,它是一个固定格式结构,位于某个偏移量处——但诀窍是这个偏移量不是从文件开头计算的,而是从文件末尾计算的。
这是标签的基本.ksy 轮廓,我认为它不应该真正改变:
meta:
id: id3v1
types:
id3v1_tag:
seq:
- id: magic
contents: 'TAG'
- id: title
size: 30
- id: artist
size: 30
- id: album
size: 30
- id: year
size: 4
- id: comment
size: 30
- id: genre
type: u1
这是我关于如何从 128 个字节到文件末尾读取它的幼稚想法:
instances:
tag:
pos: -128
type: id3v1_tag
我尝试使用一个简单的 Python 测试脚本:
#!/usr/bin/env python
from id3v1 import *
f = Id3v1.from_file('some_file_with_id3.mp3')
print(f.tag)
但是,它似乎将负数直接传递到 Python 的 File 对象 seek() 中,因此失败了:
Traceback(最近一次调用最后一次):文件“try-id3.py”,第 6 行,在 打印(f.id3v1_tag)文件“id3v1_1.py”,第 171 行,在 id3v1_tag 中 self._io.seek(-128) 文件“kaitaistruct.py”,第 29 行,在搜索中 self._io.seek(n) IOError: [Errno 22] 无效参数
在其他一些同样疯狂的想法之后,我找到了一种解决方法:我可以省略 .ksy 中的任何 pos 参数,然后手动寻找脚本中的正确位置:
f = Id3v1.from_file('some_file_with_id3.mp3')
f._io.seek(-128, 2)
print(f.tag.title)
这行得通,但感觉真的很 hackish :( 有没有更好的方法在 Kaitai Struct 和 Python 中做到这一点?
【问题讨论】:
标签: python id3 kaitai-struct