OpenCV不保存视频 [英] OpenCV doesn't save the video

查看:171
本文介绍了OpenCV不保存视频的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用以下代码从文件中读取视频,应用canny edge算法并将修改后的视频写入文件。代码编译和运行完美。但是,视频不是写的!我完全糊涂了。请告诉我错误是什么。
文件根本没有创建!操作系统:Ubuntu 12.10

I'm using the following code to read a video from file, apply the canny edge algorithm and write the modified video to a file. The code compiles and runs perfectly. But, the video is not written! I'm utterly confused. Please tell me what the error is. The file is not created at all! OS: Ubuntu 12.10

写入输出文件的代码

打开输出文件

bool setOutput(const std::string &filename, int codec=0, double framerate=0.0, bool isColor=true) {

    outputFile= filename;
    extension.clear();

    if (framerate==0.0) 
        framerate= getFrameRate(); // same as input

    char c[4];
    // use same codec as input
    if (codec==0) { 
        codec= getCodec(c);
    }

    // Open output video
    return writer.open(outputFile, // filename
    codec, // codec to be used 
    framerate,      // frame rate of the video
    getFrameSize(), // frame size
    isColor);       // color video?
}

编写框架

void writeNextFrame (Mat& frame)
{
    writer.write (frame);
}

还有一个单独的run方法执行这些

And there's a separate run method which executes these

推荐答案

每当我在我的应用程序中遇到奇怪的行为时,我都会写一个简短,自包含,正确(可编辑),示例,以帮助我了解正在发生的事情。

Whenever I encounter strange behaviors in my applications, I write a short, self contained, correct (compilable), example to help me understand what's going on.

我写下面的代码来说明你应该做什么。值得注意的是,它在我的Mac OS X上完美运行:

I wrote the code below to illustrate what you should be doing. It's worth noting that it works perfectly on my Mac OS X:

#include <cv.h>
#include <highgui.h>

#include <iostream>
#include <string>

int main(int argc, char* argv[])
{
    // Load input video
    cv::VideoCapture input_cap("Wildlife.avi");
    if (!input_cap.isOpened())
    {
        std::cout << "!!! Input video could not be opened" << std::endl;
        return -1;
    }

    // Setup output video
    cv::VideoWriter output_cap("output.avi",  
                               input_cap.get(CV_CAP_PROP_FOURCC),
                               input_cap.get(CV_CAP_PROP_FPS), 
                               cv::Size(input_cap.get(CV_CAP_PROP_FRAME_WIDTH), input_cap.get(CV_CAP_PROP_FRAME_HEIGHT)));

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

    // Loop to read frames from the input capture and write it to the output capture
    cv::Mat frame;
    while (true)
    {       
        if (!input_cap.read(frame))             
            break;

        output_cap.write(frame);
    }

    // Release capture interfaces   
    input_cap.release();
    output_cap.release();

    return 0;
}

用FFmpeg检查输入文件显示( ffmpeg - i Wildlife.avi ):

Inspecting the input file with FFmpeg reveals (ffmpeg -i Wildlife.avi):

Input #0, avi, from 'Wildlife.avi':
  Metadata:
    ISFT            : Lavf52.13.0
  Duration: 00:00:07.13, start: 0.000000, bitrate: 2401 kb/s
    Stream #0.0: Video: msmpeg4v2, yuv420p, 1280x720, PAR 1:1 DAR 16:9, 29.97 tbr, 29.97 tbn, 29.97 tbc
    Stream #0.1: Audio: mp3, 44100 Hz, 2 channels, s16, 96 kb/s

和输出:

Input #0, avi, from 'output.avi':
  Metadata:
    ISFT            : Lavf52.61.0
  Duration: 00:00:07.10, start: 0.000000, bitrate: 3896 kb/s
    Stream #0.0: Video: msmpeg4v2, yuv420p, 1280x720, 29.97 tbr, 29.97 tbn, 29.97 tbc

因此两个文件之间唯一重要的变化是OpenCV生成的输出没有音频流,这是正确的因为OpenCV不处理音频的行为。

So the only significant change between the two files is that the output generated by OpenCV doesn't have an audio stream, which is the correct behavior since OpenCV doesn't deal with audio.

确保您的用户具有在您运行应用程序的目录中读/写/执行的适当权限。此外,我在代码中添加的调试可能会帮助您找到与输入/输出捕获相关的问题。

Make sure your user has the proper permission to read/write/execute in the directory you are running the application. Also, the debugs I added in the code will probably assist you to find problems related to input/output capture.

这篇关于OpenCV不保存视频的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