Decord Video Loader

0.6.0 · active · verified Thu Apr 09

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.

Warnings

Install

Imports

Quickstart

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.")

view raw JSON →