Search code examples
pythonopencvcodec

How to make python opencv handle files which a c++ application reads?


I have a problem with a python (Python(x,y) distribution) application not being able to open a video file using opencv (version 2.3.1 with the pythonxy installer). The file has a rather problematic format (should be regular MPEG-2, but we had reports of problems with playing it), but is handled by the ffdshow video decoder.

My problem is that different instances of opencv on the same system are handling the file differently, the version installed in the python directory is failing to read the file.

Have you got any idea how to make the python app process the video file in the same way the other application does?

The following code:

import cv2.cv as cv
capture = cv.CaptureFromFile("film.mpeg")

print cv.GetCaptureProperty(capture, cv.CV_CAP_PROP_POS_MSEC)
print cv.GetCaptureProperty(capture, cv.CV_CAP_PROP_FRAME_COUNT )
print cv.GetCaptureProperty(capture, cv.CV_CAP_PROP_CONVERT_RGB  )

cv.SetCaptureProperty(capture, cv.CV_CAP_PROP_POS_MSEC, 50)

frame = cv.QueryFrame(capture)
cv.SaveImage("img.jpg",frame)

returns

0.0   
0.0  
0.0  

and writes an invalid image file.

At the same time, on the same computer I have a C++ application using opencv (the same version number 2.3.1) which is able to open files in such format. After listing the libraries both applications use, I found that the C++ one loads ffdshow.ax(only if I don't rename it, but look at point 3. below), whereas the python one doesn't.

What I have done so far:

  1. I have verified that the python app gives correct results for another file, with a different format
  2. I thought that there might be an issue with some compilation options of the opencv dlls in python, so I replaced the opencv dlls in C:\Python27\Lib\site-packages with the ones from the application that is working, getting still the same results.
  3. I renamed the file ffdshow.ax to ffdshow.ax_, and the C++ application did not load it, but was still working - that one got me really confused.

Solution

  • I got back to that project. The answer from this question:

    OpenCV 2.4 VideoCapture not working on Windows

    helped. All I needed to do was to add the 3rdparty/ffmpeg directory to the PATH environment variable and add the opencv version number to the dll file name (opencv_ffmpeg.dll -> opencv_ffmpeg246.dll)