import cv2 def frames_from_video(path, target_fps=None): cap = cv2.VideoCapture(path) orig_fps = cap.get(cv2.CAP_PROP_FPS) or 25.0 fps = target_fps or orig_fps or 25.0 step = max(1, int(round(orig_fps / fps))) if orig_fps else 1 idx = 0 while True: ret, frame = cap.read() if not ret: break if idx % step == 0: yield frame, fps idx += 1 cap.release()