1

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 Answers1

0

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()
ser356
  • 1
  • 3
  • Hi thanks for the answer, i tried with you codecs but is the same result, it seems more a problem with fps more than a codec problem, any other solutions for the terrrible low fps? – Forty966699669 Jun 11 '22 at 21:44
  • read my update from the answer box, hope this time it was useful. – ser356 Jun 12 '22 at 02:03
  • I tried but it seems the same result again, 20 fps i think are too much because the video is super fast, speeded up and still frame drops, maybe it should be a complete different program – Forty966699669 Jun 12 '22 at 06:52