Sub topic 1
What a Video Is: Frames, Frame Rate, and Codecs
Prerequisites
This chapter assumes that you can write basic Python loops and functions, understand NumPy array shapes, and know that a video is a sequence of images played over time. You should also have opencv-python installed.
The playback model
A digital video is a sequence of still images called frames. A player displays these frames quickly enough that your eyes perceive continuous motion. OpenCV usually gives your program one decoded frame at a time.
A frame has a position in the sequence. The first frame has index 0, the next has index 1, and so on. A frame also has a width, height, and number of color channels. For example, a color frame with a width of 1280 pixels and a height of 720 pixels has a shape similar to (720, 1280, 3).
The frame rate is the number of frames displayed each second. It is measured in frames per second, or FPS.
| Frame rate | Time represented by one frame | Common use |
|---|---|---|
| 24 FPS | About 41.7 milliseconds | Film-like playback |
| 30 FPS | About 33.3 milliseconds | General video |
| 60 FPS | About 16.7 milliseconds | Smooth motion or games |
If a file contains 900 frames recorded at 30 FPS, its expected duration is about 30 seconds:
duration = frame_count / frames_per_second
= 900 / 30
= 30 secondsThis relationship is useful, but metadata is not always exact. Variable-frame-rate files, damaged files, and some camera backends may report an FPS of zero or provide an estimate.
Containers and codecs
A video file usually has two important layers. A container, such as MP4, MKV, or AVI, organizes video data and may also hold audio, subtitles, and metadata. A codec is the method used to compress and decompress the video stream. H.264, H.265, and MPEG-4 are examples of video codecs.
The filename extension identifies the container more often than the codec. An .mp4 file can contain streams encoded with different codecs. Your operating system or OpenCV backend must have support for the selected codec to decode the file.
Compression reduces file size by storing changes between frames and by discarding details that are difficult for viewers to notice. This means a decoded frame is not the compressed data itself. OpenCV asks the backend to decode the compressed stream and returns an image array.
import cv2
cap = cv2.VideoCapture("input.mp4")
if not cap.isOpened():
raise RuntimeError("Could not open input.mp4")
width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
fps = cap.get(cv2.CAP_PROP_FPS)
count = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
print(f"size: {width} x {height}")
print(f"reported FPS: {fps}")
print(f"reported frames: {count}")
cap.release()The values returned by get describe what the backend reports. They are not promises that every source behaves perfectly. A live camera may not know its final frame count, and a camera's reported FPS can differ from the rate your program actually receives.
Why this matters for processing
Processing video means repeating an operation for each frame. A filter that takes 10 milliseconds per frame can theoretically process about 100 frames per second. A filter that takes 50 milliseconds per frame can process about 20 frames per second.
For a recorded file, slower processing usually means the program takes longer to finish. For a live camera, slow processing creates a different problem: incoming frames continue to arrive. The program may build a backlog, show old frames, or drop frames, depending on the backend and buffering settings.
The useful rate is therefore not only the source FPS. It is also your processing throughput, measured in frames per second. Later chapters will use timing measurements to compare these rates.
Exercise
A video reports 25 FPS and contains 500 frames. What playback duration does that represent, and what does FPS mean?
Show answer
It represents about 20 seconds because 500 / 25 = 20. FPS means the number of frames intended to be displayed each second.