ra1nty / DXcam

A Python high-performance screen capture library for Windows using Desktop Duplication API
MIT License
457 stars 67 forks source link

Multi-screen recording failing #64

Open jscanzano opened 1 year ago

jscanzano commented 1 year ago

Hello! Thanks for this great tool!

I'm having trouble recording videos from multiple screens at once- it will run fine for up to several minutes then spontaneously freeze without throwing any errors. See the following code. After some troubleshooting I've found the script is getting stuck on the frame1 = cam1.get_latest_frame() line. I thought video_mode=True is non-blocking though. Any ideas what's going on?

One note: it's important in my application that accurate timestamps are saved for each frame, so I call get_latest_frame() for each screen before any are written to disk in order to keep the buffer grabs close in time. If there's a better way of doing this please let me know. And thanks for the help.

#create cameras
cam1 = dxcam.create(output_idx=2, output_color="BGR")
cam2 = dxcam.create(output_idx=1, output_color="BGR")
cam3 = dxcam.create(output_idx=3, output_color="BGR")

#input settings
target_fps = 60
output_path = "G:/posevideos/test/screen1"

#start cameras
cam1.start(target_fps=target_fps,video_mode=True)
cam2.start(target_fps=target_fps,video_mode=True)
cam3.start(target_fps=target_fps,video_mode=True)

#outpaths for each video
vidoutpath1 =  os.path.join(output_path, "screencam1.mp4")
vidoutpath2 =  os.path.join(output_path, "screencam2.mp4")
vidoutpath3 =  os.path.join(output_path, "screencam3.mp4")

#writers for each video
writer1 = cv2.VideoWriter(vidoutpath1,cv2.VideoWriter_fourcc(*"mp4v"),target_fps,(1024,600))
writer2 = cv2.VideoWriter(vidoutpath2,cv2.VideoWriter_fourcc(*"mp4v"),target_fps,(1024,600))
writer3 = cv2.VideoWriter(vidoutpath3,cv2.VideoWriter_fourcc(*"mp4v"),target_fps,(1024,600))

#record loop
print("Press q key to end recording.")
while(True):

  #grab a frame from every buffer.
  #timestamp recorded here
  frame1 = cam1.get_latest_frame()
  frame2 = cam2.get_latest_frame()
  frame3 = cam3.get_latest_frame()

  #once all frames are grabbed, save them
  w1 = writer1.write(frame1)
  w2 = writer2.write(frame2)
  w3 = writer3.write(frame3)

  #check for end keypress
  if keyboard.is_pressed('q'): 
      print(" ")
      break

#stop and release the resources
cam1.stop()
cam2.stop()
cam3.stop()
writer1.release()
writer2.release()
writer3.release()
jscanzano commented 1 year ago

One extra piece of information- if I set video_mode=False then the first get_latest_frame() always blocks forever. Not sure if I'm doing something wrong, or...?

ismailgokhanuslu commented 2 weeks ago

same issue for me. any chance for a solution?