Packet#
- class torchcodec.decoders.Packet(handle: Tensor, stream_index: int, *, generation: int = 0)[source]#
One compressed packet of one stream.
A
Demuxercreates them, and aVideoPacketDecoderor anAudioPacketDecoderconsumes them. You can also build one from compressed data that doesn’t come from aDemuxer, withfrom_bytes()orfrom_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:- 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.
Noneif unknown.
- property duration: int#
How long the packet lasts, in units of the stream’s time base. 0 if unknown.
- 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
datais 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
Demuxeror FFmpeg’s produces.pts (int, optional) – The pts, in units of the stream’s time base (
StreamMetadata.time_base). The decoded frames’RawFrame.ptscome 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:
- 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 fordata.- Parameters:
data (torch.Tensor) – The compressed data of one packet, as a contiguous 1D uint8 CPU tensor. It is copied. Each packet must contain exactly one access unit, which is what a demuxer such as
Demuxeror FFmpeg’s produces.pts (int, optional) – See
from_bytes().dts (int, optional) – See
from_bytes().duration (int, optional) – See
from_bytes().flags (int, optional) – See
from_bytes().side_data (dict, optional) – See
from_bytes().stream_index (int, optional) – See
from_bytes().
- Returns:
The packet.
- Return type:
- property pts: int | None#
The pts, in units of the stream’s time base (
StreamMetadata.time_base).Noneif unknown.