diff --git a/trap/frame_emitter.py b/trap/frame_emitter.py index 0f732b5..e960f85 100644 --- a/trap/frame_emitter.py +++ b/trap/frame_emitter.py @@ -99,7 +99,7 @@ class FrameEmitter: logger.info(f"Play from '{str(video_path)}'") video = cv2.VideoCapture(str(video_path)) fps = video.get(cv2.CAP_PROP_FPS) - frame_duration = 1./fps + target_frame_duration = 1./fps logger.info(f"Emit frames at {fps} fps") prev_time = time.time() @@ -126,13 +126,13 @@ class FrameEmitter: self.frame_sock.send(pickle.dumps(frame)) # defer next loop - new_frame_time = time.time() - time_diff = (new_frame_time - prev_time) - if time_diff < frame_duration: - time.sleep(frame_duration - time_diff) - new_frame_time += frame_duration - time_diff - else: - prev_time = new_frame_time + now = time.time() + time_diff = (now - prev_time) + if time_diff < target_frame_duration: + time.sleep(target_frame_duration - time_diff) + now += target_frame_duration - time_diff + + prev_time = now i += 1