Skip to main content

Video Processing

Learn how to capture video from files and cameras, process frames in real-time, and write processed video to disk.

Video Capture Basics

Capturing from Camera

Camera indices start at 0. If you have multiple cameras:
  • 0: Default camera (usually built-in webcam)
  • 1, 2, 3…: Additional cameras

Reading Video Files

Writing Video Files

Basic Video Writer

Common video codecs (FourCC codes):
  • 'mp4v': MPEG-4 (good compatibility)
  • 'XVID': Xvid codec
  • 'H264' or 'X264': H.264 (best compression)
  • 'MJPG': Motion JPEG (larger files, faster encoding)

Frame-by-Frame Processing

Edge Detection on Video

Based on OpenCV’s edge.py sample:

Video Processing Pipeline

Complete example with multiple processing steps:

Advanced Video Capture

For better performance with slow cameras:
When writing videos, ensure the frame size matches the size specified in VideoWriter. Mismatched sizes will result in errors or corrupted output.

Performance Tips

1

Reduce Resolution

Process at lower resolution for real-time applications, then upscale if needed.
2

Limit FPS

For non-real-time processing, you don’t need to match the original video FPS.
3

Use Efficient Codecs

H.264 provides best compression but slower encoding. MJPEG is faster but larger files.
4

Release Resources

Always call cap.release() and out.release() when done to free resources.

Next Steps