TorchCodec

0.11.0 · active · verified Sat Apr 11

TorchCodec is a Python library developed by PyTorch that provides efficient video and audio decoding and encoding capabilities, tightly integrated with PyTorch tensors. It aims to simplify video and audio processing for machine learning workflows, supporting both CPU and GPU operations. The current version is 0.11.0, with new releases occurring approximately every 1-2 months.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to initialize a `VideoDecoder` for a given video file path. It prints basic video metadata and provides commented-out lines for decoding frames. Remember to replace the placeholder `video_path` with an actual video file on your system.

import torchcodec
from torchcodec.decoders import VideoDecoder
import os

# Replace 'path/to/your/video.mp4' with the actual path to your video file.
# For demonstration, we use an environment variable or a placeholder.
video_path = os.environ.get('TORCHCODEC_DEMO_VIDEO', 'path/to/your/video.mp4')

try:
    decoder = VideoDecoder(video_path)
    print(f"VideoDecoder initialized for: {video_path}")
    print(f"Video width: {decoder.width}")
    print(f"Video height: {decoder.height}")
    
    # Uncomment the following lines to decode frames:
    # frames = decoder.next_chunk(num_frames=10)
    # print(f"Decoded 10 frames with shape: {frames.shape}")

except FileNotFoundError:
    print(f"Error: Video file not found at '{video_path}'. Please replace with a valid path.")
except Exception as e:
    print(f"An error occurred during decoder initialization or usage: {e}")
print("\nTo decode frames, ensure a valid video_path is provided and uncomment the 'frames = decoder.next_chunk...' line.")

view raw JSON →