Skip to main content

Pythonic interface to Nvidia Codec

Project description

python-nvidia-codec

A Pythonic library for NVIDIA Video Codec (NVDEC).

The project is still in active development; expect breaking changes.

Requirements

  • Python 3.10+
  • NVIDIA GPU with NVDEC support
  • NVIDIA driver with libnvcuvid.so
  • FFmpeg 8.x (for demuxing)

Installation

pip install nvidia-codec

Or from source:

git clone https://github.com/riaqn/python-nvidia-codec.git
cd python-nvidia-codec
pip install -e .

Usage

Decode all frames

from nvidia_codec.utils import Player
import torch

# Simplest: decode at native resolution
player = Player('/path/to/video.mp4')

for timestamp, frame in player.frames(torch.uint8):
    # frame is a torch tensor on GPU (CHW format)
    print(f'{timestamp}: {frame.shape}')

Screenshot (single frame extraction)

from nvidia_codec.utils import Player
from datetime import timedelta
import torch

with Player('/path/to/video.mp4') as player:
    print(f'Duration: {player.duration}')

    # Get frame at 10 seconds (fast keyframe seek by default)
    timestamp, frame = player.screenshot(timedelta(seconds=10), torch.uint8)

    # For precise frame timing, use accurate=True (slower)
    timestamp, frame = player.screenshot(timedelta(seconds=10), torch.uint8, accurate=True)

Advanced: crop, scale, and letterbox in hardware

All post-processing is done by the NVDEC hardware during decoding — no extra GPU passes needed.

from nvidia_codec.utils import Player
import torch

player = Player(
    '/path/to/video.mp4',
    # Crop 100px from left/right, 50px from top/bottom
    cropping=lambda h, w: {'left': 100, 'top': 50, 'right': w - 100, 'bottom': h - 50},
    # Output buffer dimensions
    target_size=lambda h, w: (384, 384),
    # Place frame centered vertically; scaled to fit this rect, black bars outside
    target_rect=lambda h, w: {'left': 0, 'top': 36, 'right': 384, 'bottom': 348},
)

for timestamp, frame in player.frames(torch.float32):
    # frame is [3, 384, 384] with letterboxing
    process(frame)

Supported Codecs

Codec support depends on your GPU:

  • H.264 (AVC)
  • H.265 (HEVC)
  • VP9
  • AV1 (Ampere+)
  • VC1/WMV3 (older GPUs only, removed in Turing)

Why another Python library for NVIDIA Codec?

Comparison to Video-Processing-Framework (VPF)

Methodologies: VPF is written fully in C++ and uses pybind to expose Python interfaces. PNC is written fully in Python and uses ctypes to access NVIDIA C interfaces. Our code tends to be more concise, less duplicative and easier to read and write. It also allows better interoperability with other Python libraries.

Performance: Preliminary tests show little to no difference in performance, because the heavy lifting is done on the GPU anyway. Both libraries can saturate the GPU decoder. PNC uses more CPU than VPF as expected from Python vs. C++, but still negligible (less than 10% of Ryzen 3100 single core for 8K×4K HEVC).

Resource Management:

  • In VPF, Surface given to user is not owned by the user. It will be overwritten by new frames which is counter-intuitive. Picture is not exposed to user at all - they are always mapped (post-processed and copied) to Surface so the picture can be ready for new frames. The latter is inefficient when only a subset of Pictures are needed (e.g., screenshots).
  • VPF allocates the bare minimum of resources needed for most decoding tasks. PNC allows the user to specify the amount of resources to be allocated for advanced applications. Users own the resources and decide when and whether to deal with them.
  • Managing resources is not painful: similar to pycuda, we shift the burden of managing host/device resources to the Python garbage collector. Resources (such as Picture and Surface) are automatically freed when the user drops the reference.

Roadmap

  • Decoding
  • Color Conversion
    • Source Format: NV12, P016, YUV444, YUV444_16Bit
    • Target Format: RGB24, RGB48, RGB444P, RGB444P16
    • Color Ranges: MPEG (limited), JPEG (full)
    • Color Space: BT.601, BT.709, BT.2020
  • Built-in cropping and scaling via NV decoder
  • Thread-safe and thread-friendly
  • Encoder
  • PyTorch integration

Acknowledgements

  • Many thanks to @rarzumanyan for all the help and explanations!
  • The blog posts from myMusing are very helpful.

Project details


Download files

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

Source Distribution

nvidia_codec-0.0.17.tar.gz (45.1 kB view details)

Uploaded Source

Built Distribution

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

nvidia_codec-0.0.17-py3-none-any.whl (48.9 kB view details)

Uploaded Python 3

File details

Details for the file nvidia_codec-0.0.17.tar.gz.

File metadata

  • Download URL: nvidia_codec-0.0.17.tar.gz
  • Upload date:
  • Size: 45.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.3

File hashes

Hashes for nvidia_codec-0.0.17.tar.gz
Algorithm Hash digest
SHA256 ba07fd522534c15bbb251a9a8497863ed987ece8da27b25aecfdf3a2c62e6b5b
MD5 5027164566483a683f47d9d9781340d6
BLAKE2b-256 af22e91be81befafe68dfb0aace6bc73f63caa445254660a1b4a4bd7b99796c8

See more details on using hashes here.

File details

Details for the file nvidia_codec-0.0.17-py3-none-any.whl.

File metadata

  • Download URL: nvidia_codec-0.0.17-py3-none-any.whl
  • Upload date:
  • Size: 48.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.3

File hashes

Hashes for nvidia_codec-0.0.17-py3-none-any.whl
Algorithm Hash digest
SHA256 0b1f4d98dcf15712d3920eddad48d4f674d4d5832ce80668a160178a12f1abec
MD5 51a043d0b0d5f8446566e7b8247c6f52
BLAKE2b-256 d6aeae71dab4bc3908263a15f33c3cbd1802b6ceabb91c1a5971487b0113eefc

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