TorchCodec

repository·main·Indexed 22 days ago

https://github.com/meta-pytorch/torchcodec

A Python library for decoding video and audio data directly into PyTorch tensors on CPU or CUDA GPU, and performing encoding on CPU. It abstracts FFmpeg to provide a Pythonic interface for ML workflows, featuring VideoDecoder, AudioDecoder, and various encoders for video, audio, JPEG, and PNG. It includes utilities for sampling video clips, applying decoding transforms like Resize and CenterCrop, and managing hardware acceleration via NVDEC.

Tokens
12.2K
Snippets
42
Records
59
Agent score
77%

What's inside torchcodec

  1. Overview of TorchCodec

    main

    TorchCodec is a Python library designed to decode video and audio data directly into PyTorch tensors on both CPU and CUDA GPU. It also provides encoding capabilities for audio, video, and images.

    Key features include:

    • PyTorch Integration: Returns data as PyTorch tensors, making it ready for use in PyTorch transforms or ML model training.
    • FFmpeg Backend: Leverages the FFmpeg version installed on your system to handle complex decoding/encoding tasks while providing a simplified, Pythonic API.
    • Hardware Acceleration: Supports decoding and encoding on both CPU and CUDA GPUs.
  2. Use torchcodec.transforms for video decoding and augmentation

    main

    The torchcodec.transforms module provides tools for applying transformations during the video decoding process. These transforms can be used to perform operations like resizing, cropping, or custom decoding logic directly as part of the decoding pipeline.

    Key transform types include:

    • DecoderTransform: A base class or interface for defining custom decoding logic.
    • CenterCrop: Crops the center portion of the video frames.
    • RandomCrop: Applies a random crop to the video frames.
    • Resize: Resizes the video frames to specified dimensions.

    For a full walkthrough of how to apply these transforms during decoding, refer to the decoding transforms tutorial.

  3. Use torchcodec.samplers for video clip extraction

    main

    The torchcodec.samplers module provides utility functions to extract specific segments or frames from video files using regular or random sampling patterns. You can sample based on either frame indices or timestamps.

    Available sampling functions:

    • clips_at_regular_indices: Extracts clips at fixed, regular frame index intervals.
    • clips_at_random_indices: Extracts clips at randomly selected frame indices.
    • clips_at_regular_timestamps: Extracts clips at fixed, regular time intervals (seconds).
    • clips_at_random_timestamps: Extracts clips at randomly selected time intervals (seconds).

    For a full tutorial on decoding and sampling, refer to the generated examples in the documentation.

  4. What is a scan in TorchCodec?

    main

    A scan is a full pass over a video file used to retrieve metadata about streams and frames. Crucially, a scan does not involve decoding, making it significantly more efficient than decoding the entire file.

    In torchcodec.decoders.VideoDecoder, the scanning behavior is controlled by the seek_mode parameter:

    • seek_mode="exact": Triggers a scan.
    • seek_mode="approximate": Does not trigger a scan.
  5. What are clips and FrameBatch?

    main

    A clip is a sequence of frames, typically ordered by their pts. These frames do not have to be consecutive.

    In terms of data structures:

    • A single clip is represented as a 4D torchcodec.FrameBatch.
    • A group of clips (such as those returned by samplers) is represented as a 5D torchcodec.FrameBatch.
  6. Use multi-stream encoders for audio and video

    main

    To encode both audio and video streams simultaneously, use the multi-stream encoder classes. This approach is suitable for creating synchronized media files. For a complete walkthrough, refer to the encoding_multi_stream_encoding.py tutorial.

    Key components include:

    • Encoder: The base class for multi-stream encoding.
    • VideoStream: Manages the video component of the multi-stream encoder.
    • AudioStream: Manages the audio component of the multi-stream encoder.
    # Refer to the tutorial for a full implementation:
    # :ref:`sphx_glr_generated_examples_encoding_multi_stream_encoding.py`
  7. Install TorchCodec

    main

    To install TorchCodec, follow the instructions provided in the official repository README.

    Note: TorchCodec relies on FFmpeg being installed on your system. It will use the version of FFmpeg that is currently available in your environment.

    https://github.com/meta-pytorch/torchcodec?tab=readme-ov-file#installing-torchcodec
  8. Configure CUDA support for TorchCodec

    main

    To use CUDA-enabled decoding on Linux or Windows, ensure you have a GPU with NVDEC hardware support and the libnvrtc CUDA library installed.

    To install a specific CUDA Toolkit version (e.g., 13.0), use the --index-url flag. Ensure the installed torch version matches the CUDA version:

    pip install torch torchcodec --index-url=https://download.pytorch.org/whl/cu130

    To verify your FFmpeg installation supports NVDEC, run:

    ffmpeg -decoders | grep -i nvidia

    It should return a line like V..... h264_cuvid Nvidia CUVID H264 decoder (codec h264).

    pip install torch torchcodec --index-url=https://download.pytorch.org/whl/cu130
  9. Install TorchCodec

    main

    To install TorchCodec, follow these steps:

    1. Install FFmpeg: Required for video and audio decoding/encoding (VideoDecoder, AudioDecoder, VideoEncoder, AudioEncoder). FFmpeg versions 4 through 8 are supported. If you use conda, you can install it via:

      conda install "ffmpeg"
      # or
      conda install "ffmpeg" -c conda-forge

      Note: FFmpeg is optional if you only use image decoders like decode_image.

    2. Install PyTorch and TorchCodec:

      pip install torch torchcodec

    Platform Specifics

    • Linux (x86/aarch64): Installs CUDA-enabled wheels by default.
    • macOS/Windows: Installs CPU-only wheels by default.
    • CPU-only (Linux): To explicitly install CPU wheels on Linux:
      pip install torchcodec --index-url=https://download.pytorch.org/whl/cpu
    • Intel GPU (XPU): Requires a standalone plugin:
      pip install torchcodec-xpu --extra-index-url=https://download.pytorch.org/whl/xpu
    pip install torch torchcodec