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.1-cp314-cp314-win_amd64.whl (2.2 MB view details)

Uploaded CPython 3.14Windows x86-64

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

Uploaded CPython 3.14macOS 11.0+ ARM64

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

Uploaded CPython 3.13Windows x86-64

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

Uploaded CPython 3.13macOS 12.0+ ARM64

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

Uploaded CPython 3.12Windows x86-64

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

Uploaded CPython 3.12macOS 11.0+ ARM64

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

Uploaded CPython 3.11Windows x86-64

torchcodec-0.9.1-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.1-cp311-cp311-macosx_11_0_arm64.whl (3.9 MB view details)

Uploaded CPython 3.11macOS 11.0+ ARM64

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

Uploaded CPython 3.10Windows x86-64

torchcodec-0.9.1-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.1-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.1-cp314-cp314-win_amd64.whl.

File metadata

  • Download URL: torchcodec-0.9.1-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.1.0 CPython/3.13.2

File hashes

Hashes for torchcodec-0.9.1-cp314-cp314-win_amd64.whl
Algorithm Hash digest
SHA256 2d01c8b3685a3a38f050ed2b526808a2938dba6f56cb9f9e967884fd858bba15
MD5 56a95526be9f2658632ce2b7b24191fe
BLAKE2b-256 f263752d0fc1c6e8f799ae880ca1087510def663a7f9aa1a70074ae334c6908f

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for torchcodec-0.9.1-cp314-cp314-manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 65634bb28b3155cf99f980dac31ecedb414c07b8156f8473ec9fb74bedbd2a1f
MD5 98eff0d67884fe286442d207ecb86de3
BLAKE2b-256 59a18462b55571286847ea31edb7634583125400824267db9ba8301f4ce3f137

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for torchcodec-0.9.1-cp314-cp314-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 b9bc5a5dff925df96d11bf90bd0ce964b8086bb11ae09adf353518192b5da483
MD5 ee1a917792a2f323e2c9792f352b4de4
BLAKE2b-256 0102f8ae9443d3bcbe8a8d6d0bbc3992296e5476e5afa1f244100a3a7967a36c

See more details on using hashes here.

File details

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

File metadata

  • Download URL: torchcodec-0.9.1-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.1.0 CPython/3.13.2

File hashes

Hashes for torchcodec-0.9.1-cp313-cp313-win_amd64.whl
Algorithm Hash digest
SHA256 df0b5a15998fd7457625c2af2a6276e0e710fac158d145045340dbbcd1cfdb65
MD5 cee7582700d46fd20d47aabd2b228070
BLAKE2b-256 13a2d78cd65863fb805d9e35fe90ae7574eab86ff0ae63438208bd07d2cf1fd2

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for torchcodec-0.9.1-cp313-cp313-manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 0643c5e9c3a51fdafdea87935d5b0a38e99626c664f47a150482d77ab370a877
MD5 0aeabf54dfe83e345f77ec9c873c2526
BLAKE2b-256 680506240f661e9aa08b20765305e3b88f60bff706bbe54ac35830af74612443

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for torchcodec-0.9.1-cp313-cp313-macosx_12_0_arm64.whl
Algorithm Hash digest
SHA256 5c9cdcba50c75be70ef6ec919ec1f7f14d9d5163d93cf6bd94403e134f03734c
MD5 ec0c2379c8cc28255908fbd5526a3d59
BLAKE2b-256 6fc767fc8417f9efa8a25c00a44f0d674761a0bad9c45e9725e3fd116b3c48ed

See more details on using hashes here.

File details

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

File metadata

  • Download URL: torchcodec-0.9.1-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.1.0 CPython/3.13.2

File hashes

Hashes for torchcodec-0.9.1-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 d9c8efe5845bde45a428f96493b4a041511f47f5bd53b333a0ad90426be4623a
MD5 d98bb13806c55c2061e5dfc1398dd947
BLAKE2b-256 b212c0bbf01b0ed52b69aaeed4af1043dc8308ccc522a47fcc082b34882e2ba2

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for torchcodec-0.9.1-cp312-cp312-manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 a50568ce73b70395d113833fb07394c223f5546ef5d4fafe0fdcd91627fca270
MD5 8ec630e0dc6d5a9d1a0ffe3658a41e5c
BLAKE2b-256 17c8bfb74babec98aff11ab4f239b0901f39e1a93338b3438e842d864dc46935

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for torchcodec-0.9.1-cp312-cp312-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 8996ec62b72c69545c30246df64df386d06d7ec7de0689be5d20dfc06aad6442
MD5 4fa17f057dbc9dc61e620475194178a3
BLAKE2b-256 4b603bfa459e09987af08e188811b191437c9d8215a74f4d418be6ff7df87b5c

See more details on using hashes here.

File details

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

File metadata

  • Download URL: torchcodec-0.9.1-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.1.0 CPython/3.13.2

File hashes

Hashes for torchcodec-0.9.1-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 508e0ce6da1e4af186b131cdbdd3d998bc4313e9f3a3f5b78f64aa5f9685ed88
MD5 688ea150f96e82d8c1ea77a2c23b30b4
BLAKE2b-256 53660612c7852cb7854dec45db9357397b4dbd4d6274832cc8949a8d1c0e518a

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for torchcodec-0.9.1-cp311-cp311-manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 5e959c7abf95de62f78653d416f00f7ca32936fbfd23371b23d5c8dc199f3670
MD5 ecf1b1651347e96baa3ca7323b320cf7
BLAKE2b-256 757411c59e0592e555df78cfee2fd4b8ba5c725e4bf160002af88ce09de99d9d

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for torchcodec-0.9.1-cp311-cp311-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 7a81d29e67cebefaec08f9c817635d1ff47205814884e527ef4b135a6c795612
MD5 80e125d11cb2a1178070b53f6e49f0de
BLAKE2b-256 c037169238bb55017b08a93530d6f4474a112780df4c05c26c78df7dd58401f1

See more details on using hashes here.

File details

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

File metadata

  • Download URL: torchcodec-0.9.1-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.1.0 CPython/3.13.2

File hashes

Hashes for torchcodec-0.9.1-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 d5267e6112b0d0b948d653dc3ffa486bb9a01febc81606d3dfd75cf5a4dcc9f3
MD5 6215d01ed2603fcdc3b6277ec291a5d2
BLAKE2b-256 68ddcbda16eb33d380c3e7b3cb6dbcfa59aa21e676ddebe3627e834be3b1c124

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for torchcodec-0.9.1-cp310-cp310-manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 53dfecabc843e4d78b77453732129bd8572dd9d022ab81a94f5305cd5e920ded
MD5 048faaa870c29f030bb26d8c9715e0c9
BLAKE2b-256 3096c693d75e32b4f07828c2b8a4b894be7366d0e46366ff10dad5825d891148

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for torchcodec-0.9.1-cp310-cp310-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 72738d7870edbcd6982207609efe314352e0bf90f4e3895a549e23a49ee0673c
MD5 00e18498d73d75cc1f4fcdd270e8d962
BLAKE2b-256 4ac3ae904a2a8cb4b6288684f022a6d5061a74435bc2d6f1c4742c16d441a9ee

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