title: How to get desktop audio with python?
body: <p>I am trying to make a screen recorder with python. I have figured out how to record the screen with <code>cv2.VideoWriter</code> and <code>pyautogui.screenshot()</code>, what I can't figure out is how I can also record the desktop audio and merge it with the video that I am recording. I also need the screen capture and the audio capture to run simultaneously. They need to start and stop at command. I've looked into <code>pyaudio</code> but I can't understand it. Can anyone help me out?</p>
  <p><strong>Update:</strong></p>
  <p>I have figured out how to use pyaudio and when researching about it, I found out about portaudio from <a href="https://stackoverflow.com/a/37218364/13799055">https://stackoverflow.com/a/37218364/13799055</a>. With my newly acquired knowledge, I wrote this code.</p>
  <pre><code>import pyaudio
  import wave
  
  p = pyaudio.PyAudio()
  
  for i in range(p.get_device_count()):
      print(i, p.get_device_info_by_index(i)[&quot;name&quot;])
  
  FORMAT = pyaudio.paInt16
  CHANNELS = 2
  RATE = 16000
  CHUNK = 1 * RATE
  RECORD_SECONDS = 5
  PATH = &quot;tmp.mp3&quot;
  
  stream = p.open(format=FORMAT,
                  channels=CHANNELS,
                  rate=RATE,
                  input=True,
                  frames_per_buffer=CHUNK,
                  input_device_index=0,
                  as_loopback=True)
  
  frames = []
  
  for _ in range(int((RATE / CHUNK) * RECORD_SECONDS)):
      data = stream.read(CHUNK)
      frames.append(data)
  
  wf = wave.open(PATH, &quot;wb&quot;)
  wf.setnchannels(CHANNELS)
  wf.setsampwidth(p.get_sample_size(FORMAT))
  wf.setframerate(RATE)
  wf.writeframes(b&quot;&quot;.join(frames))
  wf.close()
  </code></pre>
  <p>For some reason, it is not working and is still taking input from my microphone. It is having no errors though. This is what the devices loop shows:</p>
  <pre><code>0 Microsoft Sound Mapper - Input
  1 Microphone (High Definition Aud
  2 CABLE Output (VB-Audio Virtual
  3 Microphone (Voicemod Virtual Au
  4 Microphone (SplitCam Audio Mixe
  5 Microsoft Sound Mapper - Output
  6 Speakers (High Definition Audio
  7 CABLE Input (VB-Audio Virtual C
  8 Line (Voicemod Virtual Audio De
  9 CABLE Input (VB-Audio Virtual Cable)
  10 Speakers (High Definition Audio Device)
  11 Line (Voicemod Virtual Audio Device (WDM))
  12 CABLE Output (VB-Audio Virtual Cable)
  13 Microphone (Voicemod Virtual Audio Device (WDM))
  14 Microphone (SplitCam Audio Mixer)
  15 Microphone (High Definition Audio Device)
  16 Output (@System32\drivers\bthhfenum.sys,#4;%1 Hands-Free HF Audio%0
  ;(Anonymous Streamer))
  17 Input (@System32\drivers\bthhfenum.sys,#4;%1 Hands-Free HF Audio%0
  ;(Anonymous Streamer))
  18 CABLE Output (VB-Audio Point)
  19 Speakers (VB-Audio Point)
  20 Microphone ()
  21 Microphone (HD Audio Microphone 2)
  22 Speakers (HD Audio Headphone/Speakers)
  23 Headset (@System32\drivers\bthhfenum.sys,#2;%1 Hands-Free AG Audio%0
  ;(Synth))
  24 Headset (@System32\drivers\bthhfenum.sys,#2;%1 Hands-Free AG Audio%0
  ;(Synth))
  25 Headphones ()
  26 Microphone (VAD Wave)
  27 Line Out (VAD Wave)
  28 Headset (@System32\drivers\bthhfenum.sys,#2;%1 Hands-Free AG Audio%0
  ;(Bluetooth music))
  29 Headset (@System32\drivers\bthhfenum.sys,#2;%1 Hands-Free AG Audio%0
  ;(Bluetooth music))
  30 Headphones ()
  </code></pre>
  <p>Can anyone help me fix up my code?</p>