OpenCV VideoCapture不会打开文件ios

时间:2012-09-14 23:37:35

标签: ios opencv video-capture

我使用以下方法将视频保存到文件中

NSString *documentsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
documentsDirectory = [documentsDirectory stringByAppendingPathComponent:@"test9.avi"];

NSLog(@"Open to: %@", documentsDirectory);

std::string fileName([documentsDirectory cString], [documentsDirectory cStringLength]);
videoWriter = new cv::VideoWriter(fileName, CV_FOURCC('H', 'F', 'Y', 'U'), 25, cvSize(288,352),1);

但我不能用VideoCapture打开它:

NSLog(@"Load the video");
videoCapture = new cv::VideoCapture();
NSLog(@"Video loaded");
if (videoCapture->open(fileName))
{
    NSLog(@"Opened");
}
else {
    NSLog(@"Failed to open");
}

只有我的日志“无法打开”,VideoCapture本身没有错误... 我可以以某种方式得到更详细的错误日志吗?

4 个答案:

答案 0 :(得分:2)

cv::VideoCapture capture;
NSString* pathToInputFile = [NSString documentPathByAppendingPath:@"Video/sample_iTunes.mov"];
if(capture.open(std::string([pathToFile UTF8String]))){
    NSLog(@"Opened");
}else{
    NSLog(@"Failed to open");
}

对我来说很好用

答案 1 :(得分:0)

虽然Kirill的回答在发布时会起作用,但现在已经不再适用了。

以下适用于XCode 8.2.1,取代了不再可用的documentPathByAppendingPath功能。

cv::VideoCapture cap;
NSString *path = [[NSBundle mainBundle] pathForResource:@"MyVideo" ofType:@"MOV"];
if ( cap.open ( std::string( path.UTF8String ) ) ) {
    NSLog(@"Opened");
} else {
    NSLog(@"Failed to open");
}

答案 2 :(得分:0)

我使用Swift 3遇到了这个问题。 感谢KirillCodeBender的答案,我解决了这个问题。

let resourceURL = Bundle.main.url(forResource: "test", withExtension: "MOV")
let openCVVideoCapture = OpenCVVideoCaptureWrapper.init(file: resourceURL?.path)

以下是我的OpenCVVideoCaptureWrapper.mm

@implementation OpenCVVideoCaptureWrapper {

    cv::VideoCapture video;
}

- (instancetype)initWithFile:(NSString*) filePath  {

    std::string cvFilePath = [filePath UTF8String];
    video = cv::VideoCapture(cvFilePath);

    if (!video.isOpened()) NSLog(@"Could not open file");

    return self;
}
@end

答案 3 :(得分:0)

此代码段在Swift 4的Xcode 10中为我工作。感谢@Nathan Tuggy。

- (void)openVideoFile:(NSURL *)fileName {

    NSString *filename = fileName.path;
    std::string filePath = [filename UTF8String];

    cv::VideoCapture capture(filePath);

    if (!capture.isOpened()) {
        printf("@Error Opening video file");
        // failed, print error message
    }
    else {
        printf("File Opened");
    }
}