How to record the sound of a headset with blueooth or USB in python and windows

I want to record the audio from the headphones, but from the microphone and what I hear, also, if I only listen to music, for example from YouTube, I want to record it. The headset is connected with Bluetooth and I can’t record it, I can only record if it is connected from the audio connection. I used stereomix and this is my code:

import pyaudio
import wave

CHUNK = 1024
FORMAT = pyaudio.paInt16
CHANNELS = 2
RATE = 44100
RECORD_SECONDS = 5
OUTPUT_FILENAME = "output.wav"

p = pyaudio.PyAudio()
for i in range(p.get_device_count()):
    dev = p.get_device_info_by_index(i)
    if ('Stereomix' in dev['name'] and dev['hostApi'] == 0):
        dev_index = dev['index']
        print('dev_index', dev_index)

stream = p.open(format=FORMAT,
               channels=CHANNELS,
               rate=RATE,
               input=True,
               input_device_index=dev_index,  
               frames_per_buffer=CHUNK)

print("Grabando...")

frames = []

for i in range(0, int(RATE / CHUNK * RECORD_SECONDS)):
    data = stream.read(CHUNK)
    frames.append(data)

print("Grabación finalizada.")

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

how can i record the sound what i hear?

thanks in advance

  • Can’t you just record the audio from your pc? It is the same audio you would hear on your headphones

    – 

  • how can i record it? that is what i want

    – 

Leave a Comment