Skip to content

Commit

Permalink
Instructed changes on "screen recorder.py" (#21)
Browse files Browse the repository at this point in the history
Co-authored-by: Tanisha Lalwani <[email protected]>
  • Loading branch information
Yash-Vashishth and tanishaness authored Oct 8, 2024
1 parent 77fbec9 commit 9de5f2c
Showing 1 changed file with 51 additions and 20 deletions.
71 changes: 51 additions & 20 deletions Backend/screen_recorder.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,57 @@
from win32api import GetSystemMetrics
import datetime

width = GetSystemMetrics(0)
height = GetSystemMetrics(1)
def initialize_video_writer(filename, fourcc, frame_rate, frame_size):
return cv2.VideoWriter(filename, fourcc, frame_rate, frame_size)

time_stamp = datetime.datetime.now().strftime('%Y-%m-%d %H-%M-%S')
filename = f'{time_stamp}.mp4'
fourcc = cv2.VideoWriter_fourcc('m', 'p', '4', 'v')
captured_video = cv2.VideoWriter(filename, fourcc, 20.0, (width, height))
def capture_screen(width, height):
img = ImageGrab.grab(bbox=(0, 0, width, height))
img_np = np.array(img)
return cv2.cvtColor(img_np, cv2.COLOR_BGR2RGB)

webcam = cv2.VideoCapture(0)
def overlay_webcam_on_screen(screen_frame, webcam_frame):
frame_h, frame_w, _ = webcam_frame.shape
screen_frame[0:frame_h, 0:frame_w, :] = webcam_frame[0:frame_h, 0:frame_w, :]
return screen_frame

while True:
img = ImageGrab.grab(bbox = (0, 0, width, height)) # captures screen
img_np = np.array(img)
img_final = cv2.cvtColor(img_np, cv2.COLOR_BGR2RGB)

_, frame = webcam.read() # webcam capture
frame_h, frame_w, _ = frame.shape
img_final[0:frame_h, 0:frame_w, :] = frame[0:frame_h, 0:frame_w, :] # overlay webcam on screen
# cv2.imshow('webcam', frame)
cv2.imshow('Capture', img_final)
captured_video.write(img_final)
if cv2.waitKey(10) == ord('q'):
break
def main():
width = GetSystemMetrics(0)
height = GetSystemMetrics(1)

time_stamp = datetime.datetime.now().strftime('%Y-%m-%d %H-%M-%S')
filename = f'{time_stamp}.mp4'
fourcc = cv2.VideoWriter_fourcc('m', 'p', '4', 'v')
frame_rate = 20.0
frame_size = (width, height)

captured_video = initialize_video_writer(filename, fourcc, frame_rate, frame_size)
webcam = cv2.VideoCapture(0)

if not webcam.isOpened():
print("Error: Could not open webcam.")
return

try:
while True:
screen_frame = capture_screen(width, height)
ret, webcam_frame = webcam.read()

if not ret:
print("Error: Failed to capture webcam frame.")
break

final_frame = overlay_webcam_on_screen(screen_frame, webcam_frame)
cv2.imshow('Capture', final_frame)
captured_video.write(final_frame)

if cv2.waitKey(10) == ord('q'):
break
except Exception as e:
print(f"An error occurred: {e}")
finally:
webcam.release()
captured_video.release()
cv2.destroyAllWindows()

if __name__ == "__main__":
main()

0 comments on commit 9de5f2c

Please sign in to comment.