Video files and cameras

OpenCV · Practical · 12 min

Video is just a stream of images, and most real-time vision apps are a loop over VideoCapture frames.

cv2.VideoCapture(source) opens a video file, an image sequence pattern, a stream URL (RTSP, HTTP) or a camera by index (0 is the default webcam). Check cap.isOpened(), then loop:

while True:
    ok, frame = cap.read()
    if not ok:
        break

read() returns (False, None) at the end of a file or when a camera drops, so always check ok. Each frame is a normal BGR array.

cap.get(cv2.CAP_PROP_FPS), CAP_PROP_FRAME_COUNT, CAP_PROP_FRAME_WIDTH and HEIGHT report properties; cap.set(...) requests camera settings, which the driver may ignore. Frame counts from containers are sometimes estimates. Call cap.release() when done.

cv2.VideoWriter(path, fourcc, fps, (width, height)) writes frames. The codec is a four-character code such as cv2.VideoWriter_fourcc(*"mp4v") for .mp4 or *"MJPG" for .avi. Every frame you write must exactly match the declared size and be 3-channel unless you pass isColor=False; mismatched frames are silently dropped, leaving an empty file. Available codecs depend on how OpenCV was built (FFmpeg, GStreamer, platform APIs).

For a live preview, cv2.imshow plus cv2.waitKey(1) in the loop gives about real-time display and lets you exit on a key.

Example

import cv2
import numpy as np

w, h, fps = 160, 120, 10
out = cv2.VideoWriter("demo.avi", cv2.VideoWriter_fourcc(*"MJPG"), fps, (w, h))
for i in range(30):
    frame = np.zeros((h, w, 3), np.uint8)
    cv2.circle(frame, (5 * i, 60), 10, (0, 255, 255), -1)   # moving dot
    out.write(frame)
out.release()

cap = cv2.VideoCapture("demo.avi")
print(cap.isOpened(), cap.get(cv2.CAP_PROP_FPS))   # True 10.0
n = 0
while True:
    ok, frame = cap.read()
    if not ok:
        break
    n += 1
cap.release()
print(n, frame is None)                            # 30 True

Remember

  • Loop ok, frame = cap.read() and stop when ok is False
  • VideoCapture opens files, URLs and cameras by index
  • VideoWriter needs a fourcc, fps and the exact (width, height) of every frame
  • Release captures and writers when done

Watch out for

  • Writing frames whose size differs from the VideoWriter's declared size, which produces an empty video without any error.

Check yourself

  1. Your VideoWriter output file is tiny and won't play, yet no error was raised. What is the most likely cause?

Sign in to answer, see the explanations, and get this lesson back as a quick review later.

Official docs: docs.opencv.org/4.x/dd/d43/tutorial_py_video_display.html