Ask Your Question

VideoCapture.isOpened() is always returning false

asked 2019-06-27 00:42:12 -0500

avj gravatar image

updated 2019-06-29 00:13:31 -0500

I'm new to openCV and I'm having trouble with VideoCapture.isOpened() always returning false. I've seen other posts that have similar problems, but I'm not managing to make headway towards a solution for my case.

image description

Text version of what is in the screenshot:

#import <opencv2/opencv.hpp>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <iostream>
#import "OpenCVWrapper.h"

using namespace cv;
using namespace std;

@implementation OpenCVWrapper

+ (int) testing : (string) filePath { // couldn't input parameter as NSString* because that made the VideoCapture's constructor not work, this way it recognized constructor pattern

VideoCapture cap(filePath);
cout << "did it work : " << cap.isOpened() << endl;

Mat frame;
cap >> frame; // get the next frame from video
cout << "mat = " << endl << frame << endl;

return 1;

+ (NSString *)openCVVersionString {
    return [NSString stringWithFormat:@"OpenCV Version %s",  CV_VERSION];


And below is how they are called (in Swift):

// first function's call
let videoFileURL = Bundle.main.url(forResource: "spaceMinimal", withExtension: "mp4")
var test = OpenCVWrapper.testing(videoFileURL.path)
// second function's call
print("Before : \(OpenCVWrapper.openCVVersionString())")

The image shows the two functions in my header that uses openCV libraries. I call both functions on my first ViewController, and you can see the output at the bottom of the image. I've pasted the output below as well:

did it work : 0 // This is the result for

Before : OpenCV Version 4.1.0 // This is the result of the other function, which is working

For whatever reason, VideoCapture is not opening the mp4 file, even though the mp4 file is in the directory (as can be seen in the image).

From other posts it seems like there may be an issue with some ffmpeg dll or something (I'm not familiar with these), but I have no idea where to go from here. Please help!

additional details: - openCV-4.0 - 1.0 included the ios package framework - using XCode 10.1 - my openCVWrapper.h :

#import <UIKit/UIKit.h>


@interface OpenCVWrapper : UICollectionView
+ (NSString *)openCVVersionString;
+ (int) testing : (NSString*)filePath; // had to pass in NSString* here instead of string because 'string' isn't recognised in this file

edit retag flag offensive close merge delete


please replace the useless screenshot with a TEXT version of your code / errors, thank you.

berak gravatar imageberak ( 2019-06-27 03:09:13 -0500 )edit

Sure @berak, I've added the code in text above, both what is in the screenshot and also how it is called in my View Controller. As for errors, there are no errors that show up. The issue I'm facing is with VideoCapture not seemingly doing what I believe it should be

avj gravatar imageavj ( 2019-06-27 03:54:59 -0500 )edit

thanks a ton, much better now, it can be indexed for search, ppl can paste / try your code, etc.

berak gravatar imageberak ( 2019-06-27 04:36:49 -0500 )edit

thanks for the tip @berak!

avj gravatar imageavj ( 2019-06-27 05:15:52 -0500 )edit

1 answer

Sort by ยป oldest newest most voted

answered 2019-06-27 07:31:33 -0500

When you run your app, all file locations are with respect to the app bundle and not your computer's file system. The way you are passing the video name is with respect to your computer's file system which is not the same as your app's file system. You need to first find the location of the video in your app's file system, then pass it as a NSString to your OpenCV wrapper.

You may do something like this in Swift

let videoFileURL = Bundle.main.url(forResource: "spaceMinimal", withExtension: "mp4")

Then pass videoFileURL.path into your OpenCV wrapper as an NSString. This is what consists of the video path relative to the app and should be the parameter inside cap.

edit flag offensive delete link more


Hi @eshirima, thanks for your reply! I tried what you suggested, and it ran fine but the .isOpened() is still returning as false. But this time I am getting this error message:

[ERROR:0] VIDEOIO(AVFOUNDATION): raised unknown C++ exception!

Would you know what this means and what I should do? (just for kicks I tried importing AVFOUNDATION but that didn't change anything)

avj gravatar imageavj ( 2019-06-27 11:11:57 -0500 )edit

Did u confirm that the path passed to VideoCapture is correct? Print it out from the Swift portion all the way to the OpenCVWrapper

eshirima gravatar imageeshirima ( 2019-06-28 00:00:10 -0500 )edit

Definitely, as a test I passed the same path to play the video on Swift's Video Player using AVFoundation and it's working so the path seems correct. But for whatever reason, VideoCapture is not working for me..


avj gravatar imageavj ( 2019-06-28 02:26:51 -0500 )edit

update your question with how you are passing the path to your wrapper

eshirima gravatar imageeshirima ( 2019-06-28 15:40:56 -0500 )edit

@eshirima, done. I had to try some weird things between the wrapper's .mm and .h files to make it run. Could you plz take a look and see if you find any glaring causes of my problems? And really thank u for your time

avj gravatar imageavj ( 2019-06-29 00:14:24 -0500 )edit

Question Tools

1 follower


Asked: 2019-06-27 00:42:12 -0500

Seen: 1,026 times

Last updated: Jun 29 '19