VideoCapture always returns False in Python OPENCV

2019-09-14 18:29发布

问题:

Every time that I use VideoCapture trying to access the frames from a video file, the return value (ret) is false. See the sample code below:

cap = cv2.VideoCapture('asd.mkv')
    vid = []
    while True:
        ret, img = cap.read()
        if not ret: # Always happens
            break
        vid.append(cv2.resize(img, (171, 128)))

I have already tried absolutely everything I could find today by googling, including the OpenCV guide and this long issue on Github. Also, I read some solutions involving moving ffmpeg dll files, but that only was in the case of Windows.

Any ideas? Because I defenitely ran out of them.

回答1:

The problem was at my IDE (Visual Studio Code) and the association with bin files when installing OpenCV... my bad. This guide worked flawlessly.



回答2:

For future reference: installing opencv with pip does not work with ffmpeg.

From the opencv-python FAQ:

Q: Why I can’t open video files on GNU/Linux distribution X or on macOS?

A: OpenCV video I/O depends heavily on FFmpeg. Manylinux and macOS OpenCV binaries are not compiled against it. The purpose of these packages is to provide as easy as possible installation experience for OpenCV Python bindings and they should work directly out-of-the-box. Adding FFmpeg as an additional dependency without a “universal” FFmpeg build (e.g. LGPL licensed build like in the Windows wheels) the goal is considerably harder to achieve. This might change in the future.

Solutions:

  1. Build from source (docs). This worked for me.
  2. Use scikit-video as a workaround (Github).