Table of Contents


Introduction

Python can be used to perform a variety of tasks, including recording your voice and screen. We will be using Python’s sounddevice and wavio modules to play and record audio. We will also be using pyautogui along with NumPy and OpenCV to record and save the videos.

Pre-requisites

Instructions

Step 1: Setup

To install all required packages, run the following commands into your terminal to install using pip:

pip install sounddevice wavio numpy pillow pyautogui opencv-python

Then, create a file called app.py and add the following inside it to import the packages we will be using:

# Import required packages
import pyautogui
import cv2
import numpy as np
import time
import sounddevice as sd
import wavio as wv

If you want to find out more about each package, you can search them up on PyPI

Step 2: Voice Recorder

As with most voice recorders, we have to set a few variables and their values. This includes the [sampling frequency](https://en.wikipedia.org/wiki/Sampling_(signal_processing)#:~:text=The sampling frequency or sampling,is 48%2C000 samples per second.) (usually 44100 or 48000 fps) and the recording duration so Python knows when to stop the recording. You can declare them using variables like this:

# Import required packages
import pyautogui
import cv2
import numpy as np
import sounddevice as sd
import wavio as wv
import time

# Settings
freq = 48000 # Sampling Frequency
duration = 5 # Recording Duration

Now, we will add the code to start the recorder:

# Import required packages
import pyautogui
import cv2
import numpy as np
import sounddevice as sd
import wavio as wv

# Settings
freq = 48000 # Sampling Frequency
duration = 5 # Recording Duration

# Start recorder
print("Recording...")
recording = sd.rec(int(duration * freq), samplerate=freq, channels=2)

# Record audio 
sd.wait()