6

I am trying to use this sample script I found here in order to take OpenCV images and convert them into a rtp/rtsp stream:
https://github.com/madams1337/python-opencv-gstreamer-examples/blob/master/gst_device_to_rtp.py

Where this is the description of the script:

"gst_device_to_rtp grabs the VideoCapture(0),encodes the frame and streams it to rtp://localhost:5000"

This is the code I am trying to use

# Cam properties
fps = 30.
frame_width = 1920
frame_height = 1080
# Create capture

#cap = cv2.VideoCapture(0)

# Set camera properties
cap.set(cv2.CAP_PROP_FRAME_WIDTH, frame_width)
cap.set(cv2.CAP_PROP_FRAME_HEIGHT, frame_height)
cap.set(cv2.CAP_PROP_FPS, fps)

# Define the gstreamer sink
gst_str_rtp = "appsrc ! videoconvert ! x264enc noise-reduction=10000 tune=zerolatency byte-stream=true threads=4 " \
              " ! h264parse ! mpegtsmux ! rtpmp2tpay ! udpsink host=127.0.0.1 port=5000"

# Create videowriter as a SHM sink
out = cv2.VideoWriter(gst_str_rtp, 0, fps, (frame_width, frame_height), True)

# Loop it
while True:
    # Get the frame
    ret, frame = cap.read()

    # Check
    if ret is True:
        # Flip frame
        frame = cv2.flip(frame, 1)
        # Write to SHM
        out.write(frame)
    else:
        print "Camera error."
        time.sleep(10)

cap.release()

Mainly this code, which specifies the gstreamer pipeline configuration:

# Define the gstreamer sink
gst_str_rtp = "appsrc ! videoconvert ! x264enc noise-reduction=10000 tune=zerolatency byte-stream=true threads=4 " \
              " ! h264parse ! mpegtsmux ! rtpmp2tpay ! udpsink host=127.0.0.1 port=5000"

# Create videowriter as a SHM sink
out = cv2.VideoWriter(gst_str_rtp, 0, fps, (frame_width, frame_height), True)

From what I understand, it is sending the OpenCV video images to "rtp://localhost:5000"

However, whenever I try to execute this terminal command in terminal while I leave the script running:

ffplay 'rtp://localhost:5000'

It just hangs forever like this: enter image description here And I can't really determine what that really means. Does it mean that it can connect to localhost at that port, but found nothing there? I don't really know. The command seems works if there is another with other rtsp urls, but not this one.

And if I try "ffplay 'rtsp://localhost:5000'", then I just get a Connection Refused error (maybe nothing is being generated at that stream)

Does the script really output a rtp stream to localhost:5000? Or is there problems with my ffplay on my machine? Or is there a special ffplay command I should execute? What should I do?

user3377126
  • 2,091
  • 4
  • 32
  • 39

0 Answers0