Ask Your Question
0

RTSP Streaming, Gstreamer or FFmpeg ?

asked 2020-09-09 03:02:33 -0600

Hello,

I would like to know which library is the best to do RTSP streaming with OpenCV on a Jetson TX2 board.

I already use UDP streaming with this code :

// VideoCapture Pipe
std::string Cap_pipeline("nvarguscamerasrc ! "
"video/x-raw(memory:NVMM), width=1920, height=1080,format=NV12, framerate=30/1 ! "
"nvvidconv ! video/x-raw,format=I420 ! appsink");

// VideoWriter Pipe
std::string Stream_Pipeline("appsrc is-live=true ! autovideoconvert ! "
"nvv4l2h264enc control-rate=1 bitrate=1000000 ! video/x-h264, "
"stream-format=byte-stream ! rtph264pay mtu=1400 ! "
"udpsink host=192.168.1.102 port=5000 sync=false async=false");

cv::VideoCapture Cap(Cap_pipeline,cv::CAP_GSTREAMER);
cv::VideoWriter Stream(Stream_Pipeline, cv::CAP_GSTREAMER,
30, cv::Size(1920, 1080), true);

// check for issues
if(!Cap.isOpened() || !Stream.isOpened()) {
    std::cout << "I/O Pipeline issue" << std::endl;
}

while(true) {
    cv::Mat frame;
    Cap >> frame; //read last frame
    if (frame.empty()) break;

      cv::Mat bgr;
      cv::cvtColor(frame, bgr, cv::COLOR_YUV2BGR_I420);

      //video processing

      Stream.write(bgr);// write the frame to the stream

      char c = (char)cv::waitKey(1);
      if( c == 27 ) break;
}

Cap.release();
Stream.release();

return 0;

And I can read this stream on another PC with GStreamer using udpsrc, as with VLC/FFmpeg using an SDP file.

Now, I would like to do RTSP streaming, so that I do not need to use any SDP file.

I would like to know which library between Gstreamer and FFmpeg has the best performance for live-streaming?

Thanks

edit retag flag offensive close merge delete

2 answers

Sort by ยป oldest newest most voted
0

answered 2020-09-12 09:56:24 -0600

AbhiTronix gravatar image

updated 2020-09-12 09:57:59 -0600

If you are looking solution in python, for RTSP Streaming with Gstreamer and FFmpeg, then you can use my powerful vidgear library that supports FFmpeg backend with its WriteGear API for writing to network. Also you can use its CamGear API for multi-threaded Gstreamer input thus boosting performance even more, the complete example is as follows:

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

# Open gstreamer source
g_source = "nvarguscamerasrc ! video/x-raw(memory:NVMM), width=1920, height=1080,format=NV12, framerate=30/1 ! nvvidconv ! video/x-raw,format=I420 ! appsink"
stream = CamGear(source=g_source, logging=True).start() 

# 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
writer = WriteGear(output_filename = 'rstp://192.168.1.102:5000', logging =True, **output_params)

# loop over
while True:

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

    # check for frame if Nonetype
    if frame is None:
        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.stop()

# safely close writer
writer.close()

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

edit flag offensive delete link more
0

answered 2020-09-09 03:53:25 -0600

berak gravatar image

opencv's ffmpeg wrapper does not allow you to use own pipelines, and you cannot stream out from the VideoWriter.

i guess, you have to use gstreamer for now

edit flag offensive delete link more

Comments

Hello, Thank you for your help.

Streaming from videowriter is not an obligation. It is also possible for me to transform the cv::Mat to avframe and then stream the avframe with FFmpeg's functions. The only important point is that I would like to have access to the cv::mat before the streaming to process them.

Do you know if FFmpeg's implementation have better performances than the Gstreamer pipeline with VideoWriter?

MatteoLuci gravatar imageMatteoLuci ( 2020-09-09 07:21:19 -0600 )edit

Do you know if FFmpeg's implementation have better performances than the Gstreamer pipeline

no, sorry.

berak gravatar imageberak ( 2020-09-09 08:08:33 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2020-09-09 03:02:33 -0600

Seen: 17,083 times

Last updated: Sep 12 '20