Skip to main content

A video decoder for PyTorch

Project description

Installation | Simple Example | Detailed Example | Documentation | Contributing | License

TorchCodec

TorchCodec is a Python library for decoding video and audio data into PyTorch tensors, on CPU and CUDA GPU. It also supports video and audio encoding on CPU! 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 and audio, TorchCodec is how you turn these into data.

We achieve these capabilities through:

  • Pythonic APIs that mirror Python and PyTorch conventions.
  • Relying on FFmpeg to do the 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.
  • Returning data as PyTorch tensors, ready to be fed into PyTorch transforms or used directly to train models.

Using TorchCodec

Here's a condensed summary of what you can do with TorchCodec. For more detailed examples, check out our documentation!

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)

Clip sampling

from torchcodec.samplers import clips_at_regular_timestamps

clips_at_regular_timestamps(
    decoder,
    seconds_between_clip_starts=1.5,
    num_frames_per_clip=4,
    seconds_between_frames=0.1
)
# FrameBatch:
#   data (shape): torch.Size([9, 4, 3, 270, 480])
#   pts_seconds: tensor([[ 0.0000,  0.0667,  0.1668,  0.2669],
#         [ 1.4681,  1.5682,  1.6683,  1.7684],
#         [ 2.9696,  3.0697,  3.1698,  3.2699],
#         ... (truncated), dtype=torch.float64)
#   duration_seconds: tensor([[0.0334, 0.0334, 0.0334, 0.0334],
#         [0.0334, 0.0334, 0.0334, 0.0334],
#         [0.0334, 0.0334, 0.0334, 0.0334],
#         ... (truncated), dtype=torch.float64)

You can use the following snippet to generate a video with FFmpeg and tryout TorchCodec:

fontfile=/usr/share/fonts/dejavu-sans-mono-fonts/DejaVuSansMono-Bold.ttf
output_video_file=/tmp/output_video.mp4

ffmpeg -f lavfi -i \
    color=size=640x400:duration=10:rate=25:color=blue \
    -vf "drawtext=fontfile=${fontfile}:fontsize=30:fontcolor=white:x=(w-text_w)/2:y=(h-text_h)/2:text='Frame %{frame_num}'" \
    ${output_video_file}

Installing TorchCodec

Installing CPU-only TorchCodec

  1. Install the latest stable version of PyTorch following the official instructions. For other versions, refer to the table below for compatibility between versions of torch and torchcodec.

  2. Install FFmpeg, if it's not already installed. TorchCodec supports supports all major FFmpeg versions in [4, 8]. 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
    
  3. Install TorchCodec:

    pip install torchcodec
    

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

torchcodec torch Python
main / nightly main / nightly >=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

Installing CUDA-enabled TorchCodec

First, make sure you have a GPU that has NVDEC hardware that can decode the format you want. Refer to Nvidia's GPU support matrix for more details here.

  1. Install FFmpeg with NVDEC support. TorchCodec with CUDA should work with FFmpeg versions in [4, 8].

    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
    

    After installing FFmpeg make sure it has NVDEC support when you list the supported decoders:

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

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

    ffmpeg -hwaccel cuda -hwaccel_output_format cuda -i test/resources/nasa_13013.mp4 -f null -
    

Linux

  1. Install Pytorch corresponding to your CUDA Toolkit using the official instructions. You'll need the libnpp and libnvrtc CUDA libraries, which are usually part of the CUDA Toolkit.

  2. Install TorchCodec

    Pass in an --index-url parameter that corresponds to your CUDA Toolkit version, for example:

    # This corresponds to CUDA Toolkit version 12.6. It should be the same one
    # you used when you installed PyTorch (If you installed PyTorch with pip).
    pip install torchcodec --index-url=https://download.pytorch.org/whl/cu126
    

    Note that without passing in the --index-url parameter, pip installs the CPU-only version of TorchCodec.

Windows

  1. On Windows (experimental support), you'll need to rely on conda to install both pytorch and TorchCodec:

    conda install -c conda-forge "torchcodec=*=*cuda*"
    

Benchmark Results

The following was generated by running our benchmark script on a lightly loaded 22-core machine with an Nvidia A100 with 5 NVDEC decoders.

benchmark_results

The top row is a Mandelbrot video generated from FFmpeg that has a resolution of 1280x720 at 60 fps and is 120 seconds long. The bottom row is promotional video from NASA that has a resolution of 960x540 at 29.7 fps and is 206 seconds long. Both videos were encoded with libx264 and yuv420p pixel format. All decoders, except for TorchVision, used FFmpeg 6.1.2. TorchVision used FFmpeg 4.2.2.

For TorchCodec, the "approx" label means that it was using approximate mode for seeking.

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.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distributions

If you're not sure about the file name format, learn more about wheel file names.

torchcodec-0.10.0-cp314-cp314-win_amd64.whl (2.2 MB view details)

Uploaded CPython 3.14Windows x86-64

