Rate this Page
★ ★ ★ ★ ★

VideoPacketDecoder#

class torchcodec.decoders.VideoPacketDecoder(codec_parameters: VideoCodecParameters, device: str | device | None = None, *, num_ffmpeg_threads: int = 1)[source]#

Decodes the compressed Packets of one video stream into RawFrames.

This is a low-level API: for straightforward decoding, use VideoDecoder instead.

Build it from the VideoCodecParameters of the stream to decode, then feed it one packet at a time, and drain it at the end:

demuxer = Demuxer("video.mp4")
decoder = VideoPacketDecoder(demuxer.streams[0].codec_parameters)

for packet in demuxer:
    for raw_frame in decoder.decode(packet):
        ...
for raw_frame in decoder.drain():
    ...

The codec parameters can also be pickled, to decode in another process than the demuxer, or built by hand, to decode packets that don’t come from a Demuxer (see Decoding packets that don’t come from a Demuxer). Frames come out on the device the decoder was built for.

It is stateful. It holds the codec’s reference-frame buffer, so it expects the packets of its own stream, in the order the demuxer produced them.

Parameters:
  • codec_parameters (VideoCodecParameters) – The parameters of the stream to decode, e.g. VideoStream.codec_parameters.

  • device (str or torch.device, optional) – The device to decode on (cpu or CUDA). If None (default), the current default device is used (see torch.set_default_device). With codec parameters you built yourself, CUDA needs some fields to be set: see VideoCodecParameters.

  • num_ffmpeg_threads (int, optional) – The number of threads to use for CPU decoding. This has no effect when decoding on GPU. Use 1 for single-threaded decoding, which may be best if you are decoding multiple streams in parallel. Use a higher number for multi-threaded decoding, which is best for a single stream. Passing 0 lets FFmpeg decide on the number of threads. Default: 1.

Examples using VideoPacketDecoder:

Build your own decoding pipeline

Build your own decoding pipeline

Low-level APIs and CUDA streams synchronization

Low-level APIs and CUDA streams synchronization

Decoding packets that don’t come from a Demuxer

Decoding packets that don't come from a Demuxer

Multi-threaded decoding pipelines

Multi-threaded decoding pipelines

Raw frames and raw audio samples

Raw frames and raw audio samples
decode(packet: Packet) → list[RawFrame][source]#

Send one Packet to the codec and return the RawFrames that are ready.

This can return zero, one, or more than one RawFrame. What comes back is not the decoding of the packet you just passed: a codec that is buffering B-frames, or still priming itself, will emit what it owes you on a later call.

Parameters:

packet (Packet) – A packet of this decoder’s own stream.

Returns:

The possibly empty list of RawFrames that the codec has ready, in presentation order.

Raises:

RuntimeError – If this decoder has been drained, or if the demuxer seeked without it being reset() afterwards.

drain() → list[RawFrame][source]#

Tell the codec the stream has ended, and return the RawFrames it was still holding.

Skipping this loses the tail of the stream. A drained decoder refuses any further packet; reset() makes it usable again.

Returns:

The possibly empty list of RawFrames that the codec was still holding, in presentation order.

reset() → None#

Drop the codec’s buffered state and start over.

Needed after a Demuxer.seek(), and after drain().