Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GSSoC'24: Python Screen Recorder - Desktop Application #1044

Merged
merged 6 commits into from
May 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 47 additions & 0 deletions Desktop Application/Basic/Python/Screen_Recorder/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
![](https://img.shields.io/badge/Programming_Language-Python-blue.svg)
![](https://img.shields.io/badge/Main_Tool_Used-OpenCV-gold.svg)
![](https://img.shields.io/badge/Supporting_Tools_Used-Pillow,_CustomTkinter-orange.svg)
![](https://img.shields.io/badge/Application-Screen_Recorder-yellow.svg)
![](https://img.shields.io/badge/Minimum_Python_Version-3.7-blue.svg)
![](https://img.shields.io/badge/Status-Complete-darklime.svg)



## Python Screen Recorder with GUI

This project demonstrates how to create a screen recording application using Python. The application features a graphical user interface (GUI) built with `customtkinter`, which allows users to start and stop screen recordings easily. The recorded video is saved in the specified format, making this tool useful for creating tutorials, capturing gameplay, or recording presentations.

### Key Features:
1. **User-Friendly Interface**: The GUI provides an intuitive way for users to input the output file name and specify a custom key to stop the recording.
2. **Real-Time Screen Capture**: Utilizes the `Pillow` library for capturing screen frames and `cv2` (OpenCV) for video encoding.
3. **Threaded Recording**: Implements threading to ensure that the GUI remains responsive while the recording is in progress.
4. **Customizable Settings**: Users can specify the output file name and an optional custom key to stop recording, enhancing usability and flexibility.

### Components:
- **Screen Capture**: Captures the screen content at a specified frame rate.
- **Video Encoding**: Encodes the captured frames into an MP4 video file.
- **GUI**: Built with `customtkinter` for an attractive and responsive user interface.
- **Concurrency**: Ensures smooth operation and responsiveness through threading.

This project is suitable for those looking to enhance their Python skills by working on an intermediate-level project that integrates multiple libraries and concepts.

---

### Example Usage:
1. **Enter File Name**: Specify the output file name in the provided entry box.
![image](https://github.com/jindalpriyanshu101/Project-Guidance/assets/52918255/b6da3508-078f-467b-b328-5242be6993a6)

2. **Custom Stop Key**: `Optionally`, enter a custom key to stop the recording (defaults to 'q' if left empty).
![image](https://github.com/jindalpriyanshu101/Project-Guidance/assets/52918255/c5888912-77f3-48af-84fe-e9fbfc8adbf6)


4. **Start Recording**: Click the "Start Recording" button to begin capturing the screen.
![image](https://github.com/jindalpriyanshu101/Project-Guidance/assets/52918255/45b026b2-d2bb-4870-bd3d-17121980e2cf)


5. **Stop Recording**: Use the specified key or the default 'q' ON the output screen to stop recording `or` click on button provided. The status label will update to indicate the recording has stopped.
![image](https://github.com/jindalpriyanshu101/Project-Guidance/assets/52918255/2e28954b-62fd-4dae-8342-2badccd56114)



This project combines practical Python programming with useful real-world application, making it a valuable addition to any developer's portfolio.
117 changes: 117 additions & 0 deletions Desktop Application/Basic/Python/Screen_Recorder/recorder.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
import customtkinter as ctk
import numpy as np
import cv2
from PIL import ImageGrab
from win32api import GetSystemMetrics
import threading
import time

# Initialize customtkinter
ctk.set_appearance_mode("dark")
ctk.set_default_color_theme("blue")

# Function to start screen recording
def start_recording():
global recording, output, key
file_name = file_name_entry.get().strip()

if not file_name:
status_label.configure(text="Please enter a valid file name.", text_color="red")
return

key = custom_key_entry.get().strip() or 'q' # Default to 'q' if no key is entered

# Video writer setup
screen_width = GetSystemMetrics(0)
screen_height = GetSystemMetrics(1)
resolution = (screen_width, screen_height)
fourcc = cv2.VideoWriter_fourcc(*'mp4v')
output = cv2.VideoWriter(file_name, fourcc, 20.0, resolution) # Adjusted frame rate to 20 FPS

recording = True
record_button.configure(state=ctk.DISABLED)
stop_button.configure(state=ctk.NORMAL)
status_label.configure(text="Recording...", text_color="white")

def record():
prev_time = time.time()
while recording:
# Capture the screen
img = ImageGrab.grab(bbox=(0, 0, screen_width, screen_height))
img_np = np.array(img)
img_final = cv2.cvtColor(img_np, cv2.COLOR_BGR2RGB)

# Write the frame to the video file
output.write(img_final)

# Show the recording window
cv2.imshow('RecorderX - Screen Recorder', img_final)

# Wait for the stop key or 10 milliseconds
if cv2.waitKey(10) == ord(key):
break

# Ensure the loop runs at the correct frame rate
time_elapsed = time.time() - prev_time
sleep_time = max(1./20. - time_elapsed, 0) # Adjusted for 20 FPS
time.sleep(sleep_time)
prev_time = time.time()

cv2.destroyAllWindows()
output.release()
status_label.configure(text="Recording stopped", text_color="white")
record_button.configure(state=ctk.NORMAL)
stop_button.configure(state=ctk.DISABLED)

threading.Thread(target=record).start()

# Function to stop screen recording
def stop_recording():
global recording
recording = False

# Main application window
app = ctk.CTk()
app.title("Screen Recorder")
app.geometry("512x512") # Set window size to 512x512 pixels

# Style configuration
padding = 10
font_large = ("Helvetica", 16, "bold")
font_small = ("Helvetica", 12)
button_color = "#1f6aa5"
text_color = "#f0f0f0"

# File name entry
file_name_label = ctk.CTkLabel(app, text="Output file name (with extension):", font=font_small, text_color=text_color)
file_name_label.pack(pady=padding)
file_name_entry = ctk.CTkEntry(app, width=300)
file_name_entry.pack(pady=padding)

# Custom key entry
custom_key_label = ctk.CTkLabel(app, text="Custom stop key (optional, default is 'q'):", font=font_small, text_color=text_color)
custom_key_label.pack(pady=padding)
custom_key_entry = ctk.CTkEntry(app, width=300)
custom_key_entry.pack(pady=padding)

# Start and Stop buttons
button_frame = ctk.CTkFrame(app)
button_frame.pack(pady=padding)

record_button = ctk.CTkButton(button_frame, text="Start Recording", command=start_recording, fg_color=button_color, font=font_large)
record_button.grid(row=0, column=0, padx=padding)

stop_button = ctk.CTkButton(button_frame, text="Stop Recording", command=stop_recording, state=ctk.DISABLED, fg_color=button_color, font=font_large)
stop_button.grid(row=0, column=1, padx=padding)

# Status label
status_label = ctk.CTkLabel(app, text="Ready to record", font=font_small, text_color=text_color)
status_label.pack(pady=padding)

# Initialize recording state and output variable
recording = False
output = None
key = 'q'

# Run the application
app.mainloop()
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
opencv-python
pillow
numpy
customtkinter
2 changes: 2 additions & 0 deletions Desktop Application/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@
| 26. | [Image Request](https://github.com/Kushal997-das/Project-Guidance/tree/main/Desktop%20Application/Basic/Python/Image%20Request) |
| 27. | [Internet Speed Meter](https://github.com/Kushal997-das/Project-Guidance/tree/main/Desktop%20Application/Basic/Python/Internet%20Speed%20Meter) |
| 28. | [Live News App](https://github.com/Kushal997-das/Project-Guidance/tree/main/Desktop%20Application/Basic/Python/Live%20News%20App) |
| 29. | [Screen Recorder](https://github.com/Kushal997-das/Project-Guidance/tree/main/Desktop%20Application/Basic/Python/Screen-Recorder) |

</br>

## Java 🚀
Expand Down
Loading