torchcodec-0.10.0-cp314-cp314-manylinux_2_28_x86_64.whl (2.1 MB view details)

Uploaded CPython 3.14manylinux: glibc 2.28+ x86-64

torchcodec-0.10.0-cp314-cp314-macosx_11_0_arm64.whl (3.8 MB view details)

Uploaded CPython 3.14macOS 11.0+ ARM64

torchcodec-0.10.0-cp313-cp313-win_amd64.whl (2.2 MB view details)

Uploaded CPython 3.13Windows x86-64

torchcodec-0.10.0-cp313-cp313-manylinux_2_28_x86_64.whl (2.1 MB view details)

Uploaded CPython 3.13manylinux: glibc 2.28+ x86-64

torchcodec-0.10.0-cp313-cp313-macosx_12_0_arm64.whl (4.0 MB view details)

Uploaded CPython 3.13macOS 12.0+ ARM64

torchcodec-0.10.0-cp312-cp312-win_amd64.whl (2.2 MB view details)

Uploaded CPython 3.12Windows x86-64

torchcodec-0.10.0-cp312-cp312-manylinux_2_28_x86_64.whl (2.1 MB view details)

Uploaded CPython 3.12manylinux: glibc 2.28+ x86-64

torchcodec-0.10.0-cp312-cp312-macosx_11_0_arm64.whl (4.1 MB view details)

Uploaded CPython 3.12macOS 11.0+ ARM64

torchcodec-0.10.0-cp311-cp311-win_amd64.whl (2.2 MB view details)

Uploaded CPython 3.11Windows x86-64

torchcodec-0.10.0-cp311-cp311-manylinux_2_28_x86_64.whl (2.1 MB view details)

Uploaded CPython 3.11manylinux: glibc 2.28+ x86-64

torchcodec-0.10.0-cp311-cp311-macosx_11_0_arm64.whl (3.9 MB view details)

Uploaded CPython 3.11macOS 11.0+ ARM64

torchcodec-0.10.0-cp310-cp310-win_amd64.whl (2.2 MB view details)

Uploaded CPython 3.10Windows x86-64

torchcodec-0.10.0-cp310-cp310-manylinux_2_28_x86_64.whl (2.1 MB view details)

Uploaded CPython 3.10manylinux: glibc 2.28+ x86-64

torchcodec-0.10.0-cp310-cp310-macosx_11_0_arm64.whl (3.4 MB view details)

Uploaded CPython 3.10macOS 11.0+ ARM64

File details

Details for the file torchcodec-0.10.0-cp314-cp314-win_amd64.whl.

File metadata

File hashes

Hashes for torchcodec-0.10.0-cp314-cp314-win_amd64.whl
Algorithm Hash digest
SHA256 4734eb82146516049e1070152eb3013d7b9689159ef35db7c0894d130d4e335d
MD5 e2293d2c2166d0dff351cda6c989546f
BLAKE2b-256 93710fc10230965e319552e064d66f1c8675b471d1b98c4a94c529e771b3c165

See more details on using hashes here.

File details

Details for the file torchcodec-0.10.0-cp314-cp314-manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for torchcodec-0.10.0-cp314-cp314-manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 bec2b938ad4b294bd71d0b0ab4976037c740be0c80be79e67803ebed4eff270e
MD5 e1f23b994cdc804563cc0bb6f225b5fa
BLAKE2b-256 96a25fe0e62b208a367f741361881321c1b25de487318a44f870f326747585a1

See more details on using hashes here.

File details

Details for the file torchcodec-0.10.0-cp314-cp314-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for torchcodec-0.10.0-cp314-cp314-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 bb882c12ca07dcf6d82833db67e6b565693a4bccfeab6696697620e43e465556
MD5 f944b2d5aa6a3c1ed06e39c092f01c6d
BLAKE2b-256 90c94b6242e3456bae148f4086337d3e43d98c4e79c04091de3462db9f5eb67a

See more details on using hashes here.

File details

Details for the file torchcodec-0.10.0-cp313-cp313-win_amd64.whl.

File metadata

File hashes

Hashes for torchcodec-0.10.0-cp313-cp313-win_amd64.whl
Algorithm Hash digest
SHA256 9e077146e894f373b805972da96aee42ff8bb87b1a3f3fb64d26925cd9e64184
MD5 bccc42d9947c5d05f06383225b185935
BLAKE2b-256 df9db73a0f47e11b66a1d23c7d867f12f8bb41b1b5e707d7e23d8f54793e3267

See more details on using hashes here.

File details

Details for the file torchcodec-0.10.0-cp313-cp313-manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for torchcodec-0.10.0-cp313-cp313-manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 71f25caf9ab89a434ae2008b1374fd98557a6864b8313b103bae53af3e6fd17f
MD5 614942bcc485bdc43a8382a191fe88d8
BLAKE2b-256 fd85fc44f6d702dfd344e6859a9a4d713aaaa991578eb74677a80297d9ae8a07

