Video Module

The video module provides classes and functions for reading and writing video files with automatic color space conversion and enhanced error handling.

Video processing operations.

This module provides classes and functions for reading and writing video files with automatic color space conversion and enhanced error handling.

Classes:

VideoCapture: Read video files or capture from camera devices. VideoWriter: Write video files with customizable encoding parameters. VideoInterface: Base class for video operations.

Functions:

Video: Factory function to create VideoCapture or VideoWriter instances.

Aliases:

VideoReader: Alias for VideoCapture class.

class cv3.video.VideoCapture(src: Path | str | int)[source]

Bases: VideoInterface

Video capture class for reading video files or camera streams.

This class provides an enhanced interface for reading video files or capturing video from camera devices with automatic color space conversion and error handling.

stream

The underlying OpenCV VideoCapture stream.

width

Width of the video frames.

Type:

int

height

Height of the video frames.

Type:

int

frame_cnt

Total number of frames in the video.

Type:

int

fps

Frames per second of the video.

Type:

int

__init__(src: Path | str | int)[source]

Initialize a VideoCapture object.

Parameters:

src (Path, str, or int) – Source of the video. Can be a file path, directory path, or camera index (integer).

Raises:
  • IsADirectoryError – If src is a directory.

  • FileNotFoundError – If src is a file path that doesn’t exist.

  • OSError – If the video stream cannot be opened.

Examples

>>> # Read from a video file
>>> cap = VideoCapture('video.mp4')
>>>
>>> # Read from a camera (index 0)
>>> cap = VideoCapture(0)
>>>
>>> # Read from a camera (by string index)
>>> cap = VideoCapture('0')
property now

Current frame number in the video stream.

Raises:

OSError – If the video is closed.

Type:

int

read()[source]

Read the next frame from the video stream.

Returns:

The next video frame.

Return type:

numpy.ndarray

Raises:
  • OSError – If the video is closed.

  • StopIteration – If the video has finished.

__iter__()[source]

Return the iterator object (self).

__next__()[source]

Read the next frame from the video stream.

Returns:

The next video frame.

Return type:

numpy.ndarray

rewind(nframe)[source]

Rewind the video stream to a specific frame.

Parameters:

nframe (int) – Frame number to rewind to.

Returns:

This VideoCapture instance.

Return type:

VideoCapture

__len__()[source]

int: Total number of frames in the video.

__getitem__(idx)[source]

Read a specific frame from the video by index.

Parameters:

idx (int) – Frame index to read.

Returns:

The requested video frame.

Return type:

numpy.ndarray

imread()

Alias for read().

seek(nframe)

Alias for rewind().

class cv3.video.VideoWriter(save_path, fps=None, fourcc=None, mkdir=False)[source]

Bases: VideoInterface

Video writer class for creating video files.

This class provides an enhanced interface for writing video files with customizable encoding parameters and automatic color space conversion.

save_path

Path to the output video file.

Type:

str

width

Width of the video frames.

Type:

int

height

Height of the video frames.

Type:

int

fps

Frames per second of the output video.

Type:

int

fourcc

OpenCV FOURCC code for video codec.

stream

The underlying OpenCV VideoWriter stream.

__init__(save_path, fps=None, fourcc=None, mkdir=False)[source]

Initialize a VideoWriter object.

Parameters:
  • save_path (str or Path) – Path to the output video file.

  • fps (int, optional) – Frames per second. Defaults to opt.FPS.

  • fourcc (str or int, optional) – FOURCC code for video codec. Defaults to opt.FOURCC.

  • mkdir (bool, optional) – If True, create parent directories if they don’t exist. Defaults to False.

Examples

>>> # Create a video writer with default settings
>>> writer = VideoWriter('output.mp4')
>>>
>>> # Create a video writer with custom FPS
>>> writer = VideoWriter('output.mp4', fps=30)
>>>
>>> # Create a video writer with string FOURCC
>>> writer = VideoWriter('output.mp4', fourcc='mp4v')
>>>
>>> # Create a video writer with integer FOURCC
>>> import cv2
>>> writer = VideoWriter('output.mp4', fourcc=cv2.VideoWriter_fourcc(*'mp4v'))
isOpened()[source]

Check if the video writer stream is opened.

Returns:

True if the stream is opened, False otherwise.

Return type:

bool

write(frame: ndarray)[source]

Write a frame to the video file.

Parameters:

frame (numpy.ndarray) – Frame to write to the video.

Raises:
  • OSError – If the stream is closed.

  • AssertionError – If the frame shape doesn’t match the video dimensions.

imwrite(frame: ndarray)

Alias for write().

cv3.video.VideoReader

Alias for VideoCapture.

cv3.video.Video(path, mode='r', **kwds)[source]

Factory function to create VideoCapture or VideoWriter instances.

Parameters:
  • path (str or Path) – Path to the video file or camera index.

  • mode (str, optional) – Mode of operation. ‘r’ for reading, ‘w’ for writing. Defaults to ‘r’.

  • **kwds – Additional keyword arguments passed to VideoWriter (only in ‘w’ mode).

Returns:

VideoCapture instance if mode=’r’, VideoWriter instance if mode=’w’.

Return type:

VideoCapture or VideoWriter

Raises:

TypeError – If keyword arguments are passed in ‘r’ mode.

Examples

>>> # Create a video reading stream
>>> reader = Video('video.mp4', mode='r')
>>>
>>> # Create a video writing stream
>>> writer = Video('output.mp4', mode='w')
>>>
>>> # Create a video writing stream with custom parameters
>>> writer = Video('output.mp4', mode='w', fps=30, fourcc='mp4v')

Video(path[, mode])

Factory function to create VideoCapture or VideoWriter instances.

VideoInterface()

Base class for video operations.

VideoCapture(src)

Video capture class for reading video files or camera streams.

VideoWriter(save_path[, fps, fourcc, mkdir])

Video writer class for creating video files.

VideoReader

Alias for VideoCapture.