-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathscreenRecorder.py
280 lines (239 loc) · 12.1 KB
/
screenRecorder.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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
"""
ScreenRecorder.py is a small, Windows app that records video from your screen and audio from the default microphone
Copyright (C) 2020 coderman64
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
"""
import subprocess
from tkinter import *
from tkinter import messagebox
from tkinter.ttk import Button,Entry,Radiobutton,Checkbutton
from time import sleep
import os
import recordFile, Webcam
import webbrowser
from cmdGen import cmdGen
from sr_settings import settingsWin
class App(Tk): #the main class for the main window
def __init__(self):
Tk.__init__(self)
# window properties
self.title(string = "Screen Recorder")
self.iconbitmap("icon.ico")
self.resizable(width = False, height = False)
ffmpegAvailable = False
for item in os.listdir():
if item == "ffmpeg.exe":
ffmpegAvailable = True
break
if not ffmpegAvailable:
self.withdraw()
if messagebox.askyesno("FFmpeg Not Found","ffmpeg.exe could not be found in screen recorder's directory. Do you want to be redirected to the ffmpeg download website?"):
webbrowser.open_new_tab("https://ffmpeg.zeranoe.com/builds/")
exit()
self.cmdGen = cmdGen() # create a command generator object to store settings
# file name
label1 = Label(self, text="File Name:")
label1.grid(row = 0, column = 0, sticky = "")
self.entry1 = Entry(self)
self.entry1.grid(row = 0, column = 1,sticky="ew")
# ensure the existance of the "ScreenCaptures" directory
try:
os.mkdir("ScreenCaptures")
except FileExistsError:
pass
os.chdir("ScreenCaptures")
# find a default file name that is currently available.
defaultFile = "ScreenCapture.mp4"
available = False
fileNum = 0
while available == False:
hasMatch = False
for item in os.listdir():
if item == defaultFile:
hasMatch = True
break
if not hasMatch:
available = True
else:
fileNum += 1
defaultFile = "ScreenCapture"+str(fileNum)+".mp4"
os.chdir("..")
self.entry1.insert(END,defaultFile)
# radio buttons determine what to record
self.what = StringVar()
self.what.set("desktop")
self.radio2 = Radiobutton(self, text="record the window with the title of: ", variable=self.what, value = "title", command = self.enDis1)
self.radio1 = Radiobutton(self, text="record the entire desktop", variable=self.what, value = "desktop", command = self.enDis)
self.radio1.grid(row = 1, column = 0, sticky="w")
self.radio2.grid(row = 2, column = 0, sticky = "w")
self.entry2 = Entry(self, state=DISABLED)
self.entry2.grid(row = 2, column = 1,sticky="ew")
# initialize webcam
self.webcamdevices = Webcam.listCam()
self.webcamrecorder = Webcam.capturer("")
# "record from webcam" checkbox
self.rcchecked = IntVar()
self.recordcam = Checkbutton(self, text="Record from webcam", command = self.checkboxChanged,variable=self.rcchecked)
self.recordcam.grid(row = 3, column = 0)
# a drop-down allowing you to select the webcam device from the available directshow capture devices
self.devicename = StringVar(self)
if self.webcamdevices:
self.devicename.set(self.webcamdevices[0])
self.deviceselector = OptionMenu(self, self.devicename, *self.webcamdevices)
self.deviceselector.config(state=DISABLED)
self.deviceselector.grid(row = 3, column = 1)
else:
self.devicename.set("NO DEVICES AVAILABLE")
self.recordcam.config(state=DISABLED)
self.deviceselector = OptionMenu(self, self.devicename, "NO DEVICES AVAILABLE")
self.deviceselector.config(state=DISABLED)
self.deviceselector.grid(row = 3, column = 1)
self.opButton = Button(self, text="⚙ Additional Options...",command = self.openSettings)
self.opButton.grid(row = 4, column=1, sticky='e')
# the "start recording" button
self.startButton = Button(self, text="⏺ Start Recording", command = self.startRecord)
self.startButton.grid(row = 5, column = 0, columnspan = 2)
# some variables
self.recording = False # are we recording?
self.proc = None # the popen object for ffmpeg (during screenrecord)
self.recorder = recordFile.recorder() # the "recorder" object for audio (see recordFile.py)
self.mergeProcess = None # the popen object for ffmpeg (while merging video and audio files)
# start the ffmpeg monitoring callback
self.pollClosed()
def openSettings(self):
self.settings = settingsWin(self,self.cmdGen,self.recorder)
def pollClosed(self):
"""callback that repeats itself every 100ms. Automatically determines if ffmpeg is still running."""
if self.recording:
if self.proc.poll() != None:
self.startRecord()
messagebox.showerror("ffmpeg error","ffmpeg has stopped working. ERROR: \n"+str(self.proc.stderr.read()).replace('\\r\\n','\n'))
if self.recorder.error:
self.startRecord()
if self.mergeProcess and self.recording == False:
if self.mergeProcess.poll() != None:
self.startButton.config(text="⏺ Start Recording", state = NORMAL)
self.title(string = "Screen Recorder")
self.after(100, self.pollClosed)
def enDis(self):
"""Called when the "desktop" radio button is pressed"""
self.entry2.config(state=DISABLED)
# self.what.set("desktop")
def enDis1(self):
"""Called when the "window title" radio button is pressed"""
self.entry2.config(state = NORMAL)
# self.what.set("title")
def checkboxChanged(self):
"""Called when the "record webcam" checkbox is checked or unchecked."""
#self.rcchecked = not self.rcchecked
if self.rcchecked.get():
self.deviceselector.config(state = NORMAL)
else:
self.deviceselector.config(state = DISABLED)
def startRecord(self):
"""toggles recording. Will start conversion subprocess on recording completion"""
if self.recording == False:
# change the window
self.title(string = "Screen Recorder (Recording...)")
self.startButton.config(text="⏹️ Stop Recording")
self.filename = self.entry1.get()
# disable interface
self.entry1.config(state = DISABLED)
self.radio1.config(state = DISABLED)
self.radio2.config(state = DISABLED)
self.deviceselector.config(state = DISABLED)
self.opButton.config(state = DISABLED)
if self.what.get() == "title":
self.entry2.config(state = DISABLED)
# ensure the existence of the "tmp" directory
try:
os.mkdir("tmp")
except FileExistsError:
pass
# start screen recording process
self.recording = True
startupinfo = subprocess.STARTUPINFO()
startupinfo.dwFlags |= subprocess.STARTF_USESHOWWINDOW
startupinfo.wShowWindow = subprocess.SW_HIDE
# self.cmdGen.setFps(60)
# self.cmdGen.setEncode('nvenc_h264') # CPU: mpeg4 // NVIDIA: h264_nvenc // AMD: no.
self.cmdGen.setSource(self.what.get()=="title",self.entry2.get())
command = self.cmdGen.getCmd("tmp/tmp.mkv")
self.proc = subprocess.Popen(args=command, startupinfo=startupinfo)#,stderr=subprocess.PIPE)
# start audio recording
self.recorder.record("tmp/tmp.wav")
# start webcam recording, if checked
self.recordcam.config(state = DISABLED)
if self.rcchecked.get() and self.webcamdevices:
self.webcamrecorder.setDevice(str(self.devicename.get()))
self.webcamrecorder.startCapture("tmp/webcamtmp.mkv")
# minimize the window to get it out of the way of the recording
self.iconify()
elif self.recording == True:
self.deiconify()
defaultFile = self.filename
# re-enable interface
self.entry1.config(state = NORMAL)
self.radio1.config(state = NORMAL)
self.radio2.config(state = NORMAL)
self.opButton.config(state = NORMAL)
if self.webcamdevices:
self.recordcam.config(state = NORMAL)
if self.rcchecked.get():
self.deviceselector.config(state = NORMAL)
if self.what.get() == "title":
self.entry2.config(state = NORMAL)
available = False
fileNum = 0
# stop all recording processes
self.recording = False
self.proc.terminate()
if self.rcchecked.get() and self.webcamdevices:
self.webcamrecorder.stopCapture()
try:
os.mkdir("ScreenCaptures")
except FileExistsError:
pass
# change the window title and button text to reflect the current process
self.title(string = "Screen Recorder (converting...)")
self.startButton.config(text="converting your previous recording, please wait...", state = DISABLED)
# start the video conversion process
startupinfo = subprocess.STARTUPINFO()
startupinfo.dwFlags |= subprocess.STARTF_USESHOWWINDOW
startupinfo.wShowWindow = subprocess.SW_HIDE
self.cmdGen.config(audList=self.recorder.devices)
command = self.cmdGen.getCvtCmd("ScreenCaptures/"+self.filename)
if not self.recorder.error:
self.mergeProcess = subprocess.Popen(args=command)#,startupinfo=startupinfo)
# if self.rcchecked.get():
# self.mergeProcess = subprocess.Popen(args= ["ffmpeg","-i",'tmp/tmp.mkv','-i','tmp/tmp.wav','-i','tmp/webcamtmp.mkv','-filter_complex','[2:v] scale=640:-1 [inner]; [0:0][inner] overlay=0:0 [out]',"-shortest",'-map','[out]','-y',"ScreenCaptures/"+self.filename])
# else:
# self.mergeProcess = subprocess.Popen(args= ["ffmpeg","-i",'tmp/tmp.mkv','-i','tmp/tmp.wav',"-shortest",'-y',"ScreenCaptures/"+self.filename], startupinfo=startupinfo)
# change the screen capture name to something that is not taken
os.chdir("ScreenCaptures")
while True:
matches = 0
for item in os.listdir():
if item == defaultFile:
matches += 1
if matches == 0:
self.entry1.delete(0,END)
self.entry1.insert(END,defaultFile)
break
else:
fileNum += 1
file = self.filename.split(".")
defaultFile = file[0].rstrip("1234567890")+str(fileNum)+"."+file[1]
os.chdir("../")
app = App()
app.mainloop()
print("DONE!")