Skip to main content
In Mediabunny, media data exists in two fundamental forms: packets (compressed data) and samples (raw data). Understanding when to use each is essential for effective media processing.

The fundamental difference

Packets

Compressed, encoded media data as stored in files. Small, efficient, but not directly usable.

Samples

Raw, decoded media data ready for processing. Large, uncompressed, directly manipulable.
Think of it like this:
  • Packet: A JPEG image file (compressed, efficient to store)
  • Sample: Raw pixel data in memory (uncompressed, ready to edit)

EncodedPacket

The EncodedPacket class represents a chunk of compressed media data - either video or audio.

Structure

An EncodedPacket contains:
packet.ts:47-87
  • data: The actual compressed bytes in codec-specific format
  • type: 'key' (can decode independently) or 'delta' (depends on previous frames)
  • timestamp: When this packet should be presented, in seconds
  • duration: How long this packet lasts, in seconds
  • sequenceNumber: Decode order (lower numbers decode first)
  • byteLength: Size of the data (useful for metadata-only packets)
  • sideData: Additional data like alpha channel information

Creating packets

You typically create packets from encoded data or WebCodecs API chunks:

Using packets

Packets are what you read from input files and write to output files:

Packet types

Key packets (also called I-frames or keyframes) can be decoded independently:
Key packets are larger but essential for random access and seeking in media files.

Converting to WebCodecs

Packets can be converted to WebCodecs API types:

VideoSample

The VideoSample class represents a raw, unencoded video frame with direct pixel data access.

Structure

A VideoSample provides:
sample.ts:171-210

Creating video samples

You can create video samples from various sources:

Pixel formats

Mediabunny supports 21 pixel formats:
sample.ts:86-121
  • I420: Most common format for video encoding (4:2:0 subsampling)
  • I420P10/P12: 10-bit and 12-bit variants for HDR content
  • I420A: I420 with alpha channel for transparency
  • RGBA/BGRA: Full-color formats with alpha, useful for graphics
  • RGBX/BGRX: Opaque RGB formats
  • NV12: Efficient format used by many hardware decoders

Working with video samples

Access raw pixel data from a sample:

Converting to VideoFrame

Convert a sample to WebCodecs VideoFrame:

Resource management

Video samples hold resources that must be explicitly freed:

AudioSample

The AudioSample class represents raw, unencoded audio data with direct PCM access.

Structure

An AudioSample provides:
sample.ts:1366-1391

Creating audio samples

Audio formats

Supported audio sample formats:
Planar formats store each channel separately, while non-planar formats interleave channels.

Working with audio samples

Converting to AudioData

Resource management

Like video samples, audio samples must be closed:

When to use packets vs samples

Choose the right data type for your use case:
Use EncodedPacket when you:✅ Copy/remux media without re-encoding
✅ Need efficient storage and transfer
✅ Don’t need to manipulate pixel/audio data
✅ Want to preserve original encoding quality

Performance considerations

Packets

  • Very fast (no encoding/decoding)
  • Low memory usage
  • Perfect for remuxing
  • Limited processing options

Samples

  • Slower (requires decode/encode)
  • High memory usage
  • Full pixel/audio access
  • Enables rich processing

Example: Remuxing (fast)

Example: Transcoding (slower)

Next steps

Input and Output

Learn how to read packets and samples from files

Encoding and Decoding

Convert between packets and samples