what is screen recording?
Screen recording permits you to make demonstration videos, record play achievements, and create videos that can be shared online on social media. abounding industrial package exists out there which can assist you are attempting this very merely although. throughout this tutorial, can learn the thanks to kind your own simple screen recorder in Python that you merely that you just simply will further be your own needs.
Let's begin, first, install the required dependencies for this tutorial:
pip3 install numpy OpenCV-python pyautogui
The process is as follows:
Capture a screenshot practice pyautogui.
Convert that screenshot to a numpy array.
Write that numpy array to a file with the proper format using a video author in OpenCV.
Importing necessary modules:
import cv2
import numpy as np
import pyautogui
Let's initialize the format we have a tendency to tend to gonna use to write down down our video file ( named "output.avi" ):
# visual show unit resolution, apprehend from your OS settings
SCREEN_SIZE = (1920, 1080)
# define the codec
fourcc = cv2.VideoWriter_fourcc(*"XVID")
# manufacture the video write object
out = cv2.VideoWriter("output.avi", fourcc, 20.0, (SCREEN_SIZE))
Note: you would like to induce the proper SCREEN_SIZE from your code package, that is the screen resolution, otherwise writing to the file won't work (alternatively, you may use pyautogui. size() perform to induce the size of the primary monitor).
fourcc is that the video codec library that OpenCV will use to write down down the video file, we have a tendency to tend to nominative XVID here. The 20.0 float price passed as the third parameter to cv2.VideoWriter corresponds to the Federal Protective Service (Frame Per Second).
Now we wish to remain capturing screenshots and writing to the enter a loop until the user clicks the "q" button, here is that the most loop for that:.
while True:
# build a screenshot
img = pyautogui.screenshot()
# convert these pixels to an accurate numpy array to work with OpenCV
frame = np.array(img)
frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
# write the frame
out.write(frame)
# show the frame
cv2.imshow("screenshot", frame)
# if the user clicks letter of the alphabet, it exits
if cv2.waitKey(1) == ord("q"):
break
# ensure everything is closed once exited
cv2.destroyAllWindows()
out.release()
First, we have a tendency to tend to use the screenshot() perform that returns an image object, so we wish to convert it to an accurate numpy array. After that, we wish to convert that frame into RGB, which's a result of OpenCV uses BGR by default.
As mentioned in pyautogui's official documentation, you may put together record only regions of your screen, bypassing region keyword argument that would be a four-integer tuple representing the best, left, dimension, and height of the region to capture, here is but it's done:
img = pyautogui. screenshot(region=(0, 0, 300, 400))
After you are finished recording, merely click "q", it will destroy the window and finish writing to the file, attempt it out!
Also, you may replace the whereas statement with a for loop as follows:
for i in range(200):
# build a screenshot
img = pyautogui.screenshot()
# the rest of the code...
This will record your screen for 10 seconds, that's as a result of we have a tendency to tend to line the Federal Protective Service twenty to twenty (which is sensible as a result of 200 is twenty times 10)
#somaymangla #somaymangla #somaymangla
#somaymangla
#somaymangla
Author
No comments:
Post a Comment