title: OpenCV videocapture read camera rtsp error
body: <p>I’m using opencv-python (version 4.4.0.46) python version 3.8.5 to capture my IP Camera stream, however, I cannot read the video frame. Here is my code:</p>
  <pre class="lang-py prettyprint-override"><code>import cv2
  # cap = cv2.VideoCapture(&quot;rtsp://wowzaec2demo.streamlock.net/vod/mp4:BigBuckBunny_115k.mov&quot;)
  cap = cv2.VideoCapture(&quot;rtsp://admin:xxx@xxx/Streaming/Channels/1&quot;)
  print(cap.isOpened())
  
  ret,frame = cap.read()
  print(ret)
  
  while ret:
      ret,frame = cap.read()
      print(ret)
      if cv2.waitKey(1) &amp; 0xFF == ord('q'):
          break
  cap.release()
  
  </code></pre>
  <p>In my code, when I use my IPC stream, I got &quot;print(cap.isOpened())&quot; as True, while &quot;cap.read()&quot; returns False. However, when it changes to &quot;BigBuckBunny_115k&quot; stream, everything is OK, and I can get the frame picture.</p>
  <p>Somebody might say that your IPC should be damaged. But I've tried using ffmpeg to capture video, which can easily get the frame picture. My code:</p>
  <pre><code>ffmpeg -i &quot;rtsp://admin:xxx@xxx/Streaming/Channels/1&quot; -y -f image2 -r 1/1 img%03d.jpg
  </code></pre>
  <p>Using python to capture video, I got &quot;False&quot; like above, but there is not ant error log printed, making me difficult to solve the problem. So I changed to use java, and using opencv-4.4.0.so as the library, my code:</p>
  <pre class="lang-java prettyprint-override"><code>VideoCapture videoCapture = new VideoCapture();
  // set the exception mode true so that it can print stack trace when error
  videoCapture.setExceptionMode(true);
  boolean openStatus = videoCapture.open(&quot;rtsp://admin:xxx@xxx/Streaming/Channels/1&quot;);
  log.info(&quot;open rtsp status: {}&quot;, openStatus);
  int i = 0;
  status = true;
  while (openStatus &amp;&amp; status) {
      Mat mat = new Mat();
      videoCapture.read(mat);
  
      Imgcodecs.imwrite(&quot;../&quot; + i + &quot;.png&quot;, mat);
      i++;
  }
  </code></pre>
  <p>by running this code, I got exception: org.opencv.core.CvException: cv::Exception: OpenCV(4.4.0) /xxx/opencv-4.4.0/modules/videoio/src/cap.cpp:177: error: (-2:Unspecified error) could not open 'rtsp://admin:xxx@xxx/Streaming/Channels/1' in function 'open'.
  The exception log above does not help to solve the problem, what does &quot;-2:Unspecified error&quot; means?</p>
  <p>Forgot to mention that my runtime is CentOS 7.5 (kernel version 3.10.0-862.el7.x86_64) . Then I changed to another server with the same opencv-python version and CentOS version. It is strange that on this new server, using the same opencv code, I can get the video frame now.</p>
  <p>The first question is, is there any relevant information that I can refer to?
  The second question is, how can I troubleshoot this problem.
  The third question is, why does it behave differently on different server, is there any system configuration on my first server error, like network configuration?</p>
  <hr />
  <hr />
  <p>Update on 2021.01.07</p>
  <p>Thanks antoine for giving me information about OpenCV backend. I've tried to print the python-opencv build information using the code below.</p>
  <pre><code>import cv2
  print(cv2.getBuildInformation())
  </code></pre>
  <p>And on the 2 servers I have (one of them can read the video stream and one of them not), I got console print totally the same. Here is the print:</p>
  <pre><code>General configuration for OpenCV 4.4.0 =====================================
    Version control:               4.4.0-dirty
  
    Platform:
      Timestamp:                   2020-11-03T00:52:03Z
      Host:                        Linux 4.15.0-1077-gcp x86_64
      CMake:                       3.18.2
      CMake generator:             Unix Makefiles
      CMake build tool:            /bin/gmake
      Configuration:               Release
  
    CPU/HW features:
      Baseline:                    SSE SSE2 SSE3
        requested:                 SSE3
      Dispatched code generation:  SSE4_1 SSE4_2 FP16 AVX AVX2 AVX512_SKX
        requested:                 SSE4_1 SSE4_2 AVX FP16 AVX2 AVX512_SKX
        SSE4_1 (15 files):         + SSSE3 SSE4_1
        SSE4_2 (1 files):          + SSSE3 SSE4_1 POPCNT SSE4_2
        FP16 (0 files):            + SSSE3 SSE4_1 POPCNT SSE4_2 FP16 AVX
        AVX (4 files):             + SSSE3 SSE4_1 POPCNT SSE4_2 AVX
        AVX2 (29 files):           + SSSE3 SSE4_1 POPCNT SSE4_2 FP16 FMA3 AVX AVX2
        AVX512_SKX (4 files):      + SSSE3 SSE4_1 POPCNT SSE4_2 FP16 FMA3 AVX AVX2 AVX_512F AVX512_COMMON AVX512_SKX
  
    C/C++:
      Built as dynamic libs?:      NO
      C++ standard:                11
      C++ Compiler:                /usr/lib/ccache/compilers/c++  (ver 9.3.1)
      C++ flags (Release):         -Wl,-strip-all   -fsigned-char -W -Wall -Werror=return-type -Werror=non-virtual-dtor -Werror=address -Werror=sequence-point -Wformat -Werror=format-security -Wmissing-declarations -Wundef -Winit-self -Wpointer-arith -Wshadow -Wsign-promo -Wuninitialized -Winit-self -Wsuggest-override -Wno-delete-non-virtual-dtor -Wno-comment -Wimplicit-fallthrough=3 -Wno-strict-overflow -fdiagnostics-show-option -Wno-long-long -pthread -fomit-frame-pointer -ffunction-sections -fdata-sections  -msse -msse2 -msse3 -fvisibility=hidden -fvisibility-inlines-hidden -O3 -DNDEBUG  -DNDEBUG
      C++ flags (Debug):           -Wl,-strip-all   -fsigned-char -W -Wall -Werror=return-type -Werror=non-virtual-dtor -Werror=address -Werror=sequence-point -Wformat -Werror=format-security -Wmissing-declarations -Wundef -Winit-self -Wpointer-arith -Wshadow -Wsign-promo -Wuninitialized -Winit-self -Wsuggest-override -Wno-delete-non-virtual-dtor -Wno-comment -Wimplicit-fallthrough=3 -Wno-strict-overflow -fdiagnostics-show-option -Wno-long-long -pthread -fomit-frame-pointer -ffunction-sections -fdata-sections  -msse -msse2 -msse3 -fvisibility=hidden -fvisibility-inlines-hidden -g  -O0 -DDEBUG -D_DEBUG
      C Compiler:                  /usr/lib/ccache/compilers/cc
      C flags (Release):           -Wl,-strip-all   -fsigned-char -W -Wall -Werror=return-type -Werror=non-virtual-dtor -Werror=address -Werror=sequence-point -Wformat -Werror=format-security -Wmissing-declarations -Wmissing-prototypes -Wstrict-prototypes -Wundef -Winit-self -Wpointer-arith -Wshadow -Wuninitialized -Winit-self -Wno-comment -Wno-strict-overflow -fdiagnostics-show-option -Wno-long-long -pthread -fomit-frame-pointer -ffunction-sections -fdata-sections  -msse -msse2 -msse3 -fvisibility=hidden -O3 -DNDEBUG  -DNDEBUG
      C flags (Debug):             -Wl,-strip-all   -fsigned-char -W -Wall -Werror=return-type -Werror=non-virtual-dtor -Werror=address -Werror=sequence-point -Wformat -Werror=format-security -Wmissing-declarations -Wmissing-prototypes -Wstrict-prototypes -Wundef -Winit-self -Wpointer-arith -Wshadow -Wuninitialized -Winit-self -Wno-comment -Wno-strict-overflow -fdiagnostics-show-option -Wno-long-long -pthread -fomit-frame-pointer -ffunction-sections -fdata-sections  -msse -msse2 -msse3 -fvisibility=hidden -g  -O0 -DDEBUG -D_DEBUG
      Linker flags (Release):      -Wl,--exclude-libs,libippicv.a -Wl,--exclude-libs,libippiw.a -L/root/ffmpeg_build/lib  -Wl,--gc-sections -Wl,--as-needed  
      Linker flags (Debug):        -Wl,--exclude-libs,libippicv.a -Wl,--exclude-libs,libippiw.a -L/root/ffmpeg_build/lib  -Wl,--gc-sections -Wl,--as-needed  
      ccache:                      YES
      Precompiled headers:         NO
      Extra dependencies:          ade Qt5::Core Qt5::Gui Qt5::Widgets Qt5::Test Qt5::Concurrent /lib64/libpng.so /lib64/libz.so dl m pthread rt
      3rdparty dependencies:       ittnotify libprotobuf libjpeg-turbo libwebp libtiff libjasper IlmImf quirc ippiw ippicv
  
    OpenCV modules:
      To be built:                 calib3d core dnn features2d flann gapi highgui imgcodecs imgproc ml objdetect photo python3 stitching video videoio
      Disabled:                    world
      Disabled by dependency:      -
      Unavailable:                 java js python2 ts
      Applications:                -
      Documentation:               NO
      Non-free algorithms:         NO
  
    GUI: 
      QT:                          YES (ver 5.15.0)
        QT OpenGL support:         NO
      GTK+:                        NO
      VTK support:                 NO
  
    Media I/O: 
      ZLib:                        /lib64/libz.so (ver 1.2.7)
      JPEG:                        libjpeg-turbo (ver 2.0.5-62)
      WEBP:                        build (ver encoder: 0x020f)
      PNG:                         /lib64/libpng.so (ver 1.5.13)
      TIFF:                        build (ver 42 - 4.0.10)
      JPEG 2000:                   build Jasper (ver 1.900.1)
      OpenEXR:                     build (ver 2.3.0)
      HDR:                         YES
      SUNRASTER:                   YES
      PXM:                         YES
      PFM:                         YES
  
    Video I/O:
      DC1394:                      NO
      FFMPEG:                      YES
        avcodec:                   YES (58.109.100)
        avformat:                  YES (58.61.100)
        avutil:                    YES (56.60.100)
        swscale:                   YES (5.8.100)
        avresample:                NO
      GStreamer:                   NO
      v4l/v4l2:                    YES (linux/videodev2.h)
  
    Parallel framework:            pthreads
  
    Trace:                         YES (with Intel ITT)
  
    Other third-party libraries:
      Intel IPP:                   2020.0.0 Gold [2020.0.0]
             at:                   /tmp/pip-req-build-99ib2vsi/_skbuild/linux-x86_64-3.8/cmake-build/3rdparty/ippicv/ippicv_lnx/icv
      Intel IPP IW:                sources (2020.0.0)
                at:                /tmp/pip-req-build-99ib2vsi/_skbuild/linux-x86_64-3.8/cmake-build/3rdparty/ippicv/ippicv_lnx/iw
      Lapack:                      NO
      Eigen:                       NO
      Custom HAL:                  NO
      Protobuf:                    build (3.5.1)
  
    OpenCL:                        YES (no extra features)
      Include path:                /tmp/pip-req-build-99ib2vsi/opencv/3rdparty/include/opencl/1.2
      Link libraries:              Dynamic load
  
    Python 3:
      Interpreter:                 /opt/python/cp38-cp38/bin/python (ver 3.8.6)
      Libraries:                   libpython3.8.a (ver 3.8.6)
      numpy:                       /tmp/pip-build-env-qru8fff1/overlay/lib/python3.8/site-packages/numpy/core/include (ver 1.17.3)
      install path:                python
  
    Python (for build):            /bin/python2.7
  
    Java:                          
      ant:                         NO
      JNI:                         NO
      Java wrappers:               NO
      Java tests:                  NO
  
    Install to:                    /tmp/pip-req-build-99ib2vsi/_skbuild/linux-x86_64-3.8/cmake-install
  -----------------------------------------------------------------
  </code></pre>
  <p>In the print, I found the information:</p>
  <pre><code>  Video I/O:
      DC1394:                      NO
      FFMPEG:                      YES
        avcodec:                   YES (58.109.100)
        avformat:                  YES (58.61.100)
        avutil:                    YES (56.60.100)
        swscale:                   YES (5.8.100)
        avresample:                NO
      GStreamer:                   NO
      v4l/v4l2:                    YES (linux/videodev2.h)
  </code></pre>
  <p>Seems my two servers both have FFMPEG and don't have GStreamer. So, is there any other reason may cause the difference?</p>
  <p>By the way, I've tried passing API preference to the constructor like below:</p>
  <pre><code>cap = cv2.VideoCapture(&quot;rtsp://admin:xxx@xxx/Streaming/Channels/1&quot;, cv2.CAP_FFMPEG)
  </code></pre>
  <p>However, passing the parameter didn't solve the problem, I cannot read video frame either.</p>