Registry / ai-ml / decord

decord

JSON →
library0.6.0pypypi✓ verified 25d ago

Decord is an efficient and user-friendly Python library for loading and processing video data. It provides fast, GPU-accelerated video frame access, leveraging underlying decoders like FFmpeg and NVIDIA's NVDEC. Decord supports various video formats, integrates seamlessly with deep learning frameworks like PyTorch and TensorFlow, and offers features such as random frame access and batch reading. The current version, 0.6.0, includes stability fixes and introduces an `AudioReader` for audio frame access within its wheels. It maintains an active development pace with frequent bug fixes and performance improvements.

pip install decord
INSTALL
IMPORT
SIG · DECORD
D
decord
ai-mlpythonv0.6.0
Install
4.1s avg
Import
254ms
Disk
125MB
Pass rate
5/ 10
Env Coverage5 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v0.6.0 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
py 3.103.95 runs
build_error
glibc
py 3.103.95 runs
installs and imports cleanly · install 4.1s · import 0.254s · 123MB
125MB installed
● package 125MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

VideoReader
from decord import VideoReader
AudioReader
from decord import AudioReader
Requires Decord v0.6.0 or newer.
cpu
from decord import cpu
from decord.cpu import cpu
Context functions (`cpu`, `gpu`) are directly under the `decord` package.
gpu
from decord import gpu
from decord.gpu import gpu
Context functions (`cpu`, `gpu`) are directly under the `decord` package.
DECORD_LOG_STREAM
from decord import DECORD_LOG_STREAM
Used for configuring Decord's internal logging level.

This quickstart demonstrates how to initialize `VideoReader` and `AudioReader` (v0.6.0+), access video frames by index, retrieve batches of frames, and iterate through frames. It uses `cpu(0)` for context, but `gpu(0)` can be used if a CUDA-enabled Decord build is installed and a GPU is available. Remember to replace `path/to/your/video.mp4` with a real video file.

