Welcome to the TorchCodec documentation!#

TorchCodec is a PyTorch-native library for decoding and encoding media: videos, audio, and images, on CPU and CUDA GPU. It aims to be fast, easy to use, and well integrated into the PyTorch ecosystem. If you want to use PyTorch to train ML models on videos, audio, or images, TorchCodec is how you turn these into tensors, and back.

We achieve these capabilities through:

  • Pythonic APIs that mirror Python and PyTorch conventions.

  • Relying on FFmpeg to do the video and audio decoding / encoding. TorchCodec uses the version of FFmpeg you already have installed. FFmpeg is a mature library with broad coverage available on most systems. It is, however, not easy to use. TorchCodec abstracts FFmpeg’s complexity to ensure it is used correctly and efficiently. FFmpeg is optional: the image decoders and encoders don’t need it.

  • Returning data as PyTorch tensors, ready to be fed into PyTorch transforms or used directly to train models.

Installation instructions#

Decoding#

Getting Started with TorchCodec

A simple video decoding example

generated_examples/decoding/basic_example.html
Image Decoding

How to decode images (JPEG on CPU and CUDA, PNG, WebP, and more)

generated_examples/decoding/image_decoding.html
Audio Decoding

A simple audio decoding example

generated_examples/decoding/audio_decoding.html
GPU decoding

A simple example demonstrating CUDA GPU decoding

generated_examples/decoding/basic_cuda_example.html
Streaming video

How to efficiently decode videos from the cloud

generated_examples/decoding/file_like.html
Parallel decoding

How to decode a video with multiple processes or threads.

generated_examples/decoding/parallel_decoding.html
Clip sampling

How to sample regular and random clips from a video

generated_examples/decoding/sampling.html
Decoder transforms

How to apply transforms while decoding

generated_examples/decoding/transforms.html
Performance Tips

Tips for optimizing video decoding performance

generated_examples/decoding/performance_tips.html
HDR Decoding

How to decode HDR videos with the output_dtype parameter

generated_examples/decoding/hdr_decoding.html

Encoding#

Video and Audio Encoding

How encode audio and video streams (CPU and CUDA)

generated_examples/encoding/multi_stream_encoding.html
Image Encoding

How to encode image tensors into JPEG (CPU and CUDA) or PNG

generated_examples/encoding/image_encoding.html
Video Encoding

How to encode video frames into a video file

generated_examples/encoding/video_encoding.html
Audio Encoding

How to encode audio samples into an audio file

generated_examples/encoding/audio_encoding.html

Migrating from torchvision#

Migrating from torchvision

How to port torchvision.io image decoding and encoding code

generated_examples/migration/torchvision_migration.html