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 audio encoding, and video encoding will come soon! 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. Linux distributions usually come with FFmpeg pre-installed. TorchCodec supports supports all major 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
    
  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.13
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.9.0-cp314-cp314-win_amd64.whl (2.2 MB view details)

Uploaded CPython 3.14Windows x86-64

torchcodec-0.9.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.9.0-cp314-cp314-macosx_11_0_arm64.whl (3.8 MB view details)

Uploaded CPython 3.14macOS 11.0+ ARM64

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

Uploaded CPython 3.13Windows x86-64

torchcodec-0.9.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.9.0-cp313-cp313-macosx_12_0_arm64.whl (4.0 MB view details)

Uploaded CPython 3.13macOS 12.0+ ARM64

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

Uploaded CPython 3.12Windows x86-64

torchcodec-0.9.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.9.0-cp312-cp312-macosx_11_0_arm64.whl (4.1 MB view details)

Uploaded CPython 3.12macOS 11.0+ ARM64

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

Uploaded CPython 3.11Windows x86-64

torchcodec-0.9.0-cp311-cp311-manylinux_2_28_x86_64.whl (2.0 MB view details)

Uploaded CPython 3.11manylinux: glibc 2.28+ x86-64

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

Uploaded CPython 3.11macOS 11.0+ ARM64

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

Uploaded CPython 3.10Windows x86-64

torchcodec-0.9.0-cp310-cp310-manylinux_2_28_x86_64.whl (2.0 MB view details)

Uploaded CPython 3.10manylinux: glibc 2.28+ x86-64

torchcodec-0.9.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.9.0-cp314-cp314-win_amd64.whl.

File metadata

  • Download URL: torchcodec-0.9.0-cp314-cp314-win_amd64.whl
  • Upload date:
  • Size: 2.2 MB
  • Tags: CPython 3.14, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.0

File hashes

Hashes for torchcodec-0.9.0-cp314-cp314-win_amd64.whl
Algorithm Hash digest
SHA256 e6813c055eb6cbead0de4b281a9d265b05c68a9701b95a2725b21ae7a2f13a68
MD5 36885439b0587fc93ce6fdc35241cb58
BLAKE2b-256 22d90f0bedc25a3ccfa1f2347444359a4e5808cca2e72a2e34bb867547c8211c

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for torchcodec-0.9.0-cp314-cp314-manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 bf866ae94f45546c43a2781508f9bfdb6d3557330065b5ac1b8e31ff2f61a005
MD5 e5807b967aac6cadcb4a80109da16fd8
BLAKE2b-256 b13dea1f5f09c9c4cc76b76699bd38d5dd7ae4c9efade0a3f010f52bcf49bbb5

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for torchcodec-0.9.0-cp314-cp314-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 6ed000612a1a8a403f52129e63022b885092d245fde9323447580024a722e01b
MD5 f9a10c852be76a56ff1c5c6caccabb01
BLAKE2b-256 ec8d243a7a828131a874493c122aaecacc04553ed5c81e4ff8f2e0730f414e6f

See more details on using hashes here.

File details

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

File metadata

  • Download URL: torchcodec-0.9.0-cp313-cp313-win_amd64.whl
  • Upload date:
  • Size: 2.2 MB
  • Tags: CPython 3.13, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.0

File hashes

Hashes for torchcodec-0.9.0-cp313-cp313-win_amd64.whl
Algorithm Hash digest
SHA256 0f6e4a37181820b90a2aadc827d95f119b11a59095252175add3fa22584f5614
MD5 1435ebe9ce6fcb14383287e17d66b085
BLAKE2b-256 74c791ed7d78d934c7decee30eaf6f783e92b9d7d7e1c05ffaf9db5d8a26b7c7

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for torchcodec-0.9.0-cp313-cp313-manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 0791fafa73eab4d12001b256d22c94b244e2dea28214ba0bb1dee0f2bfb87b13
MD5 22d6b3dfe53be15a75c4bd352d550f6b
BLAKE2b-256 a881db06e824616da3b40772a9ca8db9f49e0790e323ee160520ddd67fce19ed

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for torchcodec-0.9.0-cp313-cp313-macosx_12_0_arm64.whl
Algorithm Hash digest
SHA256 f47f03335a373cdfe32b2c9f9f515a408c64ea9c6efdf616ca6d3c487e1b34b1
MD5 fee50a4625650791c3ac6b4bf29d6279
BLAKE2b-256 b7d50be5d2be7c787bf4cefe793bf99bd09fe1f174e8827ddb29d210ae267b58