import os from decord import VideoReader, cpu, gpu, AudioReader from decord import DECORD_LOG_STREAM, logging # Configure Decord's logging level to suppress verbose FFmpeg messages DECORD_LOG_STREAM.set_level(logging.ERROR) # --- Quickstart requires a video file --- # Replace 'path/to/your/video.mp4' with an actual video file path. # You can also set this via an environment variable for easier testing: # export DECORD_VIDEO_PATH="/path/to/your/video.mp4" video_file_path = os.environ.get('DECORD_VIDEO_PATH', 'path/to/your/video.mp4') if not os.path.exists(video_file_path): print(f"WARNING: Video file not found at '{video_file_path}'. Please provide a valid path.") print("Skipping Decord quickstart execution.") else: try: print(f"Processing video: {video_file_path}") # 1. Initialize VideoReader with CPU context # Use gpu(0) for GPU context if available and Decord is built with CUDA support. vr = VideoReader(video_file_path, ctx=cpu(0)) # Get video properties print(f"Total frames: {len(vr)}") print(f"Frame rate: {vr.get_avg_fps()} fps") print(f"Video resolution: {vr.width}x{vr.height}") # Read a single frame (e.g., the first frame) frame_0 = vr[0].asnumpy() # Returns a NumPy array (H, W, C) in RGB print(f"Shape of first frame (NumPy): {frame_0.shape}") # Read a batch of frames (e.g., frames at index 10, 20, 30) if len(vr) > 30: frames_batch = vr.get_batch([10, 20, 30]).asnumpy() # Returns (N, H, W, C) print(f"Shape of batch frames (NumPy): {frames_batch.shape}") else: print("Video too short to get a batch of frames.") # Iterate through frames (e.g., first 5 frames) print("\nIterating through first 5 frames:") for i, frame in enumerate(vr): if i >= 5: break print(f" Frame {i} shape: {frame.shape}") # 2. Initialize AudioReader (available from Decord v0.6.0+) # Requires the video file to contain audio tracks. if hasattr(decord, 'AudioReader'): try: ar = AudioReader(video_file_path, ctx=cpu(0)) print(f"\nTotal audio samples (frames): {len(ar)}") # Read a chunk of audio samples (e.g., first 1024 samples) audio_chunk = ar[:1024].asnumpy() # Returns (Samples, Channels) print(f"Shape of first audio chunk (NumPy): {audio_chunk.shape}") except Exception as e: print(f"Could not initialize AudioReader: {e}. Ensure video has audio tracks.") else: print("\nAudioReader not available in this Decord version (requires v0.6.0+).") except Exception as e: print(f"An error occurred during Decord processing: {e}") print("Common issues include corrupted video files, missing FFmpeg dependencies, or incorrect Decord installation.")
Debug
Known issues
gotchaBoth `VideoReader` and `AudioReader` require an explicit context (e.g., `ctx=cpu(0)` or `ctx=gpu(0)`) during initialization. Forgetting this will result in a `RuntimeError`.
fix
Always pass a context object like `ctx=cpu(0)` or `ctx=gpu(0)` to the `VideoReader` or `AudioReader` constructor.
affects: All versions
gotchaWhile `pip install decord` often includes GPU-enabled wheels, actual GPU acceleration depends on your system's CUDA setup and if a compatible wheel was installed. If `gpu(0)` fails, you might need to build Decord from source for your specific CUDA version or verify GPU support is compiled into your installed package.
fix
Check `decord` installation logs for CUDA support. If issues persist, consider installing a specific `decord-gpu` variant if available, or follow instructions for building from source for your CUDA version.
affects: All versions
deprecatedVersions of Decord prior to `v0.5.0` had known performance issues (e.g., slow frame access) and could hang or crash when processing broken or malformed video files. It is highly recommended to upgrade to `v0.5.0` or newer for improved stability and performance.
fix
Upgrade to the latest `decord` version: `pip install --upgrade decord`.
affects: <0.5.0
gotcha`AudioReader` was introduced in Decord `v0.6.0`. Attempting to import or use `AudioReader` in older versions will result in an `ImportError` or `AttributeError`.
fix
Ensure you are using Decord `v0.6.0` or newer to use `AudioReader` functionality. Upgrade using `pip install --upgrade decord`.
affects: <0.6.0
breakingSeveral patch versions in `v0.5.x` and `v0.6.0` addressed issues with PyPI wheels (e.g., incorrect RECORD files leading to broken `pip` installations). Users might experience `pip` errors during installation or corrupted packages after installation. A clean reinstall might be needed.
fix
If installation issues occur, try `pip uninstall decord` and then `pip install --upgrade decord` to ensure the latest fixed wheel (if available) is installed. Consult the GitHub repository's issues for specific workarounds if problems persist on your platform.
affects: 0.5.1, 0.5.2, 0.6.0
Errors
Common errors & fixes
ModuleNotFoundError: No module named 'decord'
The 'decord' Python package is not installed in the active Python environment or the environment where the code is being run.
fix
Install the decord library using pip: `pip install decord`.
ERROR: Could not find a version that satisfies the requirement decord (from versions: none) / ERROR: No matching distribution found for decord / Target Platform is not supported yet
Decord could not be installed via `pip` because a pre-built wheel is not available for your specific operating system, Python version, or architecture (e.g., ARM-based systems like Jetson Nano or certain macOS versions).
fix
Try installing a specific version of decord (e.g., `pip install decord==0.6.0`), ensure you have a compatible Python version, or build decord from source, often requiring FFmpeg development libraries and a C++ compiler. For specific platforms like Jetson Nano, building from source is usually required.
decord._ffi.base.DECORDError: ... ERROR opening: [video_path] ... Protocol not found / Check failed: st_nb >= 0 ... ERROR cannot find video stream with wanted index: -1
Decord failed to open or decode the video file, often due to a corrupted video, an unsupported video format/codec, missing FFmpeg protocols (e.g., for HTTPS URLs), or issues detecting video streams within the file.
fix
Verify the video file's integrity and format, ensure FFmpeg is correctly installed and supports the necessary protocols/codecs, or try using a local video file instead of a URL. Some users report issues with specific codecs like AV1.
RuntimeError: Cannot find the files. List of candidates: ...libdecord.dylib (or .dll/.so)
The underlying native shared libraries (`libdecord.dylib` on macOS, `.dll` on Windows, `.so` on Linux) required by decord's Python bindings cannot be located at runtime, often occurring when packaging with tools like PyInstaller or in environments where the library paths are not correctly configured.
fix
Manually copy the `libdecord` shared library (and its dependencies like FFmpeg libraries) from your Python environment's `site-packages/decord` directory to a location where your application can find them (e.g., alongside the executable or within system library paths). For PyInstaller, specific hooks or `--add-binary` options might be needed.
Upgrade
Version history
0.6.0latest on PyPI · released Jun 14, 2021
Audit
Dependencies

No dependency data recorded yet.

Agent activity
56 hits · last 30 days
node
52
OpenAI (training)
1
Resources