-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAV_Recorder3.py
92 lines (71 loc) · 2.41 KB
/
AV_Recorder3.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
import time
import cv2
import speech_recognition as sr
import threading
import subprocess, os
class VideoRecorder:
def __init__(self):
self.vid_capture = None
self.output = []
self.video = None
self.read = True
def record(self):
self.vid_capture = cv2.VideoCapture(0)
# vid_cod = cv2.VideoWriter_fourcc(*'XVID')
# self.output = cv2.VideoWriter("./cam_video.mp4", vid_cod, 16.0, (640,480))
# time.sleep(1)
while(self.read):
ret, frame = self.vid_capture.read()
self.output.append(frame)
# if cv2.waitKey(1) &0XFF == ord('x'):
# break
def save_output(self):
vid_cod = cv2.VideoWriter_fourcc(*'mp4v')
height, width, layers = self.output[0].shape
self.video = cv2.VideoWriter("test3.mp4", vid_cod, 16.0, (width,height))
for image in self.output:
self.video.write(image)
self.video.release()
def start(self):
video_thread = threading.Thread(target=self.record)
video_thread.start()
def stop(self):
cv2.destroyAllWindows()
self.vid_capture.release()
# self.output.release()
class AudioRecorder:
def __init__(self):
self.r = sr.Recognizer()
self.audio = None
def record(self):
with sr.Microphone() as source:
print('Listening...')
self.r.adjust_for_ambient_noise(source, duration=1)
self.audio = self.r.listen(source)
def save_output(self):
with open('test3.wav','wb') as file:
file.write(self.audio.get_wav_data())
def start(self):
audio_thread = threading.Thread(target=self.record)
audio_thread.start()
def av_record():
video_thread = VideoRecorder()
audio_thread = AudioRecorder()
video_thread.start()
time.sleep(1.5)
audio_thread.record()
video_thread.read = False
video_thread.stop()
# removing previous files
if os.path.isfile('test3.wav'): os.remove('test3.wav')
if os.path.isfile('test3.mp4'): os.remove('test3.mp4')
if os.path.isfile('output3.mp4'): os.remove('output3.mp4')
video_thread.save_output()
audio_thread.save_output()
# merging audio and video
cmd = 'ffmpeg -i test3.mp4 -i test3.wav -c:v copy -c:a aac output3.mp4'
subprocess.call(cmd,shell=True)
def main():
av_record()
if __name__ =='__main__':
main()