File size: 703 Bytes
98a3af2
 
9482f97
98a3af2
 
 
 
 
 
 
 
 
 
 
 
 
 
9482f97
98a3af2
 
 
 
 
9482f97
98a3af2
 
9482f97
98a3af2
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
import cv2


def frame_generator(video_path, stride=1):
    cap = cv2.VideoCapture(video_path)
    if not cap.isOpened():
        raise FileNotFoundError(f"Cannot open video: {video_path}")
    i = 0
    while True:
        ok, frame = cap.read()
        if not ok:
            break
        if i % stride == 0:
            yield frame
        i += 1
    cap.release()


class VideoWriter:
    def __init__(self, out_path, fps, width, height, fourcc="mp4v"):
        self._writer = cv2.VideoWriter(
            out_path, cv2.VideoWriter_fourcc(*fourcc), fps, (width, height)
        )

    def write(self, frame):
        self._writer.write(frame)

    def release(self):
        self._writer.release()