-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvideo_writer.py
More file actions
31 lines (27 loc) · 955 Bytes
/
Copy pathvideo_writer.py
File metadata and controls
31 lines (27 loc) · 955 Bytes
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
31
import cv2
from threading import Thread, Event
from queue import Queue, Empty
class VideoWriter:
def __init__(self, output_file, fourcc, fps, width, height):
self.writer = cv2.VideoWriter(output_file, fourcc, fps, (width, height))
self.stopped = Event()
self.queue = Queue(maxsize=1024)
self.thread = Thread(target=self._write, daemon=True)
self.thread.start()
def _write(self):
while not self.stopped.is_set():
try:
frames = self.queue.get(timeout=1)
if frames is None:
break
for frame in frames:
self.writer.write(frame)
except Empty:
continue
def write_batch(self, batch):
self.queue.put(batch)
def stop(self):
self.stopped.set()
self.queue.put(None) # Signal end of writing
self.thread.join()
self.writer.release()