3
votes

I have a simple piece of code, written in Python (version 2.7.11) designed to do things to a video file as follows:

import cv2

cap = cv2.VideoCapture('MyVideo.mov')
print(cap)
print(cap.isOpened())

while(cap.isOpened()):
    #Do some stuff

The result of print(cap) is a 8 digit hex number, so I don't know if that means that the video has been found.

However, the print statement for cap.isOpened() returns False. I have tried several fixes, but none of them worked. Any help or insight would be very helpful.

Things to note/things I have tried

  • I am running Windows 8.1, Python 2.7.11 and OpenCV 3.1.0
  • The location of the video file is in the same directory as the Python script
  • I have the following directories appended to my PATH variable:

    C:\Users\MyName\OpenCV3\opencv\build\x64\vc14\bin; 
    C:\Users\MyName\OpenCV3\opencv\sources\3rdparty\ffmpeg;
    C:\Python27\;
    C:\Python27\Scripts
    
  • I have checked that I have opencv_ffmpeg.dll in the OpenCV vc14 bin directory

  • I have checked that said dll file is titled opencv_ffmpeg310_64.dll

  • I have tried redownloading said dll file, and renaming it to include the version of OpenCV and the fact that my system is a 64-bit one

  • I have tried placing the dll file in the Python27 directory

  • The code above works on Mac, but not on Windows (tried the code on 2 different Macs and it worked, tried it on 2 different Windows machines and it returned false both times)

3
I had to have opencv_ffmpeg310.dll in my C:\Python27 directory. Not sure if that helps. (this is, of course, assuming you are using OpenCV 3.1.0 and 32-bit python) - Jonathan

3 Answers

1
votes

It is most likely that if you are using windows, files are in a \Users or other \<something> directory. The \ is seen as a unicode escape by the python interpreter and whatever follows it is probably an invalid escape. try typing r'<file path>' to cause the path to be read as raw text and have the unicode escapes ignored.

Try adding:

if(not cap.isOpened()):
    cap.open(r'<file_path>')

And if the problem is the file path, it will probably cause an error. Alternatively, you could just use a loop like this:

while(True):
    # Capture frame-by-frame
    ret, frame = cap.read()
    #if frame can't be read
    if ret==False:
        print('end of input or incompatible video file')
        break
    cv2.imshow('frame',frame)
    #if esc key pressed
    if cv2.waitKey(1) & 0xFF == 27:
        break


cv2.destroyAllWindows()
cap.release()
0
votes

Since your code shows no issues on Mac, try using other file extensions (eg. mp4 or wmv) on your Windows system, for testing. If your video is loaded then, that means OpenCV is correctly configured on your Windows, but apparently there is no driver to play .mov files

0
votes

try using this :

    cam=cv2.VideoCapture('MyVideo.mov')
    while(True):
      ret, img = cam.read()
      print ("frame", img)

      if cv2.waitKey(100) & 0xFF == ord('q'):
      break

    cam.release()
    cv2.destroyAllWindows()