OpenCV videoCapture 为 iOS 应用程序文档目录中的文件提供了错误的属性
OpenCV videoCapture gives bad properties for file in iOS app documents directory
我正在开发一个iOS应用程序,该应用程序可以录制视频,然后对该视频进行一些处理以创建另一个视频文件。 不过,我从视频捕获中得到了非常奇怪的行为。 它就像文件已被打开一样,但 get() 调用似乎为我关心的所有属性返回 1(我知道它们不是 1)。 我的代码如下所示:
VideoCapture cap(fileIn); // open the video file for reading
if ( !cap.isOpened() ) // if not success, exit program
{
cout << "Cannot open the video file" << endl;
return -1;
}
else{
cout << "File Open " << fileIn << endl;
}
double dFps = cap.get(CV_CAP_PROP_FPS); //get the frames per seconds of the video
cout << "Frames per second = " << dFps << endl;
double dWidth = cap.get(CV_CAP_PROP_FRAME_WIDTH); //get the width of frames of the video
double dHeight = cap.get(CV_CAP_PROP_FRAME_HEIGHT); //get the height of frames of the video
cout << "Frame Size = " << dWidth << "x" << dHeight << endl;
Size frameSize(static_cast<int>(dWidth), static_cast<int>(dHeight));
double dFrameCount = cap.get(CV_CAP_PROP_FRAME_COUNT);
cout << "Frame count = " << dFrameCount << endl;
VideoWriter oVideoWriter (fileOut, CV_FOURCC('m','p', '4', 'v'), dFps, frameSize, true); //initialize the VideoWriter object
if ( !oVideoWriter.isOpened() ) //if not initialize the VideoWriter successfully, exit the program
{
cout << "ERROR: Failed to write the video" << endl;
return -1;
}
else{
cout << "Output file open." << endl;
}
控制台输出为:
File Open /var/mobile/Containers/Data/Application/7CEE8B87-BD50-4172-8F62-A74AD7F3C45A/Documents/15318B74-07D9-43EE-932F-224EDCA9CDA7.mov
Frames per second = 1
Frame Size = 1x1
Frame count = 1
Output file open.
我尝试将文件路径更改为无效文件,以防它根本没有找到该文件,但这给了我"无法打开视频文件"。 任何想法将不胜感激。 我认为这可能是权限问题,因为当我尝试使用此代码访问相机胶卷中的视频时,我看到了类似的行为,但此文件位于我应用程序的文档目录中,因此我应该可以完全访问它。
这似乎至少在过去 2 年中一直是一个问题(iOS 上的 OpenCV - VideoCapture 属性始终返回 1),并且 iOS 版本的视频捕获根本不适用于打开文件。
我找到了另一种方法来做到这一点(使用 apple apis 打开视频,然后将每一帧转换为 opencv Mat),但总的来说,opencv 的 iOS 端口似乎远未完成,几乎完全没有文档。
首先,从 UIImagePickerController 对象获取视频文件的 URL
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
picker.dismiss(animated: true, completion: nil)
let url = info[UIImagePickerController.InfoKey.mediaURL] as? NSURL
let asset = AVURLAsset.init(url: url! as URL)
let mainstoryBoard = UIStoryboard(name:"Main", bundle: nil)
let viewcontroller = mainstoryBoard.instantiateViewController(withIdentifier:"ViewController") as! ViewController
viewcontroller.url = url
viewcontroller.asset = asset
self.navigationController?.pushViewController(viewcontroller, animated: true)
}
之后,使用此 url 发送到 cvwrapper.mm 文件,如打开视频文件功能所示
- (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");
}
}
相关文章:
- QML:修改在不同QML文件(而非main.QML)中定义的子对象的属性
- 如何通过一个循环将多个文件的内容分配给不同的类属性?
- 即使使用 FILE_FLAG_DELETE_ON_CLOSE 属性创建文件,文件也会保留在磁盘上
- My Project的属性配置正确,但Visual Studio仍然无法打开包含头文件
- MSBuild 由于缺少属性文件(可能与 IDE 相关)而无法生成 Visual Studio 项目
- 如何将目录中的文件属性填充到结构向量中?
- 设置文件属性成功,但检索其失败
- FAT32和NTF中的自定义文件属性
- Windows上的不可读文件属性
- 如何在Windows中创建扩展(自定义)文件属性
- wxwidgets类访问文件属性
- SQLite是否可以完成此任务?存储路径名与文件属性
- Stat() 函数返回值 -1,因此无法访问文件属性.我该如何纠正这一点
- 文件属性对话框中的自定义选项卡
- 获取文件属性函数
- 如何使用替代数据流添加新的文件属性
- 如何从文件中提取文件属性/元数据/注释?
- 如何在linux上使用c++为特定文件设置文件属性(1644)
- 我如何从QT中检索Windows中的文件属性元数据
- 窗口上的扩展文件属性(ntfs)