Skip to content

Instantly share code, notes, and snippets.

@mattroz
Last active October 11, 2023 16:00
Show Gist options
  • Save mattroz/b6b8e881f5971d680905cb0c00bc4d21 to your computer and use it in GitHub Desktop.
Save mattroz/b6b8e881f5971d680905cb0c00bc4d21 to your computer and use it in GitHub Desktop.
opencv video reader/writer
import cv2
from pathlib import Path
path_to_video = Path("some/path/to/video.mp4")
video_handler = cv2.VideoCapture(str(path_to_video))
n_frames = int(video_handler.get(cv2.CAP_PROP_FRAME_COUNT))
w = int(video_handler.get(cv2.CAP_PROP_FRAME_WIDTH))
h = int(video_handler.get(cv2.CAP_PROP_FRAME_HEIGHT))
fps = video_handler.get(cv2.CAP_PROP_FPS)
fourcc = cv2.VideoWriter_fourcc(*'mp4v')
video_writer = cv2.VideoWriter(str(path_to_save_video), fourcc, fps, (w, h))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment