-
Notifications
You must be signed in to change notification settings - Fork 2
/
mic.py
54 lines (43 loc) · 1.38 KB
/
mic.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
import pyaudio
import wave
from pydub import AudioSegment
import os
def record_audio(filename, duration=10):
CHUNK = 1024
FORMAT = pyaudio.paInt16
CHANNELS = 1
RATE = 44100
RECORD_SECONDS = duration
p = pyaudio.PyAudio()
stream = p.open(format=FORMAT,
channels=CHANNELS,
rate=RATE,
input=True,
frames_per_buffer=CHUNK)
print("* Recording audio...")
frames = []
for i in range(0, int(RATE / CHUNK * RECORD_SECONDS)):
data = stream.read(CHUNK)
frames.append(data)
print("* Recording finished")
stream.stop_stream()
stream.close()
p.terminate()
wf = wave.open(f"{filename}.wav", 'wb')
wf.setnchannels(CHANNELS)
wf.setsampwidth(p.get_sample_size(FORMAT))
wf.setframerate(RATE)
wf.writeframes(b''.join(frames))
wf.close()
convert_to_mp3(f"{filename}.wav", f"{filename}.mp3")
def convert_to_mp3(input_filename, output_filename):
sound = AudioSegment.from_wav(input_filename)
sound.export(output_filename, format="mp3")
os.remove(input_filename)
if __name__ == "__main__":
filename = "recorded_audio.wav"
output_filename = "recorded_audio.mp3"
duration = 10
record_audio(filename, duration)
# convert_to_mp3(filename, output_filename)
print(f"Audio recorded and saved as {output_filename}")