-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrecord_cams.py
71 lines (62 loc) · 2.45 KB
/
record_cams.py
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
import wave
import pyaudio
import cv2
import threading
import time
class camThread(threading.Thread):
def __init__(self, previewName, camID, frame_width=1920, frame_height=1080, fps=30):
threading.Thread.__init__(self)
self.previewName = previewName
self.camID = camID
self.frame_width = frame_width
self.frame_height = frame_height
self.fps = fps
self.audio_FORMAT = pyaudio.paInt16
self.audio_CHANNELS = 2
self.audio_RATE = 44100
self.audio_CHUNK = 1024
self.audio_RECORD_SECONDS = 5
self.WAVE_OUTPUT_FILENAME = "file.wav"
def run(self):
print("Recording {}".format(self.previewName))
record_vid(self.previewName, self.camID, self.frame_width, self.frame_height, self.fps)
def record_vid(self, frame_width, frame_height, fps):
p = pyaudio.PyAudio()
audio_stream = p.open(format = self.audio_FORMAT,
channels = self.audio_CHANNELS,
rate = self.audio_RATE,
input = True,
frames_per_buffer = self.audio_CHUNK,
input_device_index = self.camID)
audio = []
out = cv2.VideoWriter('{}_{}.avi'.format(self.previewName,time.strftime("%Y%m%d-%H%M%S")),cv2.VideoWriter_fourcc('M','J','P','G'), fps, (self.frame_width, self.frame_height))
cam = cv2.VideoCapture(self.camID)
audio_stream.start_stream()
if cam.isOpened(): # try to get the first frame
rval, frame = cam.read()
audio_signal = audio_stream.read(self.audio_CHUNK)
else:
rval = False
print("Video is not being captured.")
while rval:
out.write(frame)
audio.append(audio_signal)
rval, frame = cam.read()
audio_signal = audio_stream.read(self.audio_CHUNK)
key = cv2.waitKey(20)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cam.release()
out.release()
audio_stream.close()
wf = wave.open(self.WAVE_OUTPUT_FILENAME, 'wb')
wf.setnchannels(self.audio_CHANNELS)
wf.setsampwidth(p.get_sample_size(self.audio_FORMAT))
wf.setframerate(self.audio_RATE)
wf.writeframes(b''.join(audio))
wf.close()
# Create two threads as follows
thread1 = camThread("Camera 1", 0)
thread2 = camThread("Camera 2", 1)
thread1.start()
thread2.start()