简体   繁体   English

OpenCV不保存视频

[英]OpenCV doesn't save the video

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. 我正在使用以下代码从文件中读取视频,应用canny edge算法并将修改后的视频写入文件。 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 操作系统:Ubuntu 12.10

Code for writing to the output file 写入输出文件的代码

Opening the output file 打开输出文件

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?
}

Writing the frame 写帧

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

And there's a separate run method which executes these 并且有一个单独的run方法来执行这些

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. 每当我在我的应用程序中遇到奇怪的行为时,我会写一个简短的,自包含的,正确的(可编译的)示例来帮助我理解正在发生的事情。

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: 值得注意的是,它在我的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;
}

Inspecting the input file with FFmpeg reveals ( ffmpeg -i Wildlife.avi ): 使用FFmpeg检查输入文件( 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

and the output: 和输出:

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

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

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. 此外,我在代码中添加的调试可能会帮助您找到与输入/输出捕获相关的问题。

do you have FFMPEG library installed on your machine? 你的机器上安装了FFMPEG库吗? On Linux FFMPEG is used to write videos; 在Linux上,FFMPEG用于编写视频; on Windows FFMPEG or VFW is used. 在Windows FFMPEG或VFW上使用。 check by command 按命令检查

 ffmpeg -version

try to install additional libraries sudo apt-get install libavformat-dev libswscale-dev . 尝试安装其他库sudo apt-get install libavformat-dev libswscale-dev

And by calling writer.open you only Initializes or reinitializes video writer.You have to call the void VideoWriter::write(const Mat& image) to write the next video frame to file. 通过调用writer.open,您只需初始化或重新初始化视频writer。您必须调用void VideoWriter :: write(const Mat&image)将下一个视频帧写入文件。 I hope this will work. 我希望这会奏效。

I had the same problem and I changed the codecs to DIVX. 我有同样的问题,我将编解码器更改为DIVX。 Now the video is saved in both .avi and .mp4 formats. 现在视频以.avi和.mp4格式保存。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM