Home » Tutorials » How to Create a Screen Recorder using Python

How to Create a Screen Recorder using Python

Table of Contents

In today’s article, we’re going to create a Screen Recorder in Python using the pyscreenrec and the tkinter libraries. This script will allow the user to start, pause, resume, and stop the recording as well as name the recording.

Let’s get started!

Necessary Libraries

$ pip install tk
$ pip install pyscreenrec

This allows us to install the tkinter and pyscreenrec libraries for the code to function properly.

RELATED: If you want to learn how to take screenshots using Python read this article.

Imports

import tkinter as tk
import pyscreenrec # Make sure you have the necessary library installed

We start by importing two libraries:

  • The first is tkinter, which is used to create a graphical user interface (GUI).
  • The second is pyscreenrec, which we will use for screen recording.

Creating the Main Window

root = tk.Tk()
root.geometry("400x500")
root.title("Custom Screen Recorder - The Pycodes")
root.config(bg="black")
root.resizable(False, False)

Next, we create the main window, give it a title, and define its geometry and background color, as well as disable its resizing.

Making the Title Label

# Title label
title_label = tk.Label(root, text="Custom Screen Recorder", font="arial 20 bold", bg="black", fg="red")
title_label.place(x=40, y=100)

Then, we create a label that says “Custom Screen Recorder“, give it a font and size, and then place it in specified coordinates.

Initializing the Screen Recorder

# Initialize the screen recorder
recorder = pyscreenrec.ScreenRecorder()

After that, we create an instance of the ScreenRecorder class and have it manage the screen recording features in the script.

Recording Control Functions

# Function to start recording
def start_recording():
    filename = filename_entry.get()
    recorder.start_recording(str(filename + ".mp4"), 5)


# Function to pause recording
def pause_recording():
    recorder.pause_recording()


# Function to resume recording
def resume_recording():
    recorder.resume_recording()


# Function to stop recording
def stop_recording():
    recorder.stop_recording()

This code defines four functions:

  • The first function retrieves the filename from the entry widget, adds mp4 to it, and starts recording for 5 seconds.
  • The second: pause the recording.
  • The third one resumes the recording.
  • The last function: stops the recording as their name implies.

Entry Field for Filename

# Entry field for filename
filename = tk.StringVar()
filename_entry = tk.Entry(root, textvariable=filename, width=18, font="arial 15")
filename_entry.place(x=100, y=300)
filename.set("Name This Recording")

Here, we created an entry widget where the user can name his screen recording.

Creating Buttons to Control Recording

# Buttons for controlling recording
start_button = tk.Button(root, text="Start", font="arial 22", bg="black", fg="white", bd=0, command=start_recording)
start_button.place(x=155, y=200)


pause_button = tk.Button(root, text="Pause", font="arial 22", bg="black", fg="white", bd=0, command=pause_recording)
pause_button.place(x=50, y=400)


resume_button = tk.Button(root, text="Resume", font="arial 22", bg="black", fg="white", bd=0, command=resume_recording)
resume_button.place(x=150, y=400)


stop_button = tk.Button(root, text="Stop", font="arial 22", bg="black", fg="white", bd=0, command=stop_recording)
stop_button.place(x=269, y=400)

What we did here is creating four buttons, each one of them triggers the corresponding function, the buttons are: “start, pause, resume, and stop“.

Main Loop

# Start the Tkinter main loop
root.mainloop()

Lastly, this part keeps the main window running (in a loop) and interacting with the user until he chooses to exit the main window.

Example

Full Code

import tkinter as tk
import pyscreenrec # Make sure you have the necessary library installed


# Create the main application window
root = tk.Tk()
root.geometry("400x500")
root.title("Custom Screen Recorder - The Pycodes")
root.config(bg="black")
root.resizable(False, False)


# Title label
title_label = tk.Label(root, text="Custom Screen Recorder", font="arial 20 bold", bg="black", fg="red")
title_label.place(x=40, y=100)


# Initialize the screen recorder
recorder = pyscreenrec.ScreenRecorder()


# Function to start recording
def start_recording():
    filename = filename_entry.get()
    recorder.start_recording(str(filename + ".mp4"), 5)


# Function to pause recording
def pause_recording():
    recorder.pause_recording()


# Function to resume recording
def resume_recording():
    recorder.resume_recording()


# Function to stop recording
def stop_recording():
    recorder.stop_recording()


# Entry field for filename
filename = tk.StringVar()
filename_entry = tk.Entry(root, textvariable=filename, width=18, font="arial 15")
filename_entry.place(x=100, y=300)
filename.set("Name This Recording")


# Buttons for controlling recording
start_button = tk.Button(root, text="Start", font="arial 22", bg="black", fg="white", bd=0, command=start_recording)
start_button.place(x=155, y=200)


pause_button = tk.Button(root, text="Pause", font="arial 22", bg="black", fg="white", bd=0, command=pause_recording)
pause_button.place(x=50, y=400)


resume_button = tk.Button(root, text="Resume", font="arial 22", bg="black", fg="white", bd=0, command=resume_recording)
resume_button.place(x=150, y=400)


stop_button = tk.Button(root, text="Stop", font="arial 22", bg="black", fg="white", bd=0, command=stop_recording)
stop_button.place(x=269, y=400)


# Start the Tkinter main loop
root.mainloop()

Happy Coding!

Subscribe for Top Free Python Tutorials!

Receive the best directly.  Elevate Your Coding Journey!

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top
×