#🔒 Recording desktop audio

45 messages · Page 1 of 1 (latest)

sleek pawn
#
import pyaudio
import wave
import os
import keyboard

class DesktopAudioRecorder:
    def __init__(self):
        self.recording = False
        self.output_folder = os.path.join(os.path.dirname(os.path.abspath(__file__)), "records")
        if not os.path.exists(self.output_folder):
            os.makedirs(self.output_folder)

    def record_audio(self):
        keyboard.add_hotkey('ctrl+alt+r', self.toggle_recording)

    def toggle_recording(self):
        if self.recording:
            self.stop_recording()
        else:
            self.start_recording()

    def start_recording(self):
        self.recording = True
        self.frames = []
        self.audio = pyaudio.PyAudio()
        self.stream = self.audio.open(format=pyaudio.paInt16,
                                      channels=2,
                                      rate=44100,
                                      input=True,
                                      frames_per_buffer=1024)
        print("Recording started... Press ctrl+alt+r to stop.")
        self.record_and_save_audio()

    def record_and_save_audio(self):
        while self.recording:
            data = self.stream.read(1024)
            self.frames.append(data)```
compact yewBOT
#

@sleek pawn

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

sleek pawn
#
    def stop_recording(self):
        if self.recording:
            self.recording = False
            self.stream.stop_stream()
            self.stream.close()
            self.audio.terminate()
            self.save_recording()

    def save_recording(self):
        if self.frames:
            output_filename = os.path.join(self.output_folder, f"{len(os.listdir(self.output_folder)) + 1}.wav")
            wf = wave.open(output_filename, 'wb')
            wf.setnchannels(2)
            wf.setsampwidth(self.audio.get_sample_size(pyaudio.paInt16))
            wf.setframerate(44100)
            wf.writeframes(b''.join(self.frames))
            wf.close()
            print(f"Recording saved as {output_filename}")
        else:
            print("No audio recorded.")

if __name__ == "__main__":
    recorder = DesktopAudioRecorder()
    recorder.record_audio()
    keyboard.wait('esc')  # Wait for the 'esc' key to exit the program```
#

heres the other half

#

it exports the file and all but it doesnt save any audio from my desktop

swift bridge
#

Ok, next time use the pastebin:

#

!paste

compact yewBOT
#
Pasting large amounts of code

If your code is too long to fit in a codeblock in Discord, you can paste your code here:
https://paste.pythondiscord.com/

After pasting your code, save it by clicking the Paste! button in the bottom left, or by pressing CTRL + S. After doing that, you will be navigated to the new paste's page. Copy the URL and post it here so others can see it.

sleek pawn
#

oh alr

swift bridge
#

So it's creating a file but with no sound?

sleek pawn
#

yeah

#

now it doesnt even save

#

the keybinds dont work

swift bridge
#

What did you change then?

sleek pawn
#

nothing, all of this was chat gpt'ed

#

but it worked

#

it saved the file in a folder

#

but no audio

#

now it doesnt work when i press the hotkey again

swift bridge
#

Are you running multiple copies at same time?

sleek pawn
#

nope

swift bridge
#

The audio interface may already be locked by another process. Does it just hang?

sleek pawn
#

and then once i press it again

#

it does nothing

swift bridge
#

Yah, so kill any Python process and try again. Check task manager.

sleek pawn
#

none are open in task manager

#

i think this whole thing just needs to be remade fully

#

since chatgpt is hella bad

swift bridge
#

Next time, start with the minimum and don't add features like keybinds until the core feature (recording works. Makes debugging easier

sleek pawn
#

true

swift bridge
#

for why the key bind stopped working: add some print statements so you can isolate where it's 'stuck'

sleek pawn
#

idk anything about python sorry

swift bridge
#

Now's a good time to learn!

sleek pawn
#

i gtg gonna get on this later

#
def select_input_device(p):
    for i in range(p.get_device_count()):
        print(i, p.get_device_info_by_index(i)["name"])
    device_index = int(input("Enter the index of the input device: "))
    return device_index```
but found a way to select the device
#

just need to make it save forever onto that

#

and then record the audio using that

earnest night
#

add this as a method instead of a function

#

and use the device_index attribute to specify

compact yewBOT
#
Python help channel closed

This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.