Skip to content

Instantly share code, notes, and snippets.

@nikitinvv
Created April 10, 2023 20:20
Show Gist options
  • Save nikitinvv/60fa7d7b4052ca5daff6059843b9a522 to your computer and use it in GitHub Desktop.
Save nikitinvv/60fa7d7b4052ca5daff6059843b9a522 to your computer and use it in GitHub Desktop.
# syntax: python record_screen.py <output_file_name.avi> <fps> <monitor_id>
# example: python record_screen.py /local/test.avi 20 1
# on user2bmb@arcturus
# conda activate screenrecording
# (screenrecording) user2bmb@arcturus ~/vnikitin $python record_screen.py /local/test.avi 20 3
from mss import mss
from PIL import Image
import cv2
import time
import numpy as np
import sys
def capture_screenshot(mid):
# Capture entire screen
with mss() as sct:
#print(sct.monitors)
monitor = sct.monitors[mid]
sct_img = sct.grab(monitor)
return Image.frombytes('RGB', sct_img.size, sct_img.bgra, 'raw', 'RGBX')
output = sys.argv[1]#"video.avi"
fps = int(sys.argv[2])#20
monitor_id = int(sys.argv[3])#1
width,height =capture_screenshot(monitor_id).size
fourcc = cv2.VideoWriter_fourcc(*'mp4v')
out = cv2.VideoWriter(output, fourcc, fps , (width, height))
print(f'{fps=},{monitor_id=},{output=}')
t = time.time()
while True:
try:
image=capture_screenshot(monitor_id)
out.write(np.asarray(image))
mtime = time.time()-t
print(f'\rrecording {mtime:0.1f}s', end="\r")
except KeyboardInterrupt:
print('done')
break
out.release()
cv2.destroyAllWindows()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment