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)```
#🔒 Recording desktop audio
45 messages · Page 1 of 1 (latest)
@sleek pawn
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.
Closes after a period of inactivity, or when you send !close.
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
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.
So it's creating a file but with no sound?
What did you change then?
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
Are you running multiple copies at same time?
nope
The audio interface may already be locked by another process. Does it just hang?
Yah, so kill any Python process and try again. Check task manager.
none are open in task manager
i think this whole thing just needs to be remade fully
since chatgpt is hella bad
Next time, start with the minimum and don't add features like keybinds until the core feature (recording works. Makes debugging easier
true
But, in this case, the lack of audio may be because patio is selecting the wrong device. See https://stackoverflow.com/questions/40704026/voice-recording-using-pyaudio#40704898 for a discussion. Basically, list out your devices and figure out which one is your microphone. Then pass that device index to pyaudio
for why the key bind stopped working: add some print statements so you can isolate where it's 'stuck'
idk anything about python sorry
Now's a good time to learn!
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
def select_input_device(self, p):
for i in range(p.get_device_count()):
print(i, p.get_device_info_by_index(i)["name"])
self.device_index = int(input("Enter the index of the input device: "))
add this as a method instead of a function
and use the device_index attribute to specify
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.