Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to make screen recording tool based on Python+OpenCV

2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article analyzes how to make screen recording tools based on Python+OpenCV. The content is detailed and easy to understand. Friends interested in "How to Make Screen Recording Tools Based on Python+OpenCV" can read it slowly and deeply according to the idea of Xiaobian. I hope it can help everyone after reading. Let's learn more about how to make screen recording tools based on Python+OpenCV.

application platform

windows 10

python 3.7

screen recording section

Screen recording can be simply understood as playing a screenshot in the form of an animation. Here I choose ImageGrab under PIL to capture the screen. First,

pip install Pillow

After that, you need to combine the captured snapshots into a video, using the cv2 module.

pip install opencv-python

ImageGrab class can not be stored directly as video, use numpy module for array, and then through cv2.COLOR_BGR2RGB conversion to cv2 color channels.

pip install numpy

Screen Recording Main Code:

import numpy as npfrom PIL import ImageGrabimport cv2im = ImageGrab.grab()width, high = im.size #Get width and height of screen fourcc = cv2.VideoWriter_fourcc(*'I420')#Set video encoding format fps = 15 #Set frame rate video = cv2.VideoWriter ('test.avi', fourcc, fps, (width, high))while True: #Start recording im = ImageGrab.grab() im_cv = cv2.cvtColor(np.array(im), cv2.COLOR_BGR2RGB) #Image Writing video.write(im_cv) if xx: #interrupt loop when certain condition is satisfied breakvideo.release() #release cache, persist video

Test runs can save screenshots as video, but they are not elegant and do not facilitate subsequent operations.

Encapsulated into a class, inherited from the parent class thread, convenient to use the keyboard to control the end of video recording.

from threading import Threadclass ScreenshotVideo(Thread): def __init__(self): """Initialize Parameters""" super().__ init__()

Detailed codes are given at the end of this article.

Calculate optimal fps for video and intermediate frame arrays using numpy

In actual operation, video recording in different computers will appear different frame rates, resulting in video playback or fast or slow, need to calculate the corresponding optimal fps value according to different computers.

def video_best_fps(self, path): """Get the optimal frame rate for PC recorded video""" video = cv2.VideoCapture(path) #Read Video fps = video.get(cv2.CAP_PROP_FPS) #Get frame rate of current video count = video.get(cv2.CAP_PROP_FRAME_COUNT) #Get the number of frames in the video, that is, how many pictures there are in the video self.best_fps = int(fps * ((int(count) / fps) / self.spend_time)) #Calculate the playback time and record time to get the optimal frame rate video.release()

Adjusting the frame rate parameters to record the video reduces the video playing too fast or too slow. You can also add frames to a video to extend playback time, but here I'm using a very simple way to add frames to a video, just for reference.

from numba import jit#Compute two adjacent frames closer to the next frame using numpy #Call jit method to speed up array computation @jit(nopython=True)def average_n(x, y): ""Numpy Computes Approach Value """ return ((x + y + y) // 3).astype(x.dtype)

This method is only aimed at the video perception after processing when the fps is higher than the optimal fps, but the detailed frames are increased, so the playback time will be longer than that before processing, with a slight afterimage.

Use pynput to listen for keyboard keys

In video recording, you don't know when the video ends, so use while loop to wrap the recording code, but it is impossible to let the code run endlessly. Here, use the monitor keyboard module to interrupt the recording code.

from pynput import keyboard # pip install pynputdef hotkey(self): """Hotkey Listening""" with keyboard.Listener(on_press=self.on_press) as listener: listener.join()def on_press(self, key): try: if key.char == 't': #End of screen recording, save video self.flag = True elif key.char == 'k':#screen recording aborted, delete file self.flag = True self.kill = True except Exception as e: print(e)

Press the keyboard "T" key to end recording and save the video. The "K" key stops recording and deletes cache files.

How to Save MP4 Video

The video encoding format should be ('a ', ' v','c',' 1'), and the file suffix should be '.mp4'. Before recording, download the dll.bz2 file of the corresponding platform from https://github.com/cisco/openh364/releases, extract the compressed package and put it in the project folder. Run the code again, and a line of code will appear successfully:

OpenH264 Video Codec provided by Cisco Systems, Inc.

source code

The source code implemented in this article is as follows:

import timefrom PIL import ImageGrabimport cv2from pathlib import Pathimport numpy as npfrom numba import jitfrom pynput import keyboardfrom threading import Thread@jit(nopython=True)def average_n(x, y): ""Numpy Computes Approach Value """ return ((x + y + y) // 3).astype(x.dtype)class ScreenshotVideo(Thread): def __init__(self, width, high, path='', fps=15): """Initialize Parameters""" super().__ init__() self.save_file = path self.best_fps = fps self.fps = fps self.width = width self.high = high self.spend_time = 1 self.flag = False self.kill = False self.video = None def __call__(self, path): """Overload video path for secondary invocation of class""" self.save_file = Path(path) self.video = self.init_videowriter(self.save_file) @staticmethod def screenshot(): """Static methods, screenshots, and conversion to np.array""" return np.array(ImageGrab.grab()) @staticmethod def get_fourcc(name): """Video Coding Dictionary""" fourcc_maps = {'.avi': 'I420', '.m4v': 'mp4v', '.mp4': 'avc1', '.ogv': 'THEO', '.flv': 'FLV1', } return fourcc_maps.get(name) def init_videowriter(self, path): """Get video encoding and create new video file""" if not path: raise Exception ('Video path not set, please set\nvideo = ScreenshotVideo(fps,width,high)\nvideo = video(video_path)') path = Path(path) if isinstance(path, str) else path fourcc = cv2.VideoWriter_fourcc(*self.get_fourcc(path.suffix)) return cv2.VideoWriter(path.as_posix(), fourcc, self.fps, (self.width, self.high)) def video_record_doing(self, img): """Convert BGR Array to RGB Array"" im_cv = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) self.video.write(im_cv) def video_record_end(self): """Recording ends, judge whether the file is saved according to the conditions""" self.video.release() cv2.destroyAllWindows() if self.save_file and self.kill: Path(self.save_file).unlink() def video_best_fps(self, path): """Get the optimal frame rate for PC recorded video""" video = cv2.VideoCapture(path) fps = video.get(cv2.CAP_PROP_FPS) count = video.get(cv2.CAP_PROP_FRAME_COUNT) self.best_fps = int(fps * ((int(count) / fps) / self.spend_time)) video.release() def pre_video_record(self): """Pre-record for optimal fps values"" self.video = self.init_videowriter('test.mp4') start_time = time.time() for _ in range(10): im = self.screenshot() self.video_record_doing(im) self.spend_time = round(time.time() - start_time, 4) self.video_record_end() time.sleep(2) self.video_best_fps('test.mp4') Path('test.mp4').unlink() def insert_frame_array(self, frame_list): """Numpy enhanced screenshot information""" fps_n = round(self.fps / self.best_fps) if fps_n

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report