-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclub.py
92 lines (74 loc) · 2.63 KB
/
club.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
from picamera2.encoders import H264Encoder
from picamera2 import Picamera2
import time
import pyaudio
import wave
import datetime
from threading import Thread
import os
def record_audio(filename, duration):
RESPEAKER_RATE = 16000
RESPEAKER_CHANNELS = 1
RESPEAKER_WIDTH = 2
# run getDeviceInfo.py to get index
RESPEAKER_INDEX = 2 # refer to input device id
# CHUNK = 1024
CHUNK = 2048
RECORD_SECONDS = duration
WAVE_OUTPUT_FILENAME = "output0.wav"
p = pyaudio.PyAudio()
stream = p.open(
rate=RESPEAKER_RATE,
format=p.get_format_from_width(RESPEAKER_WIDTH),
channels=RESPEAKER_CHANNELS,
input=True,
input_device_index=RESPEAKER_INDEX,)
print("* recording")
frames = []
for i in range(0, int(RESPEAKER_RATE / CHUNK * RECORD_SECONDS)):
# data = stream.read(CHUNK)
try:
data = stream.read(CHUNK)
except IOError as e:
print(f"Error recording audio: {e}")
break
frames.append(data)
print("* done recording")
stream.stop_stream()
stream.close()
p.terminate()
wf = wave.open(WAVE_OUTPUT_FILENAME, 'wb')
wf.setnchannels(RESPEAKER_CHANNELS)
wf.setsampwidth(p.get_sample_size(p.get_format_from_width(RESPEAKER_WIDTH)))
wf.setframerate(RESPEAKER_RATE)
wf.writeframes(b''.join(frames))
wf.close()
def record_video(filename, duration):
picam2 = Picamera2()
video_config = picam2.create_video_configuration()
picam2.configure(video_config)
encoder = H264Encoder(bitrate=10000000)
output = "output0.h264"
picam2.start_recording(encoder, output)
time.sleep(duration)
picam2.stop_recording()
def main():
duration = 5 # seconds
timestamp = datetime.now().strftime("%Y:%m:%d_%H:%M:%S")
audio_filename = f"audio_{timestamp}.wav"
video_filename = f"video_{timestamp}.h264"
# Start recording audio and video simultaneously
audio_thread = Thread(target=record_audio, args=(audio_filename, duration))
video_thread = Thread(target=record_video, args=(video_filename, duration))
audio_thread.start()
video_thread.start()
audio_thread.join()
video_thread.join()
# Convert video to mp4
mp4_filename = f"video_{timestamp}.mp4"
command = f"ffmpeg -framerate 30 -i {video_filename} -c:v libx264 -preset slow -crf 22 {mp4_filename}"
# Check if ffmpeg command executed successfully
result = os.system(command)
if result != 0:
print("Error converting video to mp4")
print(f"Audio and video recording saved as {audio_filename} and {mp4_filename}")