I am trying to make a screen recorder with python. I have figured out how to record the screen with cv2.VideoWriter
and pyautogui.screenshot()
, 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 pyaudio
but I can't understand it. Can anyone help me out?
Update:
I have figured out how to use pyaudio and when researching about it, I found out about portaudio from https://stackoverflow.com/a/37218364/13799055. With my newly acquired knowledge, I wrote this 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)["name"])
FORMAT = pyaudio.paInt16
CHANNELS = 2
RATE = 16000
CHUNK = 1 * RATE
RECORD_SECONDS = 5
PATH = "tmp.mp3"
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, "wb")
wf.setnchannels(CHANNELS)
wf.setsampwidth(p.get_sample_size(FORMAT))
wf.setframerate(RATE)
wf.writeframes(b"".join(frames))
wf.close()
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:
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 ()
Can anyone help me fix up my code?