Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Hey, if you're dealing with python, then you can use my powerful vidgear library that supports FFmpeg backend with its WriteGear API for writing to network directly from OpenCV. The complete example for writing opencv frames to a RTP stream is as follows:

# import required libraries
from vidgear.gears import WriteGear
import cv2

# Open your source
stream = cv2.VideoCapture(0) 


# define required FFmpeg parameters for your writer
output_params = {"-vcodec":"libx264","-profile:v":"main","-preset:v":"veryfast","-g":60,"-keyint_min":60,"-sc_threshold":0,"-b:v":"2500k","-maxrate":"2500k","-bufsize":"2500k", "-f":"flv"}

# Define writer with defined parameters and address such as `rtp://127.0.0.1:1234`
writer = WriteGear(output_filename = 'rtp://127.0.0.1:1234', logging =True, **output_params)

# loop over
while True:

    # read frames from stream
    (grabbed, frame) = stream.read()

    # check for frame if Nonetype
    if not grabbed:
        break


    # {do something with the frame here}


    # write frame to writer
    writer.write(frame)

    # Show output window
    cv2.imshow("Output Frame", frame)

    # check for 'q' key if pressed
    key = cv2.waitKey(1) & 0xFF
    if key == ord("q"):
        break

# close output window
cv2.destroyAllWindows()

# safely close video stream
stream.release()

# safely close writer
writer.close()

Docs: https://abhitronix.github.io/vidgear

Hey, if you're dealing with python, then you can use my powerful vidgear library that supports FFmpeg backend with its WriteGear API for writing to network directly from OpenCV. The complete example for writing opencv frames to a RTP stream stream is as follows:

# import required libraries
from vidgear.gears import WriteGear
import cv2

# Open your source
stream = cv2.VideoCapture(0) 


# define required FFmpeg parameters for your writer
output_params = {"-vcodec":"libx264","-profile:v":"main","-preset:v":"veryfast","-g":60,"-keyint_min":60,"-sc_threshold":0,"-b:v":"2500k","-maxrate":"2500k","-bufsize":"2500k", "-f":"flv"}

# Define writer with defined parameters and address such as `rtp://127.0.0.1:1234`
writer = WriteGear(output_filename = 'rtp://127.0.0.1:1234', logging =True, **output_params)

# loop over
while True:

    # read frames from stream
    (grabbed, frame) = stream.read()

    # check for frame if Nonetype
    if not grabbed:
        break


    # {do something with the frame here}


    # write frame to writer
    writer.write(frame)

    # Show output window
    cv2.imshow("Output Frame", frame)

    # check for 'q' key if pressed
    key = cv2.waitKey(1) & 0xFF
    if key == ord("q"):
        break

# close output window
cv2.destroyAllWindows()

# safely close video stream
stream.release()

# safely close writer
writer.close()

Docs: https://abhitronix.github.io/vidgear