0

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

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

4 回答 4

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");
}

对我来说很好

于 2013-02-12T12:42:30.370 回答
0

这个片段在 Xcode 10、Swift 4 中对我有用。感谢@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");
    }
}
于 2020-02-24T12:10:10.637 回答
0

虽然基里尔的答案在发布时会起作用,但现在情况已不再如此。

以下适用于 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");
}
于 2017-03-06T06:24:42.103 回答
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
于 2017-04-14T02:02:46.633 回答