I'm trying to make a video based on an RTSP stream, opencv creates the file correctly, but when opencv saves frames in this file, the video file quickly shows frames in a short time. In my test, I record frames for 10 seconds, after that I stop recording, when I go to check the video file, what is displayed is a video with duration of 2 seconds, but all recorded frames are present in the file.
My thread to save frames
self.rec = False
self.thread = Thread(target=self.grava_frame, args=())
self.thread.daemon = True
self.thread.start()
def grava_frame(self):
while True:
if self.rec:
#self.rec = False
while not self.thread_end:
frame = cv2.cvtColor(self.resized_cropped, cv2.COLOR_RGB2BGR)
self.video_writer.write(frame)
cv2.waitKey(1)
self.video_writer.release()
My thread to capture frames:
self.at_frame = Thread(target=self.atualiza_frame, args=())
self.at_frame.daemon = True
self.at_frame.start()
def atualiza_frame(self):
ap = argparse.ArgumentParser()
ap.add_argument("-v", "--video", required=True,
help="rtsp://admin:@192.168.1.10:554/live/0/MAIN")
args = vars(ap.parse_args())
print("[INFO] starting video file thread...")
fvs = FileVideoStream(args["video"]).start()
time.sleep(1.0)
# start the FPS timer
fps = FPS().start()
# loop over frames from the video file stream
while fvs.more():
frame = fvs.read()
self.resized_cropped = cv2.resize(frame, (1024, 600))
cv2.waitKey(1)
fps.update()
This is the button that control de record:
def record_button(self):
if self.is_recording_on:
# inicia a gravação
data = self.dataname()
self.codec = cv2.VideoWriter_fourcc(*'mp4v')
self.video_writer = cv2.VideoWriter(data+'.MP4', self.codec, 25, (self.frame_width, self.frame_height))
self.thread_end = False
self.rec = True
else:
# encerra a gravação
self.rec = False
self.thread_end = True
I suspect that my thread is writing frames too quickly, but I don't know how to solve this
EDIT: Based on the comments, i put a 'print("frame")' inside the 'while not self.thread_end'. I record a video during 5 seconds, and the output of 'frame' printed is: 30 print in 5 seconds of recording