无法播放OpenCV创建的视频(使用Ubuntu 12.04)

时间:2015-01-28 19:04:36

标签: c++ opencv ubuntu video ffmpeg

我尝试从OpenCV创建的一系列图像中编写视频。但是我写完之后无法打开视频。我想有一个编解码器问题。我发现错误恰好来自哪里非常困难。这是我的代码:

    Size size = Size(vecOfMats[0].rows,vecOfMats[0].cols);
    int codec = CV_FOURCC('D', 'I', 'V', 'X');

    VideoWriter videoWriter;
    videoWriter.open(outputFilename,codec,15.0,size,true);

    for(int z=0; z < vecOfMats.size(); z++)
    {
        videoWriter.write(vecOfMats[z]);
    }
    videoWriter.release();

我也尝试了所有这些编解码器但没有成功(OpenCv无法找到编解码器或视频无法打开):

int codec = CV_FOURCC('P','I','M','1');   // = MPEG-1 codec
int codec = CV_FOURCC('M','J','P','G');   // = motion-jpeg codec - cannot be played by VLC
int codec = CV_FOURCC('M', 'P', '4', '2');// = MPEG-4.2 codec - not found
int codec = CV_FOURCC('D', 'I', 'V', '3');// = MPEG-4.3 codec - not found
int codec = CV_FOURCC('D', 'I', 'V', 'X');// = MPEG-4 codec - cannot be played by VLC
int codec = CV_FOURCC('U', '2', '6', '3');// = H263 codec - must have w/h = 4
int codec = CV_FOURCC('I', '2', '6', '3');// = H263I codec - not found

我甚至先前通过OpenCV打开了一段视频编解码器(没有成功):

string filename = "/path/to/the/video/myVideo.avi";
VideoCapture capture(filename);
int ex = static_cast<int>(capture.get(CV_CAP_PROP_FOURCC));             // Get Codec Type- Int form
char EXT[] = {(char)(ex & 0XFF) , (char)((ex & 0XFF00) >> 8),(char)((ex & 0XFF0000) >> 16),(char)((ex & 0XFF000000) >> 24), 0};// Transform from int to char via Bitwise operators
cout<<"Codec: "<<ex<<endl;
VideoWriter videoWriter;
videoWriter.open(outputFilename,ex,15.0,size,true);

我甚至不确定问题出在我的OpenCV或我的Ubuntu上:/。 (我尝试使用默认视频播放器和vlc打开它们)

1 个答案:

答案 0 :(得分:0)

由于我放弃使用Opencv编写视频,我只想提一下如何解决我的问题。将每个图像保存为png文件后,我使用linux命令行中的ffmpeg将图像序列写为视频。

图片按顺序命名如下: 00001.png, 00002.png, 00003.png, [...]

然后我使用这个命令

  

ffmpeg -i%5d.png -vcodec mpeg4 test.avi