【问题标题】:Unable to create file with VideoWriter using OpenCV 3.1 on iOS 10无法在 iOS 10 上使用 OpenCV 3.1 使用 VideoWriter 创建文件
【发布时间】:2017-07-27 04:59:15
【问题描述】:

我的目标是创建一个基本的概念验证应用程序,我主要通过OpenCV 打开现有的 MP4 视频、处理帧并将其写回。

虽然我能够打开视频并处理帧,但我无法成功将其写回。我在网上找到的一些小文献要么没有用,要么没有答案。

以下是我尝试写入文件的第一种方法:

NSString *path = [[NSBundle mainBundle] pathForResource:@"MyVideo" ofType:@"mp4"];
std::string filename = path.UTF8String;
self.videoCapture = cv::VideoCapture(filename);
if(!self.videoCapture.isOpened()) std::cout << "Unable to open source" << std::endl;

int width = static_cast<int>(self.videoCapture.get(CV_CAP_PROP_FRAME_WIDTH));
int height = static_cast<int>(self.videoCapture.get(CV_CAP_PROP_FRAME_HEIGHT));
double fps = self.videoCapture.get(CV_CAP_PROP_FPS);
cv::VideoWriter videoWriter("output.mp4", self.videoCapture.get(CV_CAP_PROP_FOURCC), fps, cv::Size(width, height));
if (!videoWriter.isOpened())
{
    std::cout << "Unable to open writer" << std::endl;
    return;
}

这种方法的一个直接问题是,我获得的宽度、高度、fps 以及与CV_CAP 相关的几乎所有其他值始终为 1。我读到它是某些文件类型的问题,但它发生了使用 mp4、mov 和 mkv,所以没有帮助。

接下来,我使用我知道对视频正确的值对值进行硬编码,但由于无法打开编写器而得到相同的结果。

NSString *path = [[NSBundle mainBundle] pathForResource:@"MyVideo" ofType:@"mp4"];
std::string filename = path.UTF8String;
self.videoCapture = cv::VideoCapture(filename);
if(!self.videoCapture.isOpened()) std::cout << "Unable to open source" << std::endl;
cv::VideoWriter videoWriter("output.mkv", CV_FOURCC('m','p', '4', 'v'), 30, cv::Size(1920, 1080));

if (!videoWriter.isOpened())
{
    std::cout << "!!! Output video could not be opened" << std::endl;
    return;
}

对于它的价值,我确实读到了输出文件存在的问题,所以我删除了文件并更改了名称,但没有任何运气。

我还将 FOURCC 值更改为其他几种格式并相应地更新了输出名称,再次,没有任何成功。

【问题讨论】:

    标签: ios objective-c opencv video-capture


    【解决方案1】:

    我也为cv::VideoWriter 苦恼,因为它也无法保存处理后的视频。

    最后我放弃了,现在我使用AVAssetWriter : (

    希望有人能解决这个问题。

    【讨论】:

      【解决方案2】:
      int fourcc = cv::VideoWriter::fourcc('m', 'p', '4', 'v');//mp4
      double rate = 30; // frame rate
      int capture_width = 480; 
      int capture_height = 640;
      NSArray *paths = NSSearchPathForDirectoriesInDomains( NSDocumentDirectory, NSUserDomainMask, YES);
      NSString *mp4_path = [[paths objectAtIndex:0] stringByAppendingPathComponent:@"video.mp4"];
      writer.open([mp4_path UTF8String], 
                  cv::CAP_AVFOUNDATION, 
                  fourcc, 
                  rate, 
                  cv::Size(capture_width, capture_height),
                  true);
      
      

      对我来说很好。

      【讨论】:

        猜你喜欢
        • 2023-01-04
        • 1970-01-01
        • 2021-03-13
        • 2016-05-26
        • 2014-05-02
        • 2011-08-12
        • 2021-08-05
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多