veditor.utils.video_utils module

veditor.utils.video_utils.createVideoWritor(H: int, W: int, fps: float, codec: str = 'avc1', out_path: Optional[str] = None)Tuple[cv2.VideoWriter, str][source]

Create an instance of cv2.VideoWritor.

Parameters
  • H (int) – Height of the output video.

  • W (int) – Width of the output video.

  • fps (float) – Frame rate of the output video.

  • codec (str, optional) – Video codec for the output video. Defaults to "avc1".

  • out_path (Optional[str], optional) – [description]. Defaults to None.

Returns

Tuple of cv2.VideoWriter and path to output video.

Return type

Tuple[cv2.VideoWriter, str]

veditor.utils.video_utils.capture2writor(cap: cv2.VideoCapture, out_path: Optional[str] = None, codec: str = 'avc1', H: Optional[int] = None, W: Optional[int] = None, fps: Optional[float] = None)Tuple[cv2.VideoWriter, str][source]

Create a suitable cv2.VideoWriter for input cv2.VideoCapture.

Parameters
  • cap (cv2.VideoCapture) – An instance of cv2.VideoCaputure.

  • out_path (Optional[str], optional) – Path to the output video. Defaults to None.

  • codec (str, optional) – Video codec for the output video. Defaults to "avc1".

  • H (Optional[int], optional) – Height of the output video. Defaults to None.

  • W (Optional[int], optional) – Width of the output video. Defaults to None.

  • fps (Optional[float], optional) – Frame rate of the output video. Defaults to None.

Returns

Tuple of cv2.VideoWriter and path to output video.

Return type

Tuple[str,cv2.VideoWriter]

Examples

>>> import cv2
>>> from veditor.utils import capture2writor, SampleData
>>> cap = cv2.VideoCapture(SampleData().VIDEO_PATH)
>>> out, out_path = capture2writor(cap)
>>> isinstance(out, cv2.VideoWriter) and isinstance(out_path, str)
True
veditor.utils.video_utils.show_frames(video: Union[str, cv2.VideoCapture], start: int = 0, end: Optional[int] = None, step: int = 1, ncols: int = 6, nframes: Optional[int] = None, figsize: Optional[Tuple[int, int]] = None, fig: Optional[matplotlib.figure.Figure] = None)matplotlib.figure.Figure[source]

Cut out frames from the video and plot them.

Parameters
  • video (Union[str, cv2.VideoCapture]) – Path to video or an instance of cv2.VideoCaputure.

  • start (int, optional) – Draw subsequent frames from start. Defaults to 0.

  • end (Optional[int], optional) – Draw up to end-th frame. If not specified, draw to the end. Defaults to None.

  • ncols (int, optional) – Number of images lined up side by side (number of columns). Defaults to 6.

  • figsize (Optional[Tuple[int, int]], optional) – Size of one image. Defaults to None.

  • fig (Optional[Figure], optional) – Figure instance you want to draw in. Defaults to None.

Returns

Figure where frames from start to end are drawn.

Return type

Figure

Raises

ValueError – When video is cv2.VideoCapture and is not Opened.

>>> from veditor.utils import show_frames, SampleData
>>> fig = show_frames(video=SampleData().VIDEO_PATH, step=300, ncols=2)
>>> fig.show()
_images/veditor-utils-video_utils-1.png
veditor.utils.video_utils.save_frames(video: Union[str, cv2.VideoCapture], positions: Union[int, List[int]], fmt: str = '{pos}.png')None[source]

Cut out frames from the video and save them.

Parameters
  • video (Union[str, cv2.VideoCapture]) – Path to video or an instance of cv2.VideoCaputure.

  • positions (Union[int, List[int]]) – Which position(s) to save the frame.

  • fmt (str, optional) – File name format. Must include "{pos}". Defaults to "{pos}.png".

Raises

ValueError – When video is cv2.VideoCapture and is not Opened, or fmt DO NOT include "{pos}".

veditor.utils.video_utils.vcodec2ext(*codec)str[source]

Convert video codec to video extension.

Parameters

codec (Union[tuple, str]) – Video Codec.

Returns

Ideal file extension.

Return type

str

Examples

>>> from pycharmers.opencv import vcodec2ext
>>> vcodec2ext("MP4V")
'.mp4'
>>> vcodec2ext("mp4v")
'.mov'
>>> vcodec2ext("VP80")
'.webm'
>>> vcodec2ext("XVID")
'.avi
>>> vcodec2ext("☺️")
'.mp4'
Raises

KeyError – When the file extension cannot be inferred from the video codec.