-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscreen_recorder.py
144 lines (116 loc) · 4.2 KB
/
screen_recorder.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
import cv2
import numpy as np
import tkinter as tk
from tkinter import ttk
import threading
import pyautogui
global frame_count
frame_count = 0
global counter
counter = 0
global r
r = [int(0), int(0), int(0), int(0)]
global resolution
resolution = (1920, 1080) # Set initial resolution
# Create a flag to control recording
recording = False
out = cv2.VideoWriter('recording.avi', cv2.VideoWriter_fourcc(*'MJPG'), 15, resolution)
def start():
global recording
recording = True
# Disable the "Start" button after starting recording
start_button.config(state=tk.DISABLED)
# Enable the "Pause" and "Stop" buttons
pause_button.config(state=tk.NORMAL)
stop_button.config(state=tk.NORMAL)
def stop():
global recording
recording = False
out.release() # Release the VideoWriter when stopping recording
control.destroy()
def pause():
global recording
recording = False
# Enable the "Start" button when paused
start_button.config(state=tk.NORMAL)
# Disable the "Pause" button while paused
pause_button.config(state=tk.DISABLED)
# Enable the "Stop" button when paused
stop_button.config(state=tk.NORMAL)
def crop():
global crop_var, r
crop_var.set(True)
print(crop_var)
option.destroy()
if crop_var.get():
img = pyautogui.screenshot()
frame = np.array(img)
r = cv2.selectROI("Select the area", frame)
cv2.destroyAllWindows()
open_control_window()
def fullscreen():
global fullscreen_var
fullscreen_var.set(True)
option.destroy()
open_control_window()
def open_control_window():
global control, recording, start_button, pause_button, stop_button
control = tk.Tk()
control.title("Controls")
control.geometry('400x100')
recording = False # Reset the recording flag when the control window is opened
start_button = ttk.Button(control, text="Start", command=start)
start_button.pack(padx=10)
pause_button = ttk.Button(control, text="Pause", command=pause, state=tk.DISABLED)
pause_button.pack(padx=10)
stop_button = ttk.Button(control, text="Stop", command=stop, state=tk.DISABLED)
stop_button.pack(padx=10)
control.after(1, ScreenRecorder)
control.mainloop()
def highlight_recording_area():
global r
while recording:
x, y, width, height = r
img = pyautogui.screenshot()
frame = np.array(img)
# Draw a red rectangle on the screenshot to highlight the recording area
frame[y:y+height, x:x+width, :] = [0, 0, 255] # Red channel
# Convert frame to RGB format for displaying with OpenCV
frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
# Display the frame with the highlighted area
cv2.imshow('Recording', frame)
cv2.waitKey(1)
def ScreenRecorder():
global frame_count, counter, resolution, recording, r, crop_var, fullscreen_var, out
if recording:
# Take screenshot using PyAutoGUI
img = pyautogui.screenshot()
# Convert the screenshot to a numpy array
frame = np.array(img)
if crop_var.get():
frame = frame[int(r[1]):int(r[1] + r[3]), int(r[0]):int(r[0] + r[2])]
frame = cv2.resize(frame, resolution, interpolation=cv2.INTER_NEAREST)
frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
print('recording')
out.write(frame)
frame_count += 1
counter += 1
print(counter)
option.after(1, ScreenRecorder)
option = tk.Tk()
option.title("Option")
option.geometry('400x100')
crop_var = tk.BooleanVar()
crop_var.set(False)
fullscreen_var = tk.BooleanVar()
fullscreen_var.set(True)
crop_button = ttk.Button(option, text="Crop", command=crop)
crop_button.pack(padx=10)
fullscreen_button = ttk.Button(option, text="Full Screen", command=fullscreen)
fullscreen_button.pack(padx=10)
# Create a thread to highlight the recording area
highlight_thread = threading.Thread(target=highlight_recording_area)
highlight_thread.daemon = True # Allow the thread to exit when the main program exits
highlight_thread.start()
option.mainloop()
cv2.destroyAllWindows()