Skip to content

Instantly share code, notes, and snippets.

@Hanlin-Dong
Created July 20, 2021 13:04
Show Gist options
  • Save Hanlin-Dong/a4ad8fceeeb651faab4fe04b3c902262 to your computer and use it in GitHub Desktop.
Save Hanlin-Dong/a4ad8fceeeb651faab4fe04b3c902262 to your computer and use it in GitHub Desktop.
Capture a video into image every N seconds
import cv2
from os import mkdir
def capture(name, interval):
"""Capture a video into a series of pictures
Args:
name (str): The file name of the video
interval (float): The time interval to split the video, in seconds.
"""
video = cv2.VideoCapture(f"{name}")
fps = video.get(5)
every = int(interval * fps)
folder = name.replace(".", "_")
mkdir(folder)
success, frame = video.read()
i, j = 0, 0
while success:
i += 1
if (i % every == 0):
j += 1
cv2.imwrite(f"{folder}/{j}.jpg", frame)
success, frame = video.read()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment