BoboTiG / python-mss

An ultra fast cross-platform multiple screenshots module in pure Python using ctypes.
https://pypi.org/project/mss/
MIT License
998 stars 88 forks source link

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

Open hash3liZer opened 3 years ago

hash3liZer commented 3 years ago

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?

Upvote & Fund

Fund with Polar