How to Get Video Metadata in Python Using OpenCV
Description: Learn how to get video file metadata in Python using OpenCV, including frame count, FPS, width, height, duration, and other useful video properties.
When working with video processing, computer vision, or video analysis applications, you often need basic information about a video file before processing its frames.
For example, you may need to know:
- How many frames does the video contain?
- What is the video’s FPS (frames per second)?
- What are the video width and height?
- How long is the video?
- Can the video file be opened successfully?
- What format or codec is being used?
Python and OpenCV make it easy to read many of these video properties. In this tutorial, we will build a reusable VideoReader class that can read video metadata and process a video frame-by-frame.
What Is Video Metadata?
Video metadata is information that describes the properties of a video file.
Some commonly required video properties include:
| Property | Description |
| Frame Count | Total number of frames in the video |
| FPS | Number of frames displayed per second |
| Width | Video width in pixels |
| Height | Video height in pixels |
| Duration | Total video duration |
| Codec | Encoding format used by the video |
| Format | Container format such as MP4 or AVI |
These properties are particularly useful when building video analysis, computer vision, AI, sports analysis, and video-processing applications.
Why Use OpenCV to Read Video Metadata?
OpenCV is a popular open-source computer vision library with Python support.
Its VideoCapture API allows you to open a video file and retrieve properties such as:
- FPS
- Frame count
- Frame width
- Frame height
- Video position
- FourCC codec information
- Frame-by-frame image data
The main class we need is: cv2.VideoCapture
Once a video is opened with VideoCapture, OpenCV provides the get() method for retrieving video properties.
Install OpenCV for Python
Before reading video metadata, install OpenCV using pip:
pip install opencv-python
You will also need NumPy if you want to work with the video frames:
pip install numpy
You can install both packages with:
pip install opencv-python numpy
let’s wait for next part.
