> ## Documentation Index
> Fetch the complete documentation index at: https://mintlify.com/Vanilagy/mediabunny/llms.txt
> Use this file to discover all available pages before exploring further.

# Packets

> Encoded media packets for compressed data

## EncodedPacket

Represents an encoded chunk of media. Used as an expressive wrapper around WebCodecs API's [`EncodedVideoChunk`](https://developer.mozilla.org/en-US/docs/Web/API/EncodedVideoChunk) and [`EncodedAudioChunk`](https://developer.mozilla.org/en-US/docs/Web/API/EncodedAudioChunk), but can also be used standalone.

### Constructor

```typescript theme={null}
new EncodedPacket(
  data: Uint8Array,
  type: PacketType,
  timestamp: number,
  duration: number,
  sequenceNumber?: number,
  byteLength?: number,
  sideData?: EncodedPacketSideData
)
```

<ParamField path="data" type="Uint8Array" required>
  The encoded data of this packet. For any given codec, this data must adhere to the format specified in the Mediabunny Codec Registry.
</ParamField>

<ParamField path="type" type="PacketType" required>
  The type of this packet: `'key'` or `'delta'`.
</ParamField>

<ParamField path="timestamp" type="number" required>
  The presentation timestamp of this packet in seconds. May be negative. Samples with negative end timestamps should not be presented.
</ParamField>

<ParamField path="duration" type="number" required>
  The duration of this packet in seconds.
</ParamField>

<ParamField path="sequenceNumber" type="number" default="-1">
  The sequence number indicates the decode order of the packets. Packet A must be decoded before packet B if A has a lower sequence number than B. If two packets have the same sequence number, they are the same packet. Negative sequence numbers mean the sequence number is undefined.
</ParamField>

<ParamField path="byteLength" type="number">
  The actual byte length of the data in this packet. This field is useful for metadata-only packets where the `data` field contains no bytes.
</ParamField>

<ParamField path="sideData" type="EncodedPacketSideData">
  Additional data carried with this packet.
</ParamField>

### Properties

<ResponseField name="data" type="Uint8Array">
  The encoded data of this packet.
</ResponseField>

<ResponseField name="type" type="PacketType">
  The type of this packet: `'key'` or `'delta'`.
</ResponseField>

<ResponseField name="timestamp" type="number">
  The presentation timestamp in seconds.
</ResponseField>

<ResponseField name="duration" type="number">
  The duration in seconds.
</ResponseField>

<ResponseField name="sequenceNumber" type="number">
  The sequence number for decode order.
</ResponseField>

<ResponseField name="byteLength" type="number">
  The actual byte length of the data.
</ResponseField>

<ResponseField name="sideData" type="EncodedPacketSideData">
  Additional data carried with this packet.
</ResponseField>

### Methods

#### toEncodedVideoChunk()

Converts this packet to an [`EncodedVideoChunk`](https://developer.mozilla.org/en-US/docs/Web/API/EncodedVideoChunk).

```typescript theme={null}
toEncodedVideoChunk(): EncodedVideoChunk
```

#### toEncodedAudioChunk()

Converts this packet to an [`EncodedAudioChunk`](https://developer.mozilla.org/en-US/docs/Web/API/EncodedAudioChunk).

```typescript theme={null}
toEncodedAudioChunk(): EncodedAudioChunk
```

#### toEncodedChunk()

Converts this packet to either an `EncodedVideoChunk` or `EncodedAudioChunk`.

```typescript theme={null}
toEncodedChunk(kind: 'video' | 'audio'): EncodedVideoChunk | EncodedAudioChunk
```

## PacketType

The type of a packet. Key packets can be decoded without previous packets, while delta packets depend on previous packets.

```typescript theme={null}
type PacketType = 'key' | 'delta'
```

<ResponseField name="key" type="string">
  Key (keyframe) packets can be decoded independently without previous packets.
</ResponseField>

<ResponseField name="delta" type="string">
  Delta packets depend on previous packets for decoding.
</ResponseField>

## EncodedPacketSideData

Holds additional data accompanying an `EncodedPacket`.

```typescript theme={null}
type EncodedPacketSideData = {
  alpha?: Uint8Array;
  alphaByteLength?: number;
}
```

<ResponseField name="alpha" type="Uint8Array">
  An encoded alpha frame, encoded with the same codec as the packet. Typically used for transparent videos, where the alpha information is stored separately from the color information.
</ResponseField>

<ResponseField name="alphaByteLength" type="number">
  The actual byte length of the alpha data. This field is useful for metadata-only packets where the `alpha` field contains no bytes.
</ResponseField>

## Example

```typescript theme={null}
import { EncodedPacket } from 'mediabunny';

// Create a key packet
const packet = new EncodedPacket(
  encodedData,        // Uint8Array of encoded data
  'key',              // Packet type
  0.0,                // Timestamp in seconds
  0.033,              // Duration in seconds (e.g., 30fps)
  0,                  // Sequence number
  encodedData.byteLength
);

// Convert to WebCodecs format
const videoChunk = packet.toEncodedVideoChunk();
const audioChunk = packet.toEncodedAudioChunk();

// Check packet properties
console.log(`Packet type: ${packet.type}`);
console.log(`Timestamp: ${packet.timestamp}s`);
console.log(`Duration: ${packet.duration}s`);
console.log(`Size: ${packet.byteLength} bytes`);
```

## See also

* [Samples](/api/samples) - Raw unencoded media data
* [EncodedPacketSource](/api/media-sources/video-sources#encodedvideopacketsource) - Add pre-encoded packets to output
* [EncodedPacketSink](/api/media-sinks/audio-sinks#encodedpacketsink) - Extract encoded packets from input
