3

I have several streams. I try to write them altogether the the disk. This is the script i use -

import cv2
import os

output_dir = "/home/Desktop/stream_rec"

# Load Streams
input_cap = {}
output_cap = {}

fourcc = cv2.VideoWriter_fourcc(*'XVID')

for i in range(2, 6):
    input_cap[i] = cv2.VideoCapture("rtsp://stream@10.0.0.{}".format(i))
    output_cap[i] = cv2.VideoWriter(os.path.join(output_dir, f'D{i}.mp4'), fourcc, 25.0, (1080,1920)[::-1])


# Load Frames
# while all([stream.isOpened() for stream_id, stream in streams.items()]):
while True:

    for stream_id, stream in input_cap.items():

        # Read
        ret, frame = stream.read()

        # Write
        output_cap[stream_id].write(frame)

        # Show
        # frame = cv2.resize(frame, (int(frame.shape[1]/2), int(frame.shape[0]/ 2)))
        if ret:
            cv2.imshow(str(stream_id), frame)
        else:
            break

    if cv2.waitKey(1) & 0xFF == ord('q'): # Press Q to stop recording
        break

# Release Caps
for i in range(2, 6):
    input_cap[i].release()
    output_cap[i].release()

When i only show the streams it looks real time. But when i try to write them to disk as well, frame rate drops significantly. How can i show 4 streams and write them to disk with minimal impact on frame rate and latency?

nathancy
  • 42,661
  • 14
  • 115
  • 137
Alex Goft
  • 360
  • 3
  • 12
  • Use multi-threading to get I/O done in parallel. – Mark Setchell Feb 10 '20 at 18:32
  • 1
    Take a look at those links, I use 9 IP camera streams and get about ~60 FPS. The idea is to use multithreading with two threads. 1) To capture frames 2) To display frames. It will significantly improve performance due to I/O latency reduction – nathancy Feb 10 '20 at 21:15

0 Answers0