Skip to main content

A video decoder for PyTorch

Project description

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

PaddleCodec

[!NOTE]

This repo is a fork of the original torchcodec project, with modifications to enhance compatibility and integration with PaddlePaddle.

Installation

pip install paddlecodec

Usage

import paddle
paddle.enable_compat(scope={"torchcodec"})  # Enable torch proxy before importing torchcodec
import torchcodec
# use torchcodec

Version Compatibility

paddlecodec paddle Python
main / nightly main / nightly >=3.10, <=3.13
0.2 3.4 >=3.10, <=3.13
0.1 3.3 >=3.9, <=3.13

The original README.md content is as follows:


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 major FFmpeg versions in [4, 7] on all platforms, and FFmpeg version 8 is supported on Mac and Linux.

    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<8"
    # or
    conda install "ffmpeg<8" -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, 7] on all platforms, and FFmpeg version 8 is supported on Linux.

    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<8"
    # or
    conda install "ffmpeg<8" -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.

paddlecodec-0.2.0-cp313-cp313-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl (2.5 MB view details)

Uploaded CPython 3.13manylinux: glibc 2.24+ x86-64manylinux: glibc 2.28+ x86-64

paddlecodec-0.2.0-cp313-cp313-manylinux_2_24_aarch64.manylinux_2_28_aarch64.whl (2.3 MB view details)

Uploaded CPython 3.13manylinux: glibc 2.24+ ARM64manylinux: glibc 2.28+ ARM64

paddlecodec-0.2.0-cp312-cp312-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl (2.5 MB view details)

Uploaded CPython 3.12manylinux: glibc 2.24+ x86-64manylinux: glibc 2.28+ x86-64

paddlecodec-0.2.0-cp312-cp312-manylinux_2_24_aarch64.manylinux_2_28_aarch64.whl (2.3 MB view details)

Uploaded CPython 3.12manylinux: glibc 2.24+ ARM64manylinux: glibc 2.28+ ARM64

paddlecodec-0.2.0-cp311-cp311-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl (2.5 MB view details)

Uploaded CPython 3.11manylinux: glibc 2.24+ x86-64manylinux: glibc 2.28+ x86-64

paddlecodec-0.2.0-cp311-cp311-manylinux_2_24_aarch64.manylinux_2_28_aarch64.whl (2.3 MB view details)

Uploaded CPython 3.11manylinux: glibc 2.24+ ARM64manylinux: glibc 2.28+ ARM64

paddlecodec-0.2.0-cp310-cp310-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl (2.5 MB view details)

Uploaded CPython 3.10manylinux: glibc 2.24+ x86-64manylinux: glibc 2.28+ x86-64

paddlecodec-0.2.0-cp310-cp310-manylinux_2_24_aarch64.manylinux_2_28_aarch64.whl (2.3 MB view details)

Uploaded CPython 3.10manylinux: glibc 2.24+ ARM64manylinux: glibc 2.28+ ARM64

File details

Details for the file paddlecodec-0.2.0-cp313-cp313-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for paddlecodec-0.2.0-cp313-cp313-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 9837654aa3ec4c77a96163e420bd92abaed4c251a1049b3d6a942015449ff088
MD5 6b9ecee3251c96a683f177621bed4181
BLAKE2b-256 9c6df0b1fb083b4794e45fdb8b387037a66bb76d34f551d3b76e00ca140546a9

See more details on using hashes here.

Provenance

The following attestation bundles were made for paddlecodec-0.2.0-cp313-cp313-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl:

Publisher: paddle_wheel.yaml on PFCCLab/paddlecodec

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file paddlecodec-0.2.0-cp313-cp313-manylinux_2_24_aarch64.manylinux_2_28_aarch64.whl.

File metadata

File hashes

Hashes for paddlecodec-0.2.0-cp313-cp313-manylinux_2_24_aarch64.manylinux_2_28_aarch64.whl
Algorithm Hash digest
SHA256 47602c477380a6983f355c81089d1c484c839efb6314161ccc71e0f733e2dfdf
MD5 0f0d777c7204ed215f19514a182f93c6
BLAKE2b-256 1c13d5ae13e946f96e32df2424cc4551365ebd5c581e33403ae2b8c8846f3f4a

See more details on using hashes here.

Provenance

The following attestation bundles were made for paddlecodec-0.2.0-cp313-cp313-manylinux_2_24_aarch64.manylinux_2_28_aarch64.whl:

