-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathrecord_video.py
More file actions
48 lines (36 loc) · 1.24 KB
/
record_video.py
File metadata and controls
48 lines (36 loc) · 1.24 KB
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
import cv2
def list_cameras():
index = 0
arr = []
while True:
cap = cv2.VideoCapture(index)
if not cap.isOpened():
break
else:
arr.append(index)
cap.release()
index += 1
return arr
def record_video(filename, fps=20, resolution=(640, 480)):
# 0,1 is recording from the camera
cap = cv2.VideoCapture(list_cameras()[-1])
# To set the resolution
# cap.set(cv2.CAP_PROP_FRAME_WIDTH, 640)
# cap.set(cv2.CAP_PROP_FRAME_HEIGHT, 480)
# FourCC is a 4-byte code used to specify the video codec.
fourcc = cv2.VideoWriter_fourcc(*'avc1') # H.264 Encoder. Compresses video to the H.264 format.
out = cv2.VideoWriter(filename, fourcc, fps, resolution)
while cap.isOpened():
ret, frame = cap.read() # Capture frame by frame
out.write(frame)
cv2.imshow('Original', frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cap.release() # Close the window / Release webcam
out.release() # After we release our webcam, we also release the output
# De-allocate any associated memory usage
cv2.destroyAllWindows()
def main():
record_video("output/nagranie.mp4")
if __name__ == "__main__":
main()