【问题标题】:My Screen Recorder built using OpenCV and PyAutoGUI records only one frame我使用 OpenCV 和 PyAutoGUI 构建的屏幕录像机只记录一帧
【发布时间】: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


    【解决方案1】:

    您不能在按钮回调中进行阻塞调用。 正如您所写,startRecording 永远不会结束,因此会阻塞 tkinter 主循环。录制可能有效,但您的 UI 变得无响应。

    最好的办法是安排录制(查找after 方法):每 x 毫秒录制一帧。

    这是一个基于你的原始代码的简化示例(你需要完成它)

    continueRecording = True # must be declared before stopRecording 
    window = Tk() # must be declared before recordOneFrame
    
    def stopRecording():
        global continueRecording
        continueRecording = False 
    
    def recordOneFrame():
        global continueRecording
        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)
        if continueRecording:
            window.after(round(1/25. * 1000),recordOneFrame)
            
    
    def startRecording():
        recordOneFrame()
    

    【讨论】:

    • 我该怎么做?
    • 我用一些显示原理的代码更新了我的答案。
    • 谢谢你的回答...你能告诉我你是怎么计算的吗?基本上在这行代码window.after(round(1/25. * 1000),recordOneFrame)
    • @TaylorSwift 每秒 25 帧。
    • 这是课堂作业吗?在处理视频时,Python 可能不是第一个想到的代码库。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-05-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-03-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多