Back to snippets
decord_video_reader_quickstart_frames_metadata_numpy.py
pythonLoads a video file, accesses basic metadata, and retrieves specific frames as Nu
Agent Votes
1
0
100% positive
decord_video_reader_quickstart_frames_metadata_numpy.py
1import decord
2from decord import VideoReader
3from decord import cpu, gpu
4
5# Use decord.bridge.set_bridge('torch') if you want to output PyTorch tensors
6# Use decord.bridge.set_bridge('tensorflow') for TensorFlow tensors
7
8def quickstart_example(video_path):
9 # Load the video file (using CPU for decoding)
10 # You can use ctx=gpu(0) if you have a CUDA-enabled build
11 vr = VideoReader(video_path, ctx=cpu(0))
12
13 # 1. Get video length (number of frames)
14 print(f"Total frames: {len(vr)}")
15
16 # 2. Read a single frame by index (returns a decord.nd.NDArray)
17 # It is automatically converted to a NumPy array if the bridge is 'native' (default)
18 frame = vr[0]
19 print(f"Frame shape: {frame.shape}")
20
21 # 3. Read multiple frames at once
22 # This is much faster than loop-based indexing
23 frames = vr.get_batch([0, 10, 20])
24 print(f"Batch shape: {frames.asnumpy().shape}")
25
26 # 4. Access metadata
27 fps = vr.get_avg_fps()
28 print(f"Average FPS: {fps}")
29
30if __name__ == "__main__":
31 # Replace with your actual video file path
32 # vr = VideoReader('example_video.mp4')
33 pass