Rate this Page
★ ★ ★ ★ ★

Packet#

class torchcodec.decoders.Packet(handle: Tensor, stream_index: int, *, generation: int = 0)[source]#

One compressed packet of one stream.

A Demuxer creates them, and a VideoPacketDecoder or an AudioPacketDecoder consumes them. You can also build one from compressed data that doesn’t come from a Demuxer, with from_bytes() or from_tensor().

Packets can be pickled, e.g. to be decoded in another process. The pickles are only guaranteed to be readable by the same version of TorchCodec.

Examples using Packet:

Build your own decoding pipeline

Build your own decoding pipeline

Decoding packets that don’t come from a Demuxer

Decoding packets that don't come from a Demuxer
property data: Tensor#

The compressed data, as a 1D uint8 CPU tensor.

It is a view on the packet’s data: writing to it modifies the packet, so .clone() it first.

property dts: int | None#

The decoding timestamp, in units of the stream’s time base. None if unknown.

property duration: int#

How long the packet lasts, in units of the stream’s time base. 0 if unknown.

property flags: int#

FFmpeg’s AV_PKT_FLAG_* bits, e.g. flags & 1 is set for keyframes.

classmethod from_bytes(data: bytes | bytearray | memoryview, *, pts: int | None = None, dts: int | None = None, duration: int = 0, flags: int = 0, side_data: dict[str, bytes] | None = None, stream_index: int = 0) → Packet[source]#

Build a packet from compressed data, e.g. read from a database or received from the network.

Only data is required: most codecs need nothing else to decode it.

Parameters:
  • data (bytes, bytearray or memoryview) – The compressed data of one packet. It is copied. Each packet must contain exactly one access unit, which is what a demuxer such as Demuxer or FFmpeg’s produces.

  • pts (int, optional) – The pts, in units of the stream’s time base (StreamMetadata.time_base). The decoded frames’ RawFrame.pts come from it. None (default) if unknown.

  • dts (int, optional) – The decoding timestamp, in units of the stream’s time base. None (default) if unknown.

  • duration (int, optional) – How long the packet lasts, in units of the stream’s time base. Default: 0, for unknown.

  • flags (int, optional) – FFmpeg’s AV_PKT_FLAG_* bits, e.g. 1 (AV_PKT_FLAG_KEY) for a keyframe. AV_PKT_FLAG_TRUSTED (8) isn’t allowed. Default: 0.

  • side_data (dict, optional) – Extra data about the packet, keyed by FFmpeg’s names for its types, e.g. "Skip Samples".

  • stream_index (int, optional) – See stream_index. Default: 0.

Returns:

The packet.

Return type:

Packet

classmethod from_tensor(data: Tensor, *, pts: int | None = None, dts: int | None = None, duration: int = 0, flags: int = 0, side_data: dict[str, bytes] | None = None, stream_index: int = 0) → Packet[source]#

Build a packet from compressed data in a tensor.

Same as from_bytes(), except for data.

Parameters:
Returns:

The packet.

Return type:

Packet

property pts: int | None#

The pts, in units of the stream’s time base (StreamMetadata.time_base). None if unknown.

property side_data: dict[str, bytes]#

Extra data about the packet, keyed by FFmpeg’s names for its types, e.g. "Skip Samples".

stream_index: int#

The index of the stream this packet belongs to, absolute across all media types. When a demuxer follows more than one stream, this is what routes each packet to the right decoder.