一、界面开发
def set_init_window(self):
self.tk.title('')
self.tk.iconbitmap(self.icon_path())
screeWidth = self.tk.winfo_screenwidth()
screeHeight = self.tk.winfo_screenheight()
width = int((screeWidth - 500) / 2)
height = int((screeHeight - 300) / 2)
self.tk.geometry(f'500x100+{width}+{height}')
btn1 = tkinter.Button(self.tk, width=5, height=1, text='开始',
command=lambda:[threading.Thread(target=self.video_record).start(),threading.Thread(target=self.start_listen).start()])
btn1.pack()
btn1.place(x=110, y=50, anchor='n')
btn2 = tkinter.Button(self.tk, width=15, height=1, text='设置录制区域', command=lambda:threading.Thread(target=self.set_range).start())
btn2.pack()
btn2.place(x=230, y=50, anchor='n')
def icon_path(self):
ICON = (b'\x00\x00\x01\x00\x01\x00\x10\x10\x00\x00\x01\x00\x08\x00h\x05\x00\x00'
b'\x16\x00\x00\x00(\x00\x00\x00\x10\x00\x00\x00 \x00\x00\x00\x01\x00'
b'\x08\x00\x00\x00\x00\x00@\x05\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'
b'\x00\x01\x00\x00\x00\x01') + b'\x00' * 1282 + b'\xff' * 64
_, ICON_PATH = tempfile.mkstemp()
with open(ICON_PATH, 'wb') as icon_file:
icon_file.write(ICON)
return ICON_PATH
二、录屏参数设置
1.设置录屏范围
def set_range(self):
self.tk.state('icon')
screeWidth = self.tk.winfo_screenwidth()
screeHeight = self.tk.winfo_screenheight()
self.newFrame = tkinter.Toplevel(self.tk,width=screeWidth,height=screeHeight,bg='white')
self.newFrame.attributes('-transparentcolor', 'white')
self.newFrame.overrideredirect(True)
self.canvas = tkinter.Canvas(self.newFrame,bg='white',bd=0,width=screeWidth,height=screeHeight)
self.canvas.bind('<Button-1>', self.onLeftButtonDown)
self.canvas.bind('<B1-Motion>', self.onLeftButtonMove)
self.canvas.bind('<ButtonRelease-1>', self.onLeftButtonUp)
self.canvas.pack(fill=tkinter.BOTH, expand=tkinter.YES)
time.sleep(0.3)
im = ImageGrab.grab()
im.save('temp.png')
im.close()
self.image = tkinter.PhotoImage(file='temp.png')
os.remove('temp.png')
self.canvas.create_image(screeWidth//2, screeHeight//2, image=self.image)
2.鼠标事件监听
def onLeftButtonDown(self,event):
try:
self.canvas.delete(self.lastDraw)
self.canvas.delete(self.dot1)
self.canvas.delete(self.dot2)
self.btn3.destroy()
except Exception as e:
pass
self.X = event.x
self.Y = event.y
self.X2 = 0
self.Y2 = 0
def onLeftButtonMove(self,event):
try:
self.canvas.delete(self.lastDraw)
self.canvas.delete(self.dot1)
self.canvas.delete(self.dot2)
self.btn3.destroy()
except Exception as e:
pass
self.X2 = event.x
self.Y2 = event.y
self.lastDraw = self.canvas.create_rectangle(self.X, self.Y, event.x, event.y,width=2, outline='pink')
def onLeftButtonUp(self,event):
print("起点", self.X, self.Y)
print("终点", self.X2, self.Y2)
if self.X2==0 and self.X2==0:
return
self.width, self.high = self.X2-self.X,self.Y2-self.Y
self.region = (self.X, self.Y, self.X2, self.Y2)
self.dot1=self.canvas.create_text(self.X, self.Y - 10, text=f'({self.X},{self.Y})', font=("Purisa", 12), fill="pink")
self.dot2=self.canvas.create_text(self.X2, self.Y2 + 10, text=f'({self.X2},{self.Y2})', font=("Purisa", 12), fill="pink")
self.btn3 = tkinter.Button(self.canvas, width=15, height=1, text='确定录制区域',bg='pink',fg='#64854c',command=lambda:[self.newFrame.destroy(),self.tk.state('normal')])
self.btn3.pack()
self.btn3.place(x=self.X2-20, y=self.Y2+20, anchor='n')
3.键盘事件监听
def start_listen(self):
with keyboard.Listener(on_press=self.on_press) as listener:
listener.join()
def on_press(self,key):
if key == keyboard.Key.esc:
self.flag = True
return False
三、录屏操作
def video_record(self):
fourcc = cv2.VideoWriter_fourcc('X', 'V', 'I', 'D')
out = cv2.VideoWriter('output.mp4', fourcc, 14, (self.width, self.high))
self.count = 1
while (True):
img = ImageGrab.grab(self.region)
img_np = numpy.array(img)
frame = cv2.cvtColor(img_np, cv2.COLOR_BGR2RGB)
out.write(frame)
self.count += 1
label = tkinter.Label(self.tk, text=f"{int(self.count / 14)}秒")
label.pack()
label.place(x=320, y=55, anchor='n')
if self.flag == True:
tkinter.messagebox.showinfo('提示', '录屏结束')
self.flag = False
break
out.release()
cv2.destroyAllWindows()
源码地址(https://gitee.com/linjinpeng/python_case/blob/maste)
|