Can OpenCV 3.1.0 be set to capture an RTSP stream over UDP?

asked 2016-12-30 19:46:19 -0500

victorft gravatar image

Using OpenCV for Java, I've been trying to connect to an RTSP server that will transport video over UDP. The code looks like:

VideoCapture cam = new VideoCapture("rtsp://localhost:8554");

OpenCV sends a setup request to the RTSP server that looks like:

RTSP/1.0 Transport: RTP/AVP/TCP;unicast;interleaved=0-1
CSeq: 3

The server has to send data over UDP not TCP. I've tried passing in "rtsp://localhost:8554?udp" but that doesn't change the request sent. If I use OpenCV 3.0.0 the setup request that's sent looks like:

RTSP/1.0 Transport: RTP/AVP/UDP;unicast;client_port=744-7445
CSeq: 3

However, OpenCV 3.0.0 doesn't seem to have VideoWriter while OpenCV 3.1.0 does.

The discussion for a bug report, RTSP over TCP with cvCreateFileCapture not working (Bug #2235), mentions that there was a modification introduced in OpenCV 2.4.11 and 3.1.0, that forces VideoCapture to default to using TCP to transport media. Is data transport over TCP hard coded, or can VideoCapture be somehow set to use UDP?

Also, I tried having the RTSP server send "400 Bad Request" as a response to the TCP transport request, and OpenCV didn't send a follow up request for UDP. It just reported an error:

warning: Error opening file (/build/opencv/modules/videoio/src/cap_ffmpeg_impl.hpp:578)

edit retag flag offensive close merge delete


Hi. Did you ever find a solution to this? I'm in the same boat.

coder5627 gravatar imagecoder5627 ( 2017-02-19 19:12:52 -0500 )edit

I haven't found a solution yet. Instead, I've been using a workaround which is just using OpenCV 3.0.0. (OpenCV 3.2.0 seems to have the same problem.) Another workaround would be to modify the code by undoing the changes mentioned in the bug report and doing a full recompile of opencv_java310.dll, open-cv-310.jar, and (most importantly) opencv_ffmpeg310_64.dll. I haven't tried this yet.

victorft gravatar imagevictorft ( 2017-02-23 13:54:09 -0500 )edit

Using a modified version of opencv_ffmpeg320_64.dll, I was able to connect to the RTSP server and transmit video over UDP. The modification to opencv_ffmpeg320_64.dll just replaces:

av_dict_set(&dict, "rtsp_transport", "tcp", 0);
int err = avformat_open_input(&ic, _filename, NULL, &dict);


int err = avformat_open_input(&ic, _filename, NULL, NULL);

in the file cap_ffmpeg_impl.hpp. After making this change, only opencv_ffmpeg320_64.dll has to be recompiled. This workaround will most likely work with a modified opencv_ffmpeg310_64.dll, too.

victorft gravatar imagevictorft ( 2017-04-20 19:43:30 -0500 )edit

Hi victorft, I had still the same issue. I tried the following steps:

1 - change cap_ffmpeg_impl.hpp:

replace the line (798->801)
        av_dict_set(&dict, "rtsp_transport", "tcp", 0);
         int err = avformat_open_input(&ic, _filename, NULL, &dict);
        int err = avformat_open_input(&ic, _filename, NULL, NULL);

2 - recompile opencv (

What did I do wrong?

mozillaa gravatar imagemozillaa ( 2018-04-08 20:37:03 -0500 )edit

Someone help me,please... :(

mozillaa gravatar imagemozillaa ( 2018-04-11 20:04:27 -0500 )edit

Dear @victorft, can you help me again please ?

mozillaa gravatar imagemozillaa ( 2018-04-18 03:01:40 -0500 )edit

@victorft i need your help

mozillaa gravatar imagemozillaa ( 2018-04-25 19:21:48 -0500 )edit

@victorft i need your help

mozillaa gravatar imagemozillaa ( 2018-04-30 03:50:39 -0500 )edit