File size: 436 Bytes
98b1db7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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()