See more details on using hashes here.

File details

Details for the file torchcodec-0.10.0-cp313-cp313-macosx_12_0_arm64.whl.

File metadata

File hashes

Hashes for torchcodec-0.10.0-cp313-cp313-macosx_12_0_arm64.whl
Algorithm Hash digest
SHA256 be3ce7cc667effecd06da9d0d6c5e9e347c5f376b705934e7b82378a65cf6eef
MD5 87d379fc309410acccf01474f462ce11
BLAKE2b-256 d2b6b1041c8ccb175b08779b3e2d3e60f838bbcbfe2398d49e3673b6a66f0649

See more details on using hashes here.

File details

Details for the file torchcodec-0.10.0-cp312-cp312-win_amd64.whl.

File metadata

File hashes

Hashes for torchcodec-0.10.0-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 e4f022fa53d91b414b4177fe87b0afc40a806092da4595d24de4b245d6fb0fba
MD5 0e267bb8d2b68856e2a56b156f144936
BLAKE2b-256 cf6c9af3bd945a610d6c757d898aa4624e0f5135cc8a6992d0a1d846f1084576

See more details on using hashes here.

File details

Details for the file torchcodec-0.10.0-cp312-cp312-manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for torchcodec-0.10.0-cp312-cp312-manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 6e43184d83ccced965b31cad5bb6200c779646fee2ec153a6d784b4def40c91b
MD5 d5c8f600c6f9a322faede229cdb55b5a
BLAKE2b-256 2934ccc711b6dc581e43b8d8d227e4173a8826994ee7b68d6b3d82291f307325

See more details on using hashes here.

File details

Details for the file torchcodec-0.10.0-cp312-cp312-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for torchcodec-0.10.0-cp312-cp312-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 b35fa4061c5757f8d714187c040a90a11669de6470a644bb04e3cd335ff1c110
MD5 2b39ed640d491bfbbc0404e0db0c98a7
BLAKE2b-256 52ff2b27797e039673156710e5a0febe87cafc203722acafa3d34db283b40cf9

See more details on using hashes here.

File details

Details for the file torchcodec-0.10.0-cp311-cp311-win_amd64.whl.

File metadata

File hashes

Hashes for torchcodec-0.10.0-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 f51d9435d3c75c0b55f5dc64a22ce9cfb58ac19013cdd8ce572a523ef75e2b58
MD5 7541ac96bfd7b45d34d4cc002d5bc9e0
BLAKE2b-256 74f175d70391de0581069864b3c204bb7e463a2b3b32e840ff9d103688a6db94

See more details on using hashes here.

File details

Details for the file torchcodec-0.10.0-cp311-cp311-manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for torchcodec-0.10.0-cp311-cp311-manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 2e2be11c4468a58940572fcf5f8ed5e41187c1de214267f692e2fd5ac8731198
MD5 83499d8907ae245d8f7f44bf159a564f
BLAKE2b-256 b9c8618bde55c1908583290883537326174e633a383a8337226ce0c7c6d70090

See more details on using hashes here.

File details

Details for the file torchcodec-0.10.0-cp311-cp311-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for torchcodec-0.10.0-cp311-cp311-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 3dde1ebd9677ec1587f1e45486b3d59bd3e41a0bf4fc9b3dc6880e64c421ad56
MD5 484999533684dadf7a788876b1c4b943
BLAKE2b-256 1c0d51ab5cb4ba8eb60e3e39651a4d43be89a592cc193fe11feb6509509b0121

See more details on using hashes here.

File details

Details for the file torchcodec-0.10.0-cp310-cp310-win_amd64.whl.

File metadata

File hashes

Hashes for torchcodec-0.10.0-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 a49e3ebab65e559a98af6f63371a837f69b8542059da8def30bd6e658c5e86d3
MD5 3ac2323d031fa77b04321ed85783e2aa
BLAKE2b-256 dc51b7c339cabf375789fc74b56006fe6bc5e029ededf83bc7629864cb6ed083

See more details on using hashes here.

File details

Details for the file torchcodec-0.10.0-cp310-cp310-manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for torchcodec-0.10.0-cp310-cp310-manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 61f9b12fcd5b89d5e7e874c5feeb7c2c99821868a32f6ebbf6e8692409b2b6f7
MD5 348794554ccf079b5cd8c5e817b676df
BLAKE2b-256 f047041180c095e4dbc0cff8e847974bf400114379c8f114aa8c3c96e9d6bd4e

See more details on using hashes here.

File details

Details for the file torchcodec-0.10.0-cp310-cp310-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for torchcodec-0.10.0-cp310-cp310-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 a2ecb3e38414a9326ffca56e49a05a9f513813aa118b7f540110d09ab725d1f7
MD5 6e4dee270340e9013810df22ea150fff
BLAKE2b-256 d01c549e79c0f5a7e48c1f767b648c3ea3301a8cd702f66c9cb32c86eb347d63

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page