Skip to main content

Installation | Documentation | Contributing | License

TorchCodec

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, or run inference, 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 and 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, and the image decoders and encoders don't need it: see Installing TorchCodec.)
  • Returning data as PyTorch tensors, ready to be fed into PyTorch transforms or used directly to train models.

Usage Examples

Below are some examples of what you can do with TorchCodec. For more detailed examples and more use-cases, check out our documentation!

Video Decoding

from torchcodec.decoders import VideoDecoder

device = "cpu"  # or e.g. "cuda" !
decoder = VideoDecoder("path/to/video.mp4", device=device)

decoder.metadata
# VideoStreamMetadata:
#   num_frames: 250
#   duration_seconds: 10.0
#   bit_rate: 31315.0
#   codec: h264
#   average_fps: 25.0
#   ... (truncated output)

# Simple Indexing API
decoder[0]  # uint8 tensor of shape [C, H, W]
decoder[0 : -1 : 20]  # uint8 stacked tensor of shape [N, C, H, W]

# Indexing, with PTS and duration info:
decoder.get_frames_at(indices=[2, 100])
# FrameBatch:
#   data (shape): torch.Size([2, 3, 270, 480])
#   pts_seconds: tensor([0.0667, 3.3367], dtype=torch.float64)
#   duration_seconds: tensor([0.0334, 0.0334], dtype=torch.float64)

# Time-based indexing with PTS and duration info
decoder.get_frames_played_at(seconds=[0.5, 10.4])
# FrameBatch:
#   data (shape): torch.Size([2, 3, 270, 480])
#   pts_seconds: tensor([ 0.4671, 10.3770], dtype=torch.float64)
#   duration_seconds: tensor([0.0334, 0.0334], dtype=torch.float64)

You can use the following snippet to generate a video with FFmpeg and try out the VideoDecoder:

ffmpeg -f lavfi -i testsrc2=size=640x400:duration=10:rate=25 /tmp/output_video.mp4

Video and Audio Encoding

from torchcodec.encoders import Encoder

encoder = Encoder()
video_stream = encoder.add_video(
    height=height, width=width, frame_rate=frame_rate,
)
audio_stream = encoder.add_audio(
    sample_rate=sample_rate, num_channels=num_channels,
)
with encoder.open_file("output.mp4"):
    video_stream.add_frames(frames_batch_0)
    audio_stream.add_samples(samples_batch_0)
    video_stream.add_frames(frames_batch_1)
    audio_stream.add_samples(samples_batch_1)
    # ...

Image Decoding and Encoding

from torchcodec.decoders import decode_image, decode_jpeg
from torchcodec.encoders import JpegEncoder

# JPEG, PNG, WebP, GIF, AVIF and HEIC, with the format detected automatically.
image = decode_image("path/to/image.jpg")  # uint8 tensor of shape [C, H, W]

# Or use the format-specific decoders, e.g. to decode JPEGs on GPU:
image = decode_jpeg("path/to/image.jpg", device="cuda")

# JPEG and PNG encoding. JPEGEncoder also supports CUDA encoding!
JpegEncoder(image).to_file("output.jpg")  # also .to_tensor() and .to_file_like()

Installing TorchCodec

  1. Install FFmpeg, if it's not already installed. TorchCodec supports all major FFmpeg versions in [4, 9]. Linux distributions usually come with FFmpeg pre-installed. You'll need FFmpeg that comes with separate shared libraries. This is especially relevant for Windows users: these are usually called the "shared" releases.

    If FFmpeg is not already installed, or you need a more recent version, an easy way to install it is to use conda:

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

    Refer to the official FFmpeg download page for more installation options.

    Note: FFmpeg is an optional dependency. It is needed for video and audio decoding and encoding (VideoDecoder, AudioDecoder, VideoEncoder, AudioEncoder, etc.). The image decoders and encoders (decode_image, decode_jpeg, JpegEncoder, PngEncoder, etc.) do not require FFmpeg, so if you only need images you can skip this step.

  2. Install PyTorch and TorchCodec:

    pip install torch torchcodec
    

    That's it! On Linux x86 and aarch64, this will install CUDA-enabled wheels by default (matching the default behavior of pip install torch). These wheels should still work even if you do not have a GPU on your machine. On macOS and Windows this will install CPU-only wheels. CPU wheels are available for Linux (x86_64 and aarch64), macOS, and Windows.

CUDA support

On CUDA GPUs, TorchCodec supports decoding and encoding of videos and jpeg images. CUDA-enabled wheels are installed by default on Linux. For Windows, you'll need to pass --index-url as described below.

For video, make sure you have a GPU with NVDEC and NVENC hardware that supports the formats you want. Refer to Nvidia's GPU support matrix here.

To select a specific CUDA Toolkit version, use --index-url. Make sure to install the corresponding PyTorch version as well (refer to the official instructions):

# This corresponds to CUDA Toolkit version 13.0.
pip install torch torchcodec --index-url=https://download.pytorch.org/whl/cu130

Make sure your FFmpeg has NVDEC and NVENC support:

ffmpeg -decoders | grep -i nvidia
# This should show a line like this:
# V..... h264_cuvid           Nvidia CUVID H264 decoder (codec h264)

ffmpeg -encoders | grep -i nvidia
# This should show a line like this:
# V....D h264_nvenc           NVIDIA NVENC H.264 encoder (codec h264)

To check that FFmpeg libraries work with NVDEC correctly you can decode a generated test video:

ffmpeg -hwaccel cuda -hwaccel_output_format cuda -f lavfi -i testsrc2=duration=1 -f null -

CPU-only installation

To install CPU-only wheels explicitly (e.g. on Linux where CUDA wheels are the default):

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

XPU support

