【发布时间】:2020-10-15 10:51:40
【问题描述】:
我正在使用 numpy、OpenCV 和 PyAutoGUI 在 Python 中构建屏幕录像机。我已将 tkinter 用于 GUI 目的。我的屏幕录像机的问题是,当我单击“录制屏幕”按钮时它只录制一帧,然后屏幕卡住了,我什么也做不了。到目前为止,这是我的代码:
from tkinter import *
import cv2
import numpy as np
import pyautogui
resolution = (1366,768)
指定视频编解码器:
codec = cv2.VideoWriter_fourcc(*"XVID")
指定输出文件的名称:
filename = "Recordings.avi"
指定帧率(我们可以选择任何值并进行试验):
fps = 30.0
创建VideoWriter 对象:
out = cv2.VideoWriter(filename, codec, fps, resolution)
def startRecording():
window.iconify()
while True:
img = pyautogui.screenshot()
# Convert the screenshot to a numpy array
frame = np.array(img)
# Convert it from BGR(Blue, Green, Red) to
# RGB(Red, Green, Blue)
frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
# Write it to the output file
out.write(frame)
def stopRecording():
cv2.destroyAllWindows()
out.release()
window.destroy()
window = Tk()
window.title("Screen Recorder")
window.geometry("400x150")
window.config(bg='pink')
recordButton = Button(window,text="Record(F9)",font=("Bell MT",20),width=20,command=startRecording)
recordButton.pack(pady=(10,0))
stopButton = Button(window,text="Stop(F10)",font=("Bell MT",20),width=20,command=stopRecording)
stopButton.pack(pady=(10,0))
mainloop()
【问题讨论】:
标签: python python-3.x opencv pyautogui