See more details on using hashes here.

File details

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

File metadata

  • Download URL: torchcodec-0.9.0-cp312-cp312-win_amd64.whl
  • Upload date:
  • Size: 2.2 MB
  • Tags: CPython 3.12, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.0

File hashes

Hashes for torchcodec-0.9.0-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 30cf3053ddd54d993b7b28613beab4ffa1199339160c4f489a87bd5bb3c0062b
MD5 6ca182f1535c6ae12695d39616471cee
BLAKE2b-256 c15b1f712dc3cbf26e0eaab90dccf75aee468d63ce340c51beccbdfc426400c1

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for torchcodec-0.9.0-cp312-cp312-manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 c4b5964e85e616097b35db6927561bbac1cbf227e1a8d4dffb0acf00a7e94725
MD5 cfc74dae26a4e76817e268a078c8559a
BLAKE2b-256 4210742531478a71585bcb901913a9a806dc6b8c4097f39e6e82213a129cbaf1

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for torchcodec-0.9.0-cp312-cp312-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 30d031eafbe287a2a54b90b35109f7e0711b393bbb263cf90487f533b8ac92d4
MD5 5259a0ebb9505b5cf335d958699096df
BLAKE2b-256 6088dc4a7928ee80823913b1ec9d6433458b32d5510288030b122c9b3d58b484

See more details on using hashes here.

File details

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

File metadata

  • Download URL: torchcodec-0.9.0-cp311-cp311-win_amd64.whl
  • Upload date:
  • Size: 2.2 MB
  • Tags: CPython 3.11, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.0

File hashes

Hashes for torchcodec-0.9.0-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 69136a750cc6d174a469ee4b2dc91b5e167958e568f21b7c63850cc1e6dc3063
MD5 a6a32fbb23950cb0586f216da8d3e64e
BLAKE2b-256 421d0dabea1fa5ead79887a164ed5ff33fe98c6cdfee2ae3e932652316c99677

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for torchcodec-0.9.0-cp311-cp311-manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 c6536f88dec0fa323a8d6174d6527996bd7039d5a9194b52321466bb0f385b04
MD5 99d9acb7f2e3c524d54869dc16fe7497
BLAKE2b-256 363c0b83f9e0478fbf374b850c29f462545e3ad1317e7cfc1b718500e6eb274a

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for torchcodec-0.9.0-cp311-cp311-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 b25d520f02adea88b42827f0fcdde6d65dd7ee3aaeb403c578ea3bc9beac4e77
MD5 d6678660f6c963808346e4ab2baa88e7
BLAKE2b-256 f72c3488d93c68d2a91a09748fecef639bdfe954c6119ba3c902bdefcdc5cbf8

See more details on using hashes here.

File details

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

File metadata

  • Download URL: torchcodec-0.9.0-cp310-cp310-win_amd64.whl
  • Upload date:
  • Size: 2.2 MB
  • Tags: CPython 3.10, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.0

File hashes

Hashes for torchcodec-0.9.0-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 358ab279f7c85baa791023f479c9355f86bcd386a2788924a619078f7cba5a65
MD5 2456de500062133cd2773212fd00e4dd
BLAKE2b-256 ae2adcb3e0b8086d7ae6d4a5ca9a6a8ec8588182be75d5a92be0e4c4d58ef940

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for torchcodec-0.9.0-cp310-cp310-manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 7fea07ca0deb456e9f7595349f2288ac5747c9810e15dc89ca430a338d0516f5
MD5 90a7b940319687efbf641ac1633ded68
BLAKE2b-256 2d14fa78cf6e70dd1132f0a7424c7b5b52f8654f1faf8b9dde923f486686919f

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for torchcodec-0.9.0-cp310-cp310-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 bf100f3442fc93c90a20782f21d406dc5a56a0737b77b7cbbb4528b1466a8b5a
MD5 851447d6c2b83759b6814b1468c8ec6b
BLAKE2b-256 6c4ba03bdaf68e077ec6319a1cc593b256e0573eb8b50b2ac592dbd0316ec1f2

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