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.
- Packet: A JPEG image file (compressed, efficient to store)
- Sample: Raw pixel data in memory (uncompressed, ready to edit)
EncodedPacket
TheEncodedPacket class represents a chunk of compressed media data - either video or audio.
Structure
AnEncodedPacket contains:
packet.ts:47-87
Field descriptions
Field descriptions
- 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
- Delta packets
Key packets (also called I-frames or keyframes) can be decoded independently:
Converting to WebCodecs
Packets can be converted to WebCodecs API types:VideoSample
TheVideoSample class represents a raw, unencoded video frame with direct pixel data access.
Structure
AVideoSample 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
Understanding pixel formats
Understanding pixel formats
- 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
- Reading pixels
- Drawing to canvas
- Advanced drawing
Access raw pixel data from a sample:
Converting to VideoFrame
Convert a sample to WebCodecs VideoFrame:Resource management
AudioSample
TheAudioSample class represents raw, unencoded audio data with direct PCM access.
Structure
AnAudioSample 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
- Reading audio data
- Converting formats
Converting to AudioData
Resource management
When to use packets vs samples
Choose the right data type for your use case:- Use Packets
- Use Samples
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
✅ 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