How to Record Audio in Python

แชร์
ฝัง
  • เผยแพร่เมื่อ 28 ส.ค. 2024

ความคิดเห็น • 7

  • @mohammadzaid6813
    @mohammadzaid6813 3 หลายเดือนก่อน

    This video is very helpful for me
    Thanks a lot buddy

  • @mohammadzaid6813
    @mohammadzaid6813 3 หลายเดือนก่อน +6

    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()

    • @mohammadzaid6813
      @mohammadzaid6813 3 หลายเดือนก่อน

      Change chunk, channels and rate according to your choice :)

    • @aarushsaboo1194
      @aarushsaboo1194 2 หลายเดือนก่อน

      Thanks man!

  • @joshuashepherd7189
    @joshuashepherd7189 7 หลายเดือนก่อน

    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

  • @aqib5354
    @aqib5354 6 หลายเดือนก่อน +1

    can someone write the above code so any one can copy and paste

  • @YashViroja-h8t
    @YashViroja-h8t หลายเดือนก่อน

    #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()