Intel GPUs (XPU) support requires a stand-alone plugin for TorchCodec:

pip install torchcodec-xpu --extra-index-url=https://download.pytorch.org/whl/xpu

For any XPU-related support, please refer to https://github.com/intel/torchlib-xpu.

Compatibility with torch versions

The following table indicates the compatibility between versions of torchcodec, torch and Python.

torchcodec torch Python
main / nightly main / nightly >=3.11, <=3.15
0.17 >=2.11 >=3.10, <=3.14
0.16 >=2.11 >=3.10, <=3.14
0.15 >=2.11 >=3.10, <=3.14
0.14 >=2.11 >=3.10, <=3.14
older versions
torchcodec torch Python
0.13 >=2.11 >=3.10, <=3.14
0.12 >=2.11 >=3.10, <=3.14
0.11 2.11 >=3.10, <=3.14
0.10 2.10 >=3.10, <=3.14
0.9 2.9 >=3.10, <=3.14
0.8 2.9 >=3.10, <=3.13
0.7 2.8 >=3.9, <=3.13
0.6 2.8 >=3.9, <=3.13
0.5 2.7 >=3.9, <=3.13
0.4 2.7 >=3.9, <=3.13
0.3 2.7 >=3.9, <=3.13
0.2 2.6 >=3.9, <=3.13
0.1 2.5 >=3.9, <=3.12
0.0.3 2.4 >=3.8, <=3.12

Contributing

We welcome contributions to TorchCodec! Please see our contributing guide for more details.

License

TorchCodec is released under the BSD 3 license.

However, TorchCodec may be used with code not written by Meta which may be distributed under different licenses.

For example, if you build TorchCodec with ENABLE_CUDA=1 or use the CUDA-enabled release of torchcodec, please review CUDA's license here: Nvidia licenses.

Metadata

Release files for torchcodec 1.0.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Built distributions (wheels)

Table of built distributions (wheels) for torchcodec 1.0.0
File Actions
torchcodec-1.0.0-py3-none-win_amd64.whl Python 3 none Windows x86-64 Details for torchcodec-1.0.0-py3-none-win_amd64.whl
torchcodec-1.0.0-py3-none-manylinux_2_27_x86_64.whl Python 3 none Linux glibc 2.27+ x86-64 Details for torchcodec-1.0.0-py3-none-manylinux_2_27_x86_64.whl
torchcodec-1.0.0-py3-none-manylinux_2_27_aarch64.manylinux_2_28_aarch64.whl Python 3 none Linux glibc 2.27+ ARM64, Linux glibc 2.28+ ARM64 Details for torchcodec-1.0.0-py3-none-manylinux_2_27_aarch64.manylinux_2_28_aarch64.whl
torchcodec-1.0.0-py3-none-macosx_14_0_arm64.whl Python 3 none macOS 14.0+ ARM64 Details for torchcodec-1.0.0-py3-none-macosx_14_0_arm64.whl

Total release size: 29.6 MB

Release files / torchcodec-1.0.0-py3-none-win_amd64.whl

Download URL torchcodec-1.0.0-py3-none-win_amd64.whl
Size 6.4 MB
Tags Python 3 Windows x86-64
SHA-256 checksum
How to use checksums
fe2a6d8b82bb4f84bad97bba0e4c4750abcf073633bdf03a1a40bb9809bf7b27
BLAKE2b-256 checksum
How to use checksums
0a22145cc1c00f518ddf06c6fc3b79c9916967d3edb7c8dc208225d64e7e859e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Dec 2, 2026.

Transparency log

Release files / torchcodec-1.0.0-py3-none-manylinux_2_27_x86_64.whl

Download URL torchcodec-1.0.0-py3-none-manylinux_2_27_x86_64.whl
Size 10.0 MB
Tags Linux glibc 2.27+ x86-64 Python 3
SHA-256 checksum
How to use checksums
8c6e3b826fe8ba9250cf40887a728df15637a251edf0ee18dfee04f733b553da
BLAKE2b-256 checksum
How to use checksums
6683217cbee387a4c95d95eaf231944700472cd17a97d336bb3d226875f9e895
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Dec 2, 2026.

Transparency log

Release files / torchcodec-1.0.0-py3-none-manylinux_2_27_aarch64.manylinux_2_28_aarch64.whl

Download URL torchcodec-1.0.0-py3-none-manylinux_2_27_aarch64.manylinux_2_28_aarch64.whl
Size 8.9 MB
Tags Linux glibc 2.27+ ARM64 Linux glibc 2.28+ ARM64 Python 3
SHA-256 checksum
How to use checksums
f75d205638f98528fc839e1664bedcbfc023fb85c3fb2acad45021a5a4fb3437
BLAKE2b-256 checksum
How to use checksums
ee2cc443160760e5e1a77d6e4462714f6b1060e8323177419a18853e5f1954fd
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Dec 2, 2026.

Transparency log

Release files / torchcodec-1.0.0-py3-none-macosx_14_0_arm64.whl

Download URL torchcodec-1.0.0-py3-none-macosx_14_0_arm64.whl
Size 4.4 MB
Tags Python 3 macOS 14.0+ ARM64
SHA-256 checksum
How to use checksums
7c9af80d753d575d2e0afe51e8ad9d8aed450ce8900bc9015e7139c77c3318f5
BLAKE2b-256 checksum
How to use checksums
589a98839d886486831d5cbc8ed683902882f8b1a67f65d4064fde23280cc19f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Dec 2, 2026.

Transparency log

Security for torchcodec 1.0.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

Full provenance

All 4 files in this release have been uploaded with provenance metadata.

Select from 4 files

Attestation for torchcodec-1.0.0-py3-none-win_amd64.whl

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Dec 2, 2026.

Transparency log

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page