tinytag icon indicating copy to clipboard operation
tinytag copied to clipboard

Getting OPUS file tags fails with `Invalid OGG header`

Open mystieneko opened this issue 3 months ago • 0 comments

Describe the bug Getting file tags fails on seemingly random OPUS files with tinytag.tinytag.ParseError: Invalid OGG header Most of the opus files i tried work perfectly fine, so there must be something wrong with this one specifically

To Reproduce Steps to reproduce the behavior:

  1. Try to get file tags: tag = TinyTag.get("1. EminemMusic - Eminem - Renaissance [Official Audio].opus")
  2. See error

I dug into the code a bit and found this condition in tinytag.py which causes the error:

    # lines 1542-1553
    def _parse_pages(self, fh: BinaryIO) -> Iterator[bytearray]:
        # for the spec, see: https://wiki.xiph.org/Ogg
        packet_data = bytearray()
        current_serial = None
        last_granule_pos = 0
        last_audio_size = 0
        header_len = 27
        page_header = fh.read(header_len)  # read ogg page header
        while len(page_header) == header_len:
            version = page_header[4]
            if page_header[:4] != b'OggS' or version != 0: # <- this condition fails
                raise ParseError('Invalid OGG header')
            # ...

However, none of the error conditions are met:

>>> with open("1. EminemMusic - Eminem - Renaissance [Official Audio].opus", "rb") as f:
...     page_header = f.read(27)
...     
>>> version = page_header[4]
>>> page_header[:4] != b'OggS'
False
>>> version != 0
False

Here's the full code that reproduces the error:

>>> from tinytag import TinyTag
>>> with open("1. EminemMusic - Eminem - Renaissance [Official Audio].opus", "rb") as f:
...     page_header = f.read(27)
...     
>>> version = page_header[4]
>>> page_header[:4] != b'OggS'
False
>>> version != 0
False
>>> tag = TinyTag.get("1. EminemMusic - Eminem - Renaissance [Official Audio].opus")
Traceback (most recent call last):
  File "/home/mystie/dev/filekatze/venv/lib64/python3.14/site-packages/tinytag/tinytag.py", line 152, in get
    tag._load(tags=tags, duration=duration, image=image)
    ~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/mystie/dev/filekatze/venv/lib64/python3.14/site-packages/tinytag/tinytag.py", line 266, in _load
    self._parse_tag(self._filehandler)
    ~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^
  File "/home/mystie/dev/filekatze/venv/lib64/python3.14/site-packages/tinytag/tinytag.py", line 1352, in _parse_tag
    for packet in self._parse_pages(fh):
                  ~~~~~~~~~~~~~~~~~^^^^
  File "/home/mystie/dev/filekatze/venv/lib64/python3.14/site-packages/tinytag/tinytag.py", line 1480, in _parse_pages
    raise ParseError('Invalid OGG header')
tinytag.tinytag.ParseError: Invalid OGG header

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "<python-input-5>", line 1, in <module>
    tag = TinyTag.get("1. EminemMusic - Eminem - Renaissance [Official Audio].opus")
  File "/home/mystie/dev/filekatze/venv/lib64/python3.14/site-packages/tinytag/tinytag.py", line 154, in get
    raise ParseError(exc) from exc
tinytag.tinytag.ParseError: Invalid OGG header
>>> page_header
b'OggS\x00\x02\x00\x00\x00\x00\x00\x00\x00\x00\xf1\x10\xb1\xf5\x00\x00\x00\x00rH\x9d\x1a\x01'
>>> len(page_header) == 27
True

Expected behavior The file should be read correctly

Sample file https://0x0.st/KtCF.opus mirror: https://files.mystie.dev/f/7ed5aae4c49a4a5783eb/

mystieneko avatar Dec 05 '25 20:12 mystieneko