May 2, 2024, 11:02
rpi_port = 3148 context = zmq.Context() socket = context.socket(zmq.SUB) socket.bind(f'tcp://*:{rpi_port}') trial_id = "none" msg = "none" socket.setsockopt_string(zmq.SUBSCRIBE,"") output_folder = "Video_rpi/" res=(2304,1296) framerate= 30.0 frame_duration = int(1_000_000 / framerate) frontcam = Picamera2(0) sidecam = Picamera2(1) #config_front = sidecam.create_video_configuration() frontcam.video_configuration.controls.FrameRate = framerate frontcam.video_configuration.size = res #config_side = sidecam.create_video_configuration() sidecam.video_configuration.controls.FrameRate = framerate sidecam.video_configuration.size = res #frontcam.configure(config_front) #frontcam.set_controls({"FrameRate": 30}) #sidecam.configure(config_side) #sidecam.set_controls({"FrameRate": 30}) encoder_front = H264Encoder(bitrate=17000000) encoder_side = H264Encoder(bitrate=17000000) socket.RCVTIMEO = 10_000
while True : trial_id="" msg = "" try: print('hello') string= socket.recv().decode() trial_id, msg = string.split(";") except Exception as error: print('msg not received'+ str(error)) print('trial: ',trial_id) print('msg: ',msg) if msg == "start_recording": output_name_front = output_folder +trial_id+"_front.mp4" output_name_side = output_folder+ trial_id+"_side_0.mp4" output_front = FfmpegOutput(output_name_front, audio=False) output_side = FfmpegOutput(output_name_side, audio=False) front_start = time.time() frontcam.start_recording(encoder_front,output_front) side_start = time.time() sidecam.start_recording(encoder_side,output_side) start_diff = side_start-front_start try: print('hello') string= socket.recv().decode() trial_id, msg = string.split(";") except Exception as error: print('msg not received'+ str(error)) print('trial: ',trial_id) print('msg: ',msg) side_end = time.time() sidecam.stop_recording() front_end = time.time() frontcam.stop_recording() end_diff = side_end - front_start first_frame = int(start_diff*framerate) os.rename(output_name_front, output_folder +trial_id+"_front_"+str(first_frame)+".mp4") time.sleep(0.001) socket.setsockopt(zmq.LINGER, 0) socket.close() context.term()