Python 3.x 屏幕记录时间与使用opencv的PyQt中的实际使用时间不一致
最近,我正在写一个关于屏幕记录器的代码。本程序使用Python3.5+PyQt+Opencv3.2。 我有个问题。当我使用该程序录制10秒的视频时,实际录制的文件视频只有5秒,移动速度看起来更快(可能快2倍)。 我不知道为什么。有人能帮我吗?多谢各位 该计划的主要思想是:Python 3.x 屏幕记录时间与使用opencv的PyQt中的实际使用时间不一致,python-3.x,opencv,pyqt5,Python 3.x,Opencv,Pyqt5,最近,我正在写一个关于屏幕记录器的代码。本程序使用Python3.5+PyQt+Opencv3.2。 我有个问题。当我使用该程序录制10秒的视频时,实际录制的文件视频只有5秒,移动速度看起来更快(可能快2倍)。 我不知道为什么。有人能帮我吗?多谢各位 该计划的主要思想是: 单击开始按钮后,开始计时 在功能captureScreen中,捕获屏幕,然后写入视频文件 单击停止按钮后,停止计时器 源代码放在这里 ui.py mainDialog.py # -*- coding: utf-8 -*- _
captureScreen
中,捕获屏幕,然后写入视频文件计时器
# -*- coding: utf-8 -*-
__author__ = '350187947@qq.com'
import sys
from PyQt5.QtWidgets import QApplication, QDialog
from PyQt5.QtCore import Qt, QTimer
from ui import *
from PIL import ImageGrab
import numpy as np
import cv2
class MainDialog(QDialog,Ui_mainDialog):
def __init__(self,parent=None):
super(MainDialog,self).__init__(parent)
# UI properties
self.setupUi(self)
self.setWindowFlags(Qt.WindowCloseButtonHint)
self.stopBtn.setEnabled(False)
# video parameter
self.__fps = 25.0
self.__filename = "1stTest.avi"
self.__codecs = cv2.VideoWriter_fourcc(*"XVID")
im = ImageGrab.grab() # capture the screen
frame = np.array(im) # change the frame from Image to np array
self.__height, self.__width, self.__depth = frame.shape # get the shape of the frame
print(self.__height, self.__width, self.__depth)
self.__timer = QTimer(self)
self.__timer.timeout.connect(self.capturing)
def capturing(self):
# self.__timer.setInterval(1000.0 / self.__fps)
frame = self.captureScreen()
self.videoWrite(self.__video,frame)
def setFps(self, fps=25.0):
self.__fps = fps
def setFilename(self, filename):
self.__filename = filename
# 1' create a videowriter
# 2' set timer intervel
# 3' start timer
def startBtn_clicked(self):
self.stopBtn.setEnabled(True)
self.startBtn.setEnabled(False)
self.__video = cv2.VideoWriter(self.__filename, self.__codecs, self.__fps, (self.__width, self.__height), True) # Make a video writer
self.__timer.start(1000/self.__fps)
def stopBtn_clicked(self):
self.__timer.stop()
self.__video.release()
self.stopBtn.setEnabled(False)
self.startBtn.setEnabled(True)
def captureScreen(self):
# capture the screen
im = ImageGrab.grab()
# change the frame from Image to nparray
frame = np.array(im)
# convert the color from BGR to RGB
frame2 = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
return frame2
def videoWrite(self,video,frame):
if not type(video)==type(cv2.VideoWriter()):
raise TypeError('video type ERROR')
if not isinstance(frame,np.ndarray):
raise TypeError('frame type ERROR')
video.write(frame)
if __name__ == '__main__':
'''
main function
'''
app = QApplication(sys.argv)
mainDialog = MainDialog()
mainDialog.show()
sys.exit(app.exec_())
听起来您的源FPS与输出FPS不同。也许捕获和保存图像所需的时间比FPS长(我希望如此),并且如果系统忙,QTimer不会进行任何调用。。。这听起来可能更难,但也许您可以使用回调调用抓取图像并将其放入队列中,由另一个线程处理?谢谢。如果我减少fps,那么时间可能是正确的?如果我想获得更高的fps视频,我需要照你说的做?另一个问题,我想写一个屏幕记录程序,最好的解决方案是什么?我认为它应该至少有两个线程,一个用来抓取并放入队列,另一个用来写,但一般来说,这并不清楚,因为如果抓取的线程比另一个线程快,那么这可能会在一段时间后消耗大量内存。非常感谢您。我试试看。
# -*- coding: utf-8 -*-
__author__ = '350187947@qq.com'
import sys
from PyQt5.QtWidgets import QApplication, QDialog
from PyQt5.QtCore import Qt, QTimer
from ui import *
from PIL import ImageGrab
import numpy as np
import cv2
class MainDialog(QDialog,Ui_mainDialog):
def __init__(self,parent=None):
super(MainDialog,self).__init__(parent)
# UI properties
self.setupUi(self)
self.setWindowFlags(Qt.WindowCloseButtonHint)
self.stopBtn.setEnabled(False)
# video parameter
self.__fps = 25.0
self.__filename = "1stTest.avi"
self.__codecs = cv2.VideoWriter_fourcc(*"XVID")
im = ImageGrab.grab() # capture the screen
frame = np.array(im) # change the frame from Image to np array
self.__height, self.__width, self.__depth = frame.shape # get the shape of the frame
print(self.__height, self.__width, self.__depth)
self.__timer = QTimer(self)
self.__timer.timeout.connect(self.capturing)
def capturing(self):
# self.__timer.setInterval(1000.0 / self.__fps)
frame = self.captureScreen()
self.videoWrite(self.__video,frame)
def setFps(self, fps=25.0):
self.__fps = fps
def setFilename(self, filename):
self.__filename = filename
# 1' create a videowriter
# 2' set timer intervel
# 3' start timer
def startBtn_clicked(self):
self.stopBtn.setEnabled(True)
self.startBtn.setEnabled(False)
self.__video = cv2.VideoWriter(self.__filename, self.__codecs, self.__fps, (self.__width, self.__height), True) # Make a video writer
self.__timer.start(1000/self.__fps)
def stopBtn_clicked(self):
self.__timer.stop()
self.__video.release()
self.stopBtn.setEnabled(False)
self.startBtn.setEnabled(True)
def captureScreen(self):
# capture the screen
im = ImageGrab.grab()
# change the frame from Image to nparray
frame = np.array(im)
# convert the color from BGR to RGB
frame2 = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
return frame2
def videoWrite(self,video,frame):
if not type(video)==type(cv2.VideoWriter()):
raise TypeError('video type ERROR')
if not isinstance(frame,np.ndarray):
raise TypeError('frame type ERROR')
video.write(frame)
if __name__ == '__main__':
'''
main function
'''
app = QApplication(sys.argv)
mainDialog = MainDialog()
mainDialog.show()
sys.exit(app.exec_())