cv3.video.VideoWriter

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'))
width = None
height = None
stream = None
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.

__enter__()

Enter the runtime context for the video stream.

Returns:

This video interface instance.

Return type:

VideoInterface

__exit__(exc_type, exc_val, exc_tb)

Exit the runtime context for the video stream.

This method ensures the video stream is released when exiting the context manager.

close()

Release the video stream and free associated resources.

Note

If the stream has not been started, a warning will be issued.

property is_opened

Check if the video stream is opened (property version).

Type:

bool

release()

Release the video stream and free associated resources.

Note

If the stream has not been started, a warning will be issued.

property shape

Shape of the video frames as (width, height).

Type:

tuple

imwrite(frame: ndarray)

Alias for write().