I'm trying to create a simple screen recorder with Python. This is the code
import cv2
import numpy as np
import pyautogui
import time
SCREEN_SIZE = (1920, 1080)
fourcc = cv2.VideoWriter_fourcc(*"MP4V")
out = cv2.VideoWriter("output.mp4v", fourcc, 20.0, (SCREEN_SIZE))
fps = 120
prev = 0
print('================= ScreenRecording Started =================')
while True:
time_elapsed = time.time() - prev
img = pyautogui.screenshot()
if time_elapsed > 1.0/fps:
prev = time.time()
frame = np.array(img)
frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
out.write(frame)
if cv2.waitKey(100) & 0xFF == ord('q'):
break
else:
break
cv2.destroyAllWindows()
out.release()
It doesn't give any error while it's going, and it creates a .mp4v file correctly, however when I try to watch the video that was supposed to be recorded, it can't be opened. I tried with VLC and other apps but it's unsupported everywhere.
Can someone tell me why?