ubuntu – Minimal Latency for RTMP Livestream Viewing in OpenCV

I’m currently reading an RTMP stream from localhost RTMP server on Ubuntu 22.04 using the default version of OpenCV installed via apt. When I use ffplay, the stream plays with minimal (almost no) latency using the following command:

ffplay -f live_flv \
    -fast \
    -fflags nobuffer \
    -flags low_delay \
    -strict experimental \
    -vf "setpts=N/30/TB" \
    -noframedrop \
    -i "rtmp://localhost:1935/live"

However, when I attempt to read the same stream with OpenCV, I notice some latency. Here’s the code snippet:

cap = cv2.VideoCapture("rtmp://localhost:1935/live buffer=0")
while cap.isOpened():
    success, frame = cap.read()
    if not success:
        continue
    cv2.imshow("window", frame)
    if cv2.waitKey(1) & 0xFF == ord("q"):
        break
cap.release()
cv2.destroyAllWindows()

Based on some suggestions I found online, I tried setting CAP_PROP_BUFFERSIZE to 1 and added buffer=0 to the stream URL. However, there is still noticeable latency compared to ffplay.

How to achieve the same low (almost no) latency with OpenCV as I do with ffplay?

Read more here: Source link