【问题标题】:Cannot play video created by OpenCV (using Ubuntu 12.04)无法播放 OpenCV 创建的视频(使用 Ubuntu 12.04)
【发布时间】:2015-03-27 19:56:28
【问题描述】:

我尝试从 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 打开它们)

【问题讨论】:

  • 你没有在你的问题中说,你确定安装了codec 吗?
  • 我在编译 OpenCV 之前从源代码编译了 ffmpeg(ffmpeg -version 说我安装了 ffmpeg 2.3.3)。我从源代码编译 OpenCV(版本 2.4.5)并且 ccmake 说它肯定找到了 ffmpeg .我不确定 vlc 和 videoplayer 使用了哪些编解码器,但由于我可以对视频进行编码,我认为我应该能够以相同的格式对它们进行解码(如第二个编码示例中所示)

标签: c++ opencv ubuntu video ffmpeg


【解决方案1】:

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

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

那我就用这个命令

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

【讨论】:

    猜你喜欢
    • 2023-01-04
    • 1970-01-01
    • 1970-01-01
    • 2015-04-13
    • 2018-11-14
    • 1970-01-01
    • 1970-01-01
    • 2016-10-14
    • 2013-10-02
    相关资源
    最近更新 更多