Publisher: paddle_wheel.yaml on PFCCLab/paddlecodec

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file paddlecodec-0.2.0-cp312-cp312-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for paddlecodec-0.2.0-cp312-cp312-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 c52a7eb0223b75ed416cb327efc6b9a99ce688c10c605649e0db88470fd07325
MD5 260a3f9d09f66f24b46f8ea5ff3c493a
BLAKE2b-256 de8ccc2fed6068feedc359acfabf8af6b91d073da9a53404472d976567a5d701

See more details on using hashes here.

Provenance

The following attestation bundles were made for paddlecodec-0.2.0-cp312-cp312-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl:

Publisher: paddle_wheel.yaml on PFCCLab/paddlecodec

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file paddlecodec-0.2.0-cp312-cp312-manylinux_2_24_aarch64.manylinux_2_28_aarch64.whl.

File metadata

File hashes

Hashes for paddlecodec-0.2.0-cp312-cp312-manylinux_2_24_aarch64.manylinux_2_28_aarch64.whl
Algorithm Hash digest
SHA256 8c7ed47ee4b432f1c83e4d1e03593354e34a231561720b487b89d92281267a32
MD5 51b41f9ced801da3edb51ca03bf1ae93
BLAKE2b-256 d877d8dec8ba40188ec1303753fbf9b451d90cc1a0cacc906ce763850a3e38e3

See more details on using hashes here.

Provenance

The following attestation bundles were made for paddlecodec-0.2.0-cp312-cp312-manylinux_2_24_aarch64.manylinux_2_28_aarch64.whl:

Publisher: paddle_wheel.yaml on PFCCLab/paddlecodec

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file paddlecodec-0.2.0-cp311-cp311-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for paddlecodec-0.2.0-cp311-cp311-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 72cf35d611a94c463c42cc114874e058bad0740da4268fd858dc3e86896a3aa7
MD5 3cec9785904a7b3a5528ab966731ec78
BLAKE2b-256 ab6348d11c11bbfce330272e7e3cfac919681b95ef964e76e962fefbf90ab922

See more details on using hashes here.

Provenance

The following attestation bundles were made for paddlecodec-0.2.0-cp311-cp311-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl:

Publisher: paddle_wheel.yaml on PFCCLab/paddlecodec

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file paddlecodec-0.2.0-cp311-cp311-manylinux_2_24_aarch64.manylinux_2_28_aarch64.whl.

File metadata

File hashes

Hashes for paddlecodec-0.2.0-cp311-cp311-manylinux_2_24_aarch64.manylinux_2_28_aarch64.whl
Algorithm Hash digest
SHA256 73466babf2dcf4e82e51c7ec4a3f868c7b648c91691d102c714d8b022d2bdda2
MD5 aca286822f6c9458c3bdc70d7978f17e
BLAKE2b-256 0ad848c58c9f36bb22e074d19a6845cfcaf3907d0aaf9ae8956b45ca6639bd26

See more details on using hashes here.

Provenance

The following attestation bundles were made for paddlecodec-0.2.0-cp311-cp311-manylinux_2_24_aarch64.manylinux_2_28_aarch64.whl:

Publisher: paddle_wheel.yaml on PFCCLab/paddlecodec

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file paddlecodec-0.2.0-cp310-cp310-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for paddlecodec-0.2.0-cp310-cp310-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 c12c34a8a7eddcd14fd6ac39ffda9b0f40e31f59cf976863ad7f7445218fa1c1
MD5 45175eafca535e8bd617e3898478b07e
BLAKE2b-256 05a4a00d9b97728b2589bf21d016a41c87879a98b78255cf7b4b09c4e3eae85c

See more details on using hashes here.

Provenance

The following attestation bundles were made for paddlecodec-0.2.0-cp310-cp310-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl:

Publisher: paddle_wheel.yaml on PFCCLab/paddlecodec

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file paddlecodec-0.2.0-cp310-cp310-manylinux_2_24_aarch64.manylinux_2_28_aarch64.whl.

File metadata

File hashes

Hashes for paddlecodec-0.2.0-cp310-cp310-manylinux_2_24_aarch64.manylinux_2_28_aarch64.whl
Algorithm Hash digest
SHA256 90517e699ad8e4b8c3d687f866a16b5c7a368a7ca311d10ca751a6ee68f4f84c
MD5 79b477357c1586310c1477c7724ee7ce
BLAKE2b-256 fa7fb67405184572283d319b8e0f1bee2d81731ad85614f5fd71c3d11fdd85bb

See more details on using hashes here.

Provenance

The following attestation bundles were made for paddlecodec-0.2.0-cp310-cp310-manylinux_2_24_aarch64.manylinux_2_28_aarch64.whl:

Publisher: paddle_wheel.yaml on PFCCLab/paddlecodec

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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