Advertisement
Guest User

Untitled

a guest
Jun 1st, 2020
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.31 KB | None | 0 0
  1. import sys
  2.  
  3. import numpy as np
  4. import cv2
  5. from mss.windows import MSS as mss
  6. from PIL import Image
  7.  
  8.  
  9. class Capturer:
  10.  
  11. def __init__(self, w, h, resolution='1080p', fps=24.975):
  12. if resolution == '1080p':
  13. self.monitor = {
  14. 'top': 1080 - h,
  15. 'left': 1920 - w,
  16. 'width': w,
  17. 'height': h
  18. }
  19.  
  20. elif resolution == '1440p':
  21. self.monitor = {
  22. 'top': 1440 - h,
  23. 'left': 2560 - w,
  24. 'width': w,
  25. 'height': h
  26. }
  27.  
  28. else:
  29. raise ValueError('Unsupported monitor resolution')
  30.  
  31. self.sct = mss()
  32.  
  33. self.vid = cv2.VideoWriter(
  34. 'output.avi',
  35. cv2.VideoWriter_fourcc(*'XVID'),
  36. fps=fps,
  37. frameSize=(
  38. self.monitor['width'],
  39. self.monitor['height']
  40. )
  41. )
  42.  
  43. def mainloop(self):
  44. while True:
  45. sct_img = self.sct.grab(self.monitor)
  46. sct_img_size = (sct_img.width, sct_img.height)
  47.  
  48. img = Image.frombytes(
  49. 'RGB',
  50. sct_img_size,
  51. sct_img.rgb
  52. )
  53.  
  54. frame = cv2.cvtColor(
  55. np.array(img),
  56. cv2.COLOR_BGR2RGB
  57. )
  58.  
  59. self.vid.write(frame)
  60.  
  61.  
  62. if __name__ == '__main__':
  63. if len(sys.argv) != 3:
  64. print(f'Usage: {sys.argv[0]} <WIDTH> <HEIGHT>')
  65. sys.exit()
  66.  
  67. w, h = (int(x) for x in sys.argv[1:3])
  68. capturer = Capturer(w, h, fps=50.0)
  69.  
  70. try:
  71. capturer.mainloop()
  72. except KeyboardInterrupt:
  73. print('Interrupted')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement