python-mss icon indicating copy to clipboard operation
python-mss copied to clipboard

MSS gives 4-5fps for video recording to be stored on disk

Open hash3liZer opened this issue 3 years ago • 0 comments

I've been trying to record a video using mss. As it claims, you can record a video at about more than 30, even 60 fps in some cases. However, i am finding it difficult to implement in practical.

I am using one of your examples from the documentation. I've extended it a bit to write a video on disk instead of showing it in a window using imshow method. Hope it explains the issue with mss.

So, this is my code:

import numpy as np
import cv2
from win32api import GetSystemMetrics
from mss.windows import MSS as mss

smonitor  = {"top": 0, "left": 0, "width": GetSystemMetrics(0), "height": GetSystemMetrics(1)}
codec       = cv2.VideoWriter_fourcc(*"vp80")
vwriter     = cv2.VideoWriter("file.webm", codec, 30.0, (GetSystemMetrics(0), GetSystemMetrics(1)))

with mss() as sct:
    try:
        while True:
           img = np.array(sct.grab(smonitor))
           vwriter.write(img)
    except KeyboardInterrupt:
           vwriter.release()

It does record the video. But as a matter of fact, this video won't be playable. It's a corrupted file. Now, apparently i need to run this line: cv2.cvtColor(frame, cv2.COLOR_BGR2RGB) to actually record the video. So, the code would become:


with mss() as sct:
    try:
        while True:
           img = cv2.cvtColor(np.array(sct.grab(smonitor)), cv2.COLOR_BGR2RGB)
           vwriter.write(img)
    except KeyboardInterrupt:
           vwriter.release()

This time it works, but the max frames per second would be like 4 or at max it would be 5. So, we are back to the orignal issue. So, my question in simple is, how to use mss for recording a video?

  • OS name: Windows
  • OS version: 10
  • OS architecture: x64
  • Python version: 3.9
  • MSS version: 6.1.0

Upvote & Fund

  • We're using Polar.sh so you can upvote and help fund this issue.
  • We receive the funding once the issue is completed & confirmed by you.
  • Thank you in advance for helping prioritize & fund our backlog.
Fund with Polar

hash3liZer avatar Aug 24 '21 14:08 hash3liZer