ขนาดวิดีโอ: 1280 X 720853 X 480640 X 360
แสดงแผงควบคุมโปรแกรมเล่น
เล่นอัตโนมัติ
เล่นใหม่
This video is very helpful for meThanks a lot buddy
import pyaudioimport keyboardimport timeimport wavechunk = 1024format = pyaudio.paInt16channels = 2rate = 44100Output_Filename = "Recorded.wav" p = pyaudio.PyAudio()stream = p.open(format=format, channels=channels, rate=rate, input=True, frames_per_buffer=chunk)frames = []print("Press SPACE to start recording")keyboard.wait('space')print("Recording... Press SPACE to stop.")time.sleep(0.2)while True: try: data = stream.read(chunk) frames.append(data) except KeyboardInterrupt: break if keyboard.is_pressed('space'): print("stopping recording") time.sleep(0.2) break stream.stop_stream()stream.close()p.terminate()wf = wave.open(Output_Filename, 'wb') wf.setnchannels(channels)wf.setsampwidth(p.get_sample_size(format))wf.setframerate(rate)wf.writeframes(b''.join(frames))wf.close()
Change chunk, channels and rate according to your choice :)
Thanks man!
Thanks for the handy run down! I do have a question though! Any particular reason why you use Visual Studio over VS Code? Just wondering if I should check it out
can someone write the above code so any one can copy and paste
#Properly working in Linux import pyaudioimport waveimport timefrom pynput import keyboardimport threadingchunk = 1024format = pyaudio.paInt16channels = 2rate = 44100Output_Filename = "Recorded.wav"p = pyaudio.PyAudio()stream = p.open(format=format, channels=channels, rate=rate, input=True, frames_per_buffer=chunk)frames = []recording = Falsestop_recording = Falsedef on_press(key): global recording, stop_recording if key == keyboard.Key.space: if not recording: print("Recording... Press SPACE to stop.") recording = True else: print("Stopping recording") stop_recording = True return False # Stop listenerdef start_listener(): with keyboard.Listener(on_press=on_press) as listener: listener.join()print("Press SPACE to start recording")start_listener()time.sleep(0.2)def record_audio(): global recording, stop_recording while recording and not stop_recording: data = stream.read(chunk) frames.append(data) stream.stop_stream() stream.close() p.terminate() wf = wave.open(Output_Filename, 'wb') wf.setnchannels(channels) wf.setsampwidth(p.get_sample_size(format)) wf.setframerate(rate) wf.writeframes(b''.join(frames)) wf.close() print("Recording saved to", Output_Filename)record_thread = threading.Thread(target=record_audio)record_thread.start()# Listen for the stop event in another threadwhile recording and not stop_recording: start_listener() time.sleep(0.1)record_thread.join()
This video is very helpful for me
Thanks a lot buddy
import pyaudio
import keyboard
import time
import wave
chunk = 1024
format = pyaudio.paInt16
channels = 2
rate = 44100
Output_Filename = "Recorded.wav"
p = pyaudio.PyAudio()
stream = p.open(format=format,
channels=channels,
rate=rate,
input=True,
frames_per_buffer=chunk)
frames = []
print("Press SPACE to start recording")
keyboard.wait('space')
print("Recording... Press SPACE to stop.")
time.sleep(0.2)
while True:
try:
data = stream.read(chunk)
frames.append(data)
except KeyboardInterrupt:
break
if keyboard.is_pressed('space'):
print("stopping recording")
time.sleep(0.2)
break
stream.stop_stream()
stream.close()
p.terminate()
wf = wave.open(Output_Filename, 'wb')
wf.setnchannels(channels)
wf.setsampwidth(p.get_sample_size(format))
wf.setframerate(rate)
wf.writeframes(b''.join(frames))
wf.close()
Change chunk, channels and rate according to your choice :)
Thanks man!
Thanks for the handy run down!
I do have a question though! Any particular reason why you use Visual Studio over VS Code? Just wondering if I should check it out
can someone write the above code so any one can copy and paste
#Properly working in Linux
import pyaudio
import wave
import time
from pynput import keyboard
import threading
chunk = 1024
format = pyaudio.paInt16
channels = 2
rate = 44100
Output_Filename = "Recorded.wav"
p = pyaudio.PyAudio()
stream = p.open(format=format,
channels=channels,
rate=rate,
input=True,
frames_per_buffer=chunk)
frames = []
recording = False
stop_recording = False
def on_press(key):
global recording, stop_recording
if key == keyboard.Key.space:
if not recording:
print("Recording... Press SPACE to stop.")
recording = True
else:
print("Stopping recording")
stop_recording = True
return False # Stop listener
def start_listener():
with keyboard.Listener(on_press=on_press) as listener:
listener.join()
print("Press SPACE to start recording")
start_listener()
time.sleep(0.2)
def record_audio():
global recording, stop_recording
while recording and not stop_recording:
data = stream.read(chunk)
frames.append(data)
stream.stop_stream()
stream.close()
p.terminate()
wf = wave.open(Output_Filename, 'wb')
wf.setnchannels(channels)
wf.setsampwidth(p.get_sample_size(format))
wf.setframerate(rate)
wf.writeframes(b''.join(frames))
wf.close()
print("Recording saved to", Output_Filename)
record_thread = threading.Thread(target=record_audio)
record_thread.start()
# Listen for the stop event in another thread
while recording and not stop_recording:
start_listener()
time.sleep(0.1)
record_thread.join()