Shortcuts

SimpleVideoDecoder

class torchcodec.decoders.SimpleVideoDecoder(source: Union[str, Path, bytes, Tensor], dimension_order: Literal['NCHW', 'NHWC'] = 'NCHW')[source]

A single-stream video decoder.

If the video contains multiple video streams, the best stream is used. This decoder always performs a scan of the video.

Parameters:
  • source (str, Pathlib.path, torch.Tensor, or bytes) –

    The source of the video.

    • If str or Pathlib.path: a path to a local video file.

    • If bytes object or torch.Tensor: the raw encoded video data.

  • dimension_order (str, optional) –

    The dimension order of the decoded frames. This can be either “NCHW” (default) or “NHWC”, where N is the batch size, C is the number of channels, H is the height, and W is the width of the frames.

    Note

    Frames are natively decoded in NHWC format by the underlying FFmpeg implementation. Converting those into NCHW format is a cheap no-copy operation that allows these frames to be transformed using the torchvision transforms.

Variables:

metadata (VideoStreamMetadata) – Metadata of the video stream.

Examples using SimpleVideoDecoder:

Decoding a video with SimpleVideoDecoder

Decoding a video with SimpleVideoDecoder
__getitem__(key: Union[int, slice]) Tensor[source]

Return frame or frames as tensors, at the given index or range.

Parameters:

key (int or slice) – The index or range of frame(s) to retrieve.

Returns:

The frame or frames at the given index or range.

Return type:

torch.Tensor

get_frame_at(index: int) Frame[source]

Return a single frame at the given index.

Parameters:

index (int) – The index of the frame to retrieve.

Returns:

The frame at the given index.

Return type:

Frame

get_frame_displayed_at(seconds: float) Frame[source]

Return a single frame displayed at the given timestamp in seconds.

Parameters:

seconds (float) – The time stamp in seconds when the frame is displayed.

Returns:

The frame that is displayed at seconds.

Return type:

Frame

get_frames_at(start: int, stop: int, step: int = 1) FrameBatch[source]

Return multiple frames at the given index range.

Frames are in [start, stop).

Parameters:
  • start (int) – Index of the first frame to retrieve.

  • stop (int) – End of indexing range (exclusive, as per Python conventions).

  • step (int, optional) – Step size between frames. Default: 1.

Returns:

The frames within the specified range.

Return type:

FrameBatch

Docs

Access comprehensive developer documentation for PyTorch

View Docs

Tutorials

Get in-depth tutorials for beginners and advanced developers

View Tutorials

Resources

Find development resources and get your questions answered

View Resources