Ask Your Question
0

Why would video files not open in OpenCV (both C & C++ APIs)?

asked 2013-10-25 12:10:27 -0500

dumbledad gravatar image

updated 2013-10-26 04:21:11 -0500

berak gravatar image

I am swapping from EmguCV to OpenCV so that I can try some of the OpenCV functions that are not yet wrapped by EmguCV. However I'm failing to load my simple test video file, which loads without issue in EmguCV. Looking at the EmguCV source code I notice that they call the OpenCV C functions rather than C++ to initialise capture from a file (or a webcam) so I wrote the following test code (with help from stackoverflow). Here is my simple code

int _tmain()
{
    string filename = "sample.wmv";
    if (!FileExists(filename.c_str))
    {
        cout << "File does not exist" << endl;  
        return -1;
    }
    else
    {
        cout << "File exists" << endl;
        VideoCapture cap (filename);
        if(!cap.isOpened())
        {
            cout << "Failed to open file in OpenCV C++" << endl;
            //Trying C API too just-in-case
            CvCapture* capture = cvCreateFileCapture(filename.c_str());
            if (!capture)
            {
                cout << "Failed to open file in OpenCV C" << endl;  
            }
            else
            {
                //Grab frames and do stuff
                cvReleaseCapture(&capture);
            }
            return -1;
        }
        else
        {
            //Grab frames and do stuff
        }
    }
    return 0;
}

When I run this I get the output

File exists

Failed to open file in OpenCV C++

Failed to open file in OpenCV C

This happens regardless of the file, i.e. it happens for "sample.wmv", "sample.avi", and even one of the OpenCV sample files "tree.avi".

Why? What is going on?

(I'm running OpenCV 2.4.6 on a Windows 8 and a Windows 8.1 machine.)

edit retag flag offensive close merge delete

Comments

1

My experience with this tells me that it probably is a codec issue. Try to add the opencv_ffmpeg.dll to your project folder

Pedro Batista gravatar imagePedro Batista ( 2013-10-25 12:15:20 -0500 )edit

It has opencv_ffmpeg246.dll in there already, should that do?

dumbledad gravatar imagedumbledad ( 2013-10-25 15:29:00 -0500 )edit

Yes that should be sufficient. However, could you try using an absolute path to your files instead of relative. Your code gets executed in a specific structure on windows, so absolute paths first to see if it actually works, then experimenting with relative paths.

StevenPuttemans gravatar imageStevenPuttemans ( 2013-10-28 09:48:22 -0500 )edit

1 answer

Sort by ยป oldest newest most voted
0

answered 2013-10-28 07:59:41 -0500

dumbledad gravatar image

It appears that, following advice from Tobias Senst on a stackoverflow answer, I needed to build OpenCV on my local machine rather than rely on the DLLs that are provided with the standard distribution. It has been a headache to get OpenCV to build locally (see here, here, and here) but now that I reference to a copy of OpenCV 2.4.6 that was built on this machine I can open the video files.

I have no idea why this works, why it is not mentioned in the requirements, why others are not more frequently having this problem, nor what is failing with the default deployment, but at least I have it fixed.

edit flag offensive delete link more
Login/Signup to Answer

Question Tools

Stats

Asked: 2013-10-25 12:10:27 -0500

Seen: 3,012 times

Last updated: Oct 28 '13