Read rtmp live streaming video using python

1.5k Views Asked by At

I am streaming a session using rtmp server(NGINX). I got the stream url as rtmp://ip:port/live/stream_name. How can I read the live stream in my python code(or any other) to do live transcription?

1

There are 1 best solutions below

0
skylighty On

You have to use one of libraries that allow such process, one of the most commonly used is OpenCV.

Install through pip:

python3 -m pip install opencv-python

Code sample

import cv2  # Import OpenCV lib

cap = cv2.VideoCapture('<your RTMP url>') # Open video source as object
while cap.isOpened():  # Untill end of file/error occured
    ret, frame = cap.read()  # Read frame as object - numpy.ndarray, ret is a confirmation of a successfull retrieval of the frame
    if ret: 
        <your code here>
    else:
        break
cap.release()