1 min read

Python Chunk Module

The chunk module provides a parser for reading EA IFF 85 chunks. This format is used in Audio Interchange File Format (AIFF/AIFF-C) and Real Media File Format (RMFF).

Note: The chunk module is deprecated as of Python 3.11 and was removed in Python 3.13.

Importing the Module

import chunk

Reading Chunks

The chunk.Chunk class is used to read chunks from a file.

Basic Usage

You can read chunks from a file-like object.

import chunk
import io

# Create a dummy IFF chunk
# ID: 'TEST', Size: 12 bytes, Data: 'Hello World!'
data = b'TEST\x00\x00\x00\x0cHello World!'
file = io.BytesIO(data)

# Create a Chunk instance
ch = chunk.Chunk(file)

print(f"Chunk Name: {ch.getname()}") # Output: b'TEST'
print(f"Chunk Size: {ch.getsize()}") # Output: 12

# Read data
print(f"Data: {ch.read()}") # Output: b'Hello World!'

Methods

  • getname(): Returns the name (ID) of the chunk.
  • getsize(): Returns the size of the chunk.
  • read(size): Reads at most size bytes from the chunk. If size is omitted, reads until the end of the chunk.
  • skip(): Skips to the end of the chunk.
  • seek(pos, whence=0): Set the current position.
  • tell(): Return the current position.
  • close(): Closes the object.

programming/python/python