2

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.

6
  • Even though ret is false does img contain anything Commented Apr 3, 2017 at 3:23
  • Can you try providing a full qualified file path to cv2.VideoCapture('full/path/to/vid.mkv') Commented Apr 3, 2017 at 4:39
  • @chbchb55 No, it contains None. @ZdaR already tried that, and also different video formats. Commented Apr 6, 2017 at 14:22
  • Maybe it's writing directly to the file 'asd.mkv' Commented Apr 6, 2017 at 14:46
  • @chbchb55 The method calls are to read the file. Commented Apr 6, 2017 at 19:53

2 Answers 2

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).
Sign up to request clarification or add additional context in comments.

4 Comments

could you please explain what "compiled against" means? Does it mean we can install ffmpeg ourselves? or does it mean it has to be included in the opencv package
@AdamB I don't know the internals but it means OpenCV when compiling had to include ffmpeg to enable ffmpeg features. I believe you have to compile yourself with ffmpeg enabled.
A while ago I made a guide for ubuntu installing from source that may help askubuntu.com/a/1150443/477026
Great! I’ll try that out.
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.