使用Python进行自动化录屏

本文将展示如何在有另一个Python进程运行时,使用Python自动录制屏幕生成视频,并用以检查其执行情况。

大多数时候,当需要对非常老旧的系统进行自动化时,将不得不通过GUI来完成,因为它们没有API,GUI自动化可能非常棘手并且会出现意外行为。

解决这个问题的最好方法是尝试处理代码中的每一个可能的异常,但也可能会发生意外错误。所以当发生异常时,最好保存一个异常情况的视频来以便于分析,并调试代码。

使用Python的多进程库,在与运行自动化的线程不同的线程中,运行屏幕录制脚本。已经用三个不同的库测试了这个脚本的屏幕录制:MssPillow,和Pyautogui。在这些库中,Mss是表现最好的一个。

以下是固定时间录制的代码片段:

from time import sleep
from mss.windows import MSS as mss 
import multiprocessing
import pyautogui
import cv2
import numpy as np
from os import remove


class FixedTimeCapture:

    def __init__(self, capture_time:int, video_path:str='C:/test') -> None:
        self.fps = 15
        self.capture_time = capture_time
        self.video_path = video_path.replace('\\','/') + '.avi'

        self.process = multiprocessing.Process(
            target=self.capture_screen,
            args=(),
            name='Screen Capture'
        )  

    def capture_screen(self):
        SCREEN_SIZE = tuple(pyautogui.size())
        fourcc = cv2.VideoWriter_fourcc(*'XVID')
        out = cv2.VideoWriter(self.video_path, fourcc, self.fps, (SCREEN_SIZE))
        print(f'Screen capture started')

        w, h = pyautogui.size() 
        monitor = {'top':0, 'left':0, 'width':w, 'height':h}  
        with mss() as sct:
            for i in range(int(self.capture_time * self.fps)):
                try:
                    img = sct.grab(monitor=monitor) 
                    frame = np.array(img)
                    frame = cv2.cvtColor(frame, cv2.COLOR_BGRA2BGR)
                    out.write(frame)
                except IOError:                    
                    pass   
            cv2.destroyAllWindows()
            out.release()
            sleep(3)
            print('Screen capture finished')

    def start_capture(self):
        self.process.start()

    def abort_capture(self):
        if not self.process.is_alive():
            print('The process is not executing')
        else:
            print('Aborting screen capture')
            self.process.kill()
            sleep(3)
            try:
                remove(self.video_path)
            except:
                print('Couldnt delete the file')


if __name__ == '__main__':
    ct = FixedTimeCapture(capture_time=10, video_path=r'C:\test')
    ct.start_capture()

    # 如果想中止录制并在其完成之前删除文件
    # ct = FixedTimeCapture(capture_time=10, video_path=r'C:\test2')
    # ct.start_capture()
    # sleep(5)
    # ct.abort_capture()

请注意,其中包含了一个选项来中止进程并在必要时删除文件。使用的是AVI视频编解码器,录制速度为15帧/秒,但可以自行更改这些设置。

不知道录制的过程要花多长时间的情况下,可以创建一个函数来开始录制,另一个函数来结束录制。

from time import sleep
from mss.windows import MSS as mss 
import multiprocessing
import pyautogui
import cv2
import numpy as np
from os import remove


class CaptureScreen:

    def __init__(self, video_path:str) -> None:
        self.fps = 15
        self.video_path = video_path.replace('\\','/') + '.avi'

        self.process = multiprocessing.Process(
             target=self.capture_screen,
             args=(),
             name='Screen Capture'
        )  


    def capture_screen(self):

        SCREEN_SIZE = tuple(pyautogui.size())
        fourcc = cv2.VideoWriter_fourcc(*'XVID')
        out = cv2.VideoWriter(self.video_path, fourcc, self.fps, (SCREEN_SIZE))
        img = None
        print(f'Screen capture started')

        # MSS
        w, h = pyautogui.size() 
        monitor = {'top':0, 'left':0, 'width':w, 'height':h}      
        with mss() as sct:        
            while True:      
                try:                
                    try:               
                        img = sct.grab(monitor=monitor)         
                        frame = np.array(img)               
                        frame = cv2.cvtColor(frame, cv2.COLOR_BGRA2BGR)                 
                        out.write(frame)                                 
                    except IOError:                    
                        pass            
                except KeyboardInterrupt:                
                    break
            cv2.destroyAllWindows()
            out.release()

        # # PILLOW 
        # from PIL import ImageGrab
        # w, h = pyautogui.size()           
        # while True:        
        #     try:   
        #         try:      
        #             img = ImageGrab.grab(bbox =(0, 0, w, h))         
        #             frame = np.array(img)                  
        #             frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)                 
        #             out.write(frame)   
        #         except IOError:                    
        #             pass                                               
        #     except KeyboardInterrupt:                
        #         break
        # cv2.destroyAllWindows()
        # out.release()

        # # PYAUTOGUI
        # while True:
        #     try:
        #         try:   
        #             img = pyautogui.screenshot()
        #             frame = np.array(img)
        #             frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
        #             out.write(frame)
        #         except IOError:                    
        #             pass  
        #     except KeyboardInterrupt:
        #         break
        # cv2.destroyAllWindows()
        # out.release()

    def start_capture(self):
        self.process.start()

    def finish_capture(self):
        if not self.process.is_alive():
            print('The process is not executing')
        else:
            self.process.terminate()
            sleep(3)
            print('Screen capture finished')


if __name__ == '__main__':
    c = CaptureScreen(video_path=r'C:\test')
    c.start_capture()
    sleep(15)
    c.finish_capture()

在这个例子中,还包含了PillowPyautogui的实现,如果想进行一下测试,需要调整帧率以获得100%完美的视频再现速度。

  • 4
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

python慕遥

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值