You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用Python pymp4检测MP4 Box类型时遇‘str does not have type’错误求助

解决pymp4检测Box类型时的‘str does not have type’错误

问题原因

你遇到的错误是因为Box.parse_stream返回的迭代器中,混合了Box对象和字符串类型的元素——字符串没有type属性,直接访问就会触发AttributeError。另外部分pymp4版本中,Box对象的type属性是字符串而非bytes,也可能导致匹配失败。

修复方案

方案1:先过滤出有type属性的Box对象

在遍历的时候先检查元素是否具备type属性,再进行类型判断:

import io
from pymp4 import Box

boxes = Box.parse_stream(io.BytesIO(data))
for box in boxes:
    # 仅处理有type属性的Box实例
    if hasattr(box, 'type'):
        # 兼容bytes和str两种类型的type值
        if box.type in (b"senc", "senc"):
            print("found senc box")

方案2:用try-except跳过错误元素

通过捕获AttributeError,直接跳过不具备type属性的元素:

import io
from pymp4 import Box

boxes = Box.parse_stream(io.BytesIO(data))
for box in boxes:
    try:
        if box.type == b"senc":
            print("found senc box")
    except AttributeError:
        # 跳过无type属性的元素
        continue

额外调试建议

如果不确定box的类型和属性,可以在循环中加入调试代码,打印元素的类型和可用属性:

for box in boxes:
    print(f"元素类型: {type(box)}")
    print(f"可用属性: {dir(box)}")
    print("---")

根据输出结果调整判断逻辑,确保和pymp4实际返回的对象结构匹配。

内容的提问来源于stack exchange,提问作者Dude2019

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.23 12:33:13