Skip to main content

A video decoder for PyTorch

Project description

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

TorchCodec

TorchCodec is a Python package with a goal to provide useful and fast APIs to decode video frames to PyTorch Tensors.

[!NOTE] ⚠️ TorchCodec is still in early development stage and some APIs may be updated in future versions without a deprecation cycle, depending on user feedback. If you have any suggestions or issues, please let us know by opening an issue!

Using TorchCodec

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

from torchcodec.decoders import SimpleVideoDecoder

decoder = SimpleVideoDecoder("path/to/video.mp4")

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

len(decoder)  # == decoder.metadata.num_frames!
# 250
decoder.metadata.average_fps  # Note: instantaneous fps can be higher or lower
# 25.0

# 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]


# Iterate over frames:
for frame in decoder:
    pass

# Indexing, with PTS and duration info
decoder.get_frame_at(len(decoder) - 1)
# Frame:
#   data (shape): torch.Size([3, 400, 640])
#   pts_seconds: 9.960000038146973
#   duration_seconds: 0.03999999910593033

decoder.get_frames_at(start=10, stop=30, step=5)
# FrameBatch:
#   data (shape): torch.Size([4, 3, 400, 640])
#   pts_seconds: tensor([0.4000, 0.6000, 0.8000, 1.0000])
#   duration_seconds: tensor([0.0400, 0.0400, 0.0400, 0.0400])

# Time-based indexing with PTS and duration info
decoder.get_frame_displayed_at(pts_seconds=2)
# Frame:
#   data (shape): torch.Size([3, 400, 640])
#   pts_seconds: 2.0
#   duration_seconds: 0.03999999910593033

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

We'll be providing wheels in the coming days so that you can just install torchcodec using pip. For now, you can just build from source. You will need the following dependencies:

  • A C++ compiler+linker. This is typically available on a baseline Linux installation already.
  • cmake
  • pkg-config
  • FFmpeg
  • PyTorch nightly

Start by installing PyTorch following the official instructions.

Then, the easiest way to install the rest of the dependencies is to run:

conda install cmake pkg-config ffmpeg -c conda-forge

To clone and install the repo, run:

git clone git@github.com:pytorch/torchcodec.git
# Or, using https instead of ssh: git clone https://github.com/pytorch/torchcodec.git
cd torchcodec

pip install -e ".[dev]" --no-build-isolation -vv

TorchCodec supports all major FFmpeg version in [4, 7].

Planned future work

We are actively working on the following features:

Let us know if you have any feature requests by opening an issue!

Contributing

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

License

TorchCodec is released under the BSD 3 license.

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

TorchCodec-0.0.1-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (633.7 kB view details)

Uploaded CPython 3.12 manylinux: glibc 2.17+ x86-64

TorchCodec-0.0.1-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (635.1 kB view details)

Uploaded CPython 3.11 manylinux: glibc 2.17+ x86-64

TorchCodec-0.0.1-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (631.5 kB view details)

Uploaded CPython 3.10 manylinux: glibc 2.17+ x86-64

TorchCodec-0.0.1-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (631.9 kB view details)

Uploaded CPython 3.9 manylinux: glibc 2.17+ x86-64

File details

Details for the file TorchCodec-0.0.1-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for TorchCodec-0.0.1-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 cb934d9b38a32c18eab759c1d331025137a40995e3e15c6591312bc10a332aba
MD5 526ddac449d6451b16dd23ed57124596
BLAKE2b-256 0b0de10ba6b4ff7a9b76b7e77a5e8b879a4ede17fe8734809d21c2f22f9676a8

See more details on using hashes here.

File details

Details for the file TorchCodec-0.0.1-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for TorchCodec-0.0.1-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 6b41083d40445674ac295ee77280eff90f29789a63e1220bdfd65c54bc993899
MD5 16b766134cd8f692028925e3be283d41
BLAKE2b-256 13fd16e786f2d70804075d800d9eafe9286428411a6dc8cb6f221c120356913f

See more details on using hashes here.

File details

Details for the file TorchCodec-0.0.1-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for TorchCodec-0.0.1-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 83137994e1d83d9547d0fa428d6ccb15d577fd0247b2066a28191eb05784b9b3
MD5 6a6fb7a52dca16f101b8827ea6f5163a
BLAKE2b-256 036433428f4fb0c13e4e5a99ec93b9773adea8981955a3a123dd618d154111d0

See more details on using hashes here.

File details

Details for the file TorchCodec-0.0.1-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for TorchCodec-0.0.1-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 0c88cfecb9bee0d9b2fb973e84b86389dce9a5375461dbf4d2cedee97365c17f
MD5 9ee59bb1986d719e8ffb6722065dd1b6
BLAKE2b-256 02cbccfb509371460206a0ef5bb1a1f1884ce4a6cc21ede95a81cc8413a8b05e

See more details on using hashes here.

Supported by

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