cv2.VideoCapture(url).read always returns false

I’m using opencv_python 4.4 and here is my code:

vidcap = cv2.VideoCapture(’‘http://localhost:7500/static/vi_django/myvideo.mp4’’)
success, image = vidcap.read()

Success is always false. I’ve tried it with other URLs as well.
Thoughts?

vidcap.isOpened()

you must check that (once) before trying to read.

likely your build contains no video I/O backend that can do HTTP.

What do you mean by?

likely your build contains no video I/O backend that can do HTTP.

I tried that as well:

vidcap = cv2.VideoCapture(pathIn)
while vidcap.isOpened():
    success, image = vidcap.read()
    if not success:
        break

do this:

print(cv2.getBuildInformation())

General configuration for OpenCV 4.4.0 =====================================
  Version control:               4.4.0
  Platform:
    Timestamp:                   2020-09-23T00:17:25Z
    Host:                        Windows 6.3.9600 AMD64
    CMake:                       3.18.2
    CMake generator:             Visual Studio 14 2015 Win64
    CMake build tool:            C:/Program Files (x86)/MSBuild/14.0/bin/MSBuild.exe
    MSVC:                        1900
  CPU/HW features:
    Baseline:                    SSE SSE2 SSE3
      requested:                 SSE3
    Dispatched code generation:  SSE4_1 SSE4_2 FP16 AVX AVX2
      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
  C/C++:
    Built as dynamic libs?:      NO
    C++ standard:                11
    C++ Compiler:                C:/Program Files (x86)/Microsoft Visual Studio 14.0/VC/bin/x86_amd64/cl.exe  (ver 19.0.24241.7)
    C++ flags (Release):         /DWIN32 /D_WINDOWS /W4 /GR  /D _CRT_SECURE_NO_DEPRECATE /D _CRT_NONSTDC_NO_DEPRECATE /D _SCL_SECURE_NO_WARNINGS /Gy /bigobj /Oi  /fp:precise     /EHa /wd4127 /wd4251 /wd4324 /wd4275 /wd4512 /wd4589 /MP  /MT /O2 /Ob2 /DNDEBUG 
    C++ flags (Debug):           /DWIN32 /D_WINDOWS /W4 /GR  /D _CRT_SECURE_NO_DEPRECATE /D _CRT_NONSTDC_NO_DEPRECATE /D _SCL_SECURE_NO_WARNINGS /Gy /bigobj /Oi  /fp:precise     /EHa /wd4127 /wd4251 /wd4324 /wd4275 /wd4512 /wd4589 /MP  /MTd /Zi /Ob0 /Od /RTC1 
    C Compiler:                  C:/Program Files (x86)/Microsoft Visual Studio 14.0/VC/bin/x86_amd64/cl.exe
    C flags (Release):           /DWIN32 /D_WINDOWS /W3  /D _CRT_SECURE_NO_DEPRECATE /D _CRT_NONSTDC_NO_DEPRECATE /D _SCL_SECURE_NO_WARNINGS /Gy /bigobj /Oi  /fp:precise     /MP   /MT /O2 /Ob2 /DNDEBUG 
    C flags (Debug):             /DWIN32 /D_WINDOWS /W3  /D _CRT_SECURE_NO_DEPRECATE /D _CRT_NONSTDC_NO_DEPRECATE /D _SCL_SECURE_NO_WARNINGS /Gy /bigobj /Oi  /fp:precise     /MP /MTd /Zi /Ob0 /Od /RTC1 
    Linker flags (Release):      /machine:x64  /NODEFAULTLIB:atlthunk.lib /INCREMENTAL:NO  /NODEFAULTLIB:libcmtd.lib /NODEFAULTLIB:libcpmtd.lib /NODEFAULTLIB:msvcrtd.lib
    Linker flags (Debug):        /machine:x64  /NODEFAULTLIB:atlthunk.lib /debug /INCREMENTAL  /NODEFAULTLIB:libcmt.lib /NODEFAULTLIB:libcpmt.lib /NODEFAULTLIB:msvcrt.lib
    ccache:                      NO
    Precompiled headers:         YES
    Extra dependencies:          ade wsock32 comctl32 gdi32 ole32 setupapi ws2_32
    3rdparty dependencies:       ittnotify libprotobuf zlib libjpeg-turbo libwebp libpng 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
  Windows RT support:            NO
  GUI: 
    Win32 UI:                    YES
    VTK support:                 NO
  Media I/O: 
    ZLib:                        build (ver 1.2.11)
    JPEG:                        build-libjpeg-turbo (ver 2.0.5-62)
    WEBP:                        build (ver encoder: 0x020f)
    PNG:                         build (ver 1.6.37)
    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 (prebuilt binaries)
      avcodec:                   YES (58.54.100)
      avformat:                  YES (58.29.100)
      avutil:                    YES (56.31.100)
      swscale:                   YES (5.5.100)
      avresample:                YES (4.0.0)
    GStreamer:                   NO
    DirectShow:                  YES
    Media Foundation:            YES
      DXVA:                      NO
  Parallel framework:            Concurrency
  Trace:                         YES (with Intel ITT)
  Other third-party libraries:
    Intel IPP:                   2020.0.0 Gold [2020.0.0]
           at:                   C:/Users/appveyor/AppData/Local/Temp/1/pip-req-build-52oirelq/_skbuild/win-amd64-3.7/cmake-build/3rdparty/ippicv/ippicv_win/icv
    Intel IPP IW:                sources (2020.0.0)
              at:                C:/Users/appveyor/AppData/Local/Temp/1/pip-req-build-52oirelq/_skbuild/win-amd64-3.7/cmake-build/3rdparty/ippicv/ippicv_win/iw
    Lapack:                      NO
    Eigen:                       NO
    Custom HAL:                  NO
    Protobuf:                    build (3.5.1)
  OpenCL:                        YES (NVD3D11)
    Include path:                C:/Users/appveyor/AppData/Local/Temp/1/pip-req-build-52oirelq/opencv/3rdparty/include/opencl/1.2
    Link libraries:              Dynamic load
  Python 3:
    Interpreter:                 C:/Python37-x64/python.exe (ver 3.7.5)
    Libraries:                   C:/Python37-x64/libs/python37.lib (ver 3.7.5)
    numpy:                       C:/Users/appveyor/AppData/Local/Temp/1/pip-build-env-nsz8jsj9/overlay/Lib/site-packages/numpy/core/include (ver 1.14.5)
    install path:                python
  Python (for build):            C:/Python27-x64/python.exe
  Java:                          
    ant:                         NO
    JNI:                         C:/Program Files/Java/jdk1.8.0/include C:/Program Files/Java/jdk1.8.0/include/win32 C:/Program Files/Java/jdk1.8.0/include
    Java wrappers:               NO
    Java tests:                  NO
  Install to:                    C:/Users/appveyor/AppData/Local/Temp/1/pip-req-build-52oirelq/_skbuild/win-amd64-3.7/cmake-install
----------------------------------------------------------------

okay so you seem to have a build with support for FFMPEG.

find any video file, try to open it using VideoCapture. does that work?

Yes, the code works using a local mp4 file.

can VLC open and display video from that URL?

do you have ffmpeg handy? can ffmpeg or ffplay read/play-back from that URL?

please post sample data that is emitted from that web server…

and it says “django”. please describe in painstaking detail what is going on here because there is and you should think it a good idea to share.

I’m running a django web application locally on my pc. Pasting the url http://localhost:7500/static/vi_django/ConsoleCapture_clipped.mp4 in a browser displays the video. VLC media player didn’t respond but didn’t error either.

Actually if I put the URL of any mp4 file on the internet in my code, it still doesn’t work. I withdraw my last comment about not having my django app configured correctly.
.

your code seems to run on windows, so please make sure you have the opencv_videoio_ffmpeg.dll on the path (this is used for decoding video files and ip urls)

My code works for local video files in the CWD, doesn’t that imply that the dll is on the path? It just doesn’t work for any URL.

that is odd.

I have a local http server and both command line ffplay as well as VideoCapture can read and decode an XviD/AVI file from the server. it shows that OpenCV is capable of the task in principle.

where is your build from? what prevents you from updating? what other builds can you try? could you build OpenCV yourself?

it works for me so I’d say it’s possible.

the question is, what’s going on on his computer

Good questions. I don’t recall where my build is from but I just uninstalled it from PyCharm, although it took some doing, the uninstalled failed the first couple of times. I reinstalled using pip

pip install opencv-python

That didn’t help, so I uninstalled and then re-installed using conda via PyCharm. Same result. Both installed V4.5.2.52 I’m trying a manual build according to the instructions https://pypi.org/project/opencv-python/

This attempt resulted with this error:

Building windows wheels for Python 3.7 requires Microsoft Visual Studio 2017.
Get it with “Visual Studio 2017”:
Visual Studio 2019 IDE - Programming Software for Windows


Building wheel for opencv-python (PEP 517) … error
ERROR: Failed building wheel for opencv-python
Failed to build opencv-python
ERROR: Failed to build one or more wheels

I already have VS 2019 installed, do I really need to install VS 2017?

you don’t. VS 2019 works just the same.

and you can just follow official OpenCV build instructions (on docs.opencv.org) rather than instructions specific to building a “python wheel”, because OpenCV always has the option to be built with python bindings. it’s just not packaged into a wheel normally.

as for python packages… avoid conda like the plague. their package index is full of abandoned junk and it’s a waste of time to find anything usable or trustworthy on there. only go for packages on the official python package index. for opencv, that is specifically the packages by “skvark”, opencv-python being the most prominent one.

Thanks for everyone’s help, but we’re taking a different approach for now.