Very low fps and frame drop in screen recording?

Viewed 77

Hi guys i'm building a screen recorder, it works and it creates the "output" video of the screen recording but there is one problem, when i run the program i notice a lot of frame drops when i drag and move a window for example, and in the final file, the video output, the FPS are not right i mean there are lags every time i move a window when i watch the final video, i tried to change the "fps" value of the program but the frame drops still remain, i have a monitor with 1920x1080 resolution with 120 fps so it's very smooth, how to solve this in order to have a fluid output video? Thanks a lot

import cv2
import numpy as np
from  PIL import ImageGrab
def screenRec():
    fourcc = cv2.VideoWriter_fourcc(*"XVID")
    fps = 8.0
    out = cv2.VideoWriter("output.mp4",fourcc,fps,(1920,1080))
    while (True):
        img = ImageGrab.grab()
        img_np = np.array(img)
        frame = cv2.cvtColor(img_np, cv2.COLOR_BGR2RGB)
        out.write(frame)
screenRec()
1 Answers

What about trying DivX MPEG-4 codification?

Replace this line

 fourcc = cv2.VideoWriter_fourcc(*"XVID")

With

fourcc = cv2.VideoWriter_fourcc(*"DIVX")

Another codecs you could try are MP4v (H264 predecessor) and H264/H265 encoding (for this buds you'll have to manually build the OpenCV library which steps are described here

Some of the information is extracted from this post Save video in opencv with H264 codec

This new one gives you more fps and has a sinchronization with waitKey of 0.05 before printing the frame.

import cv2
import numpy as np
from  PIL import ImageGrab
def screenRec():
    fourcc = cv2.VideoWriter_fourcc(*"XVID")
    fps = 20
    out = cv2.VideoWriter("output.mp4",fourcc,fps,(1920,1080))
    while (True):
        img = ImageGrab.grab()
        img_np = np.array(img)
        frame = cv2.cvtColor(img_np, cv2.COLOR_BGR2RGB)
        out.write(frame)
        cv2.waitKey(50)
screenRec()
Related