windows OpenCV 视频编写器 H264 编解码器 (ffmpeg)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19934437/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
OpenCV videowriter H264 codec (ffmpeg)
提问by alcon
I want to save a video with opencv with lossless compresion so I don't lose any details of the frames. Everything works with the xvid codec but offcourse this is not a lossless compression so I found that the x264 codec is suitable. However it doesn't work, I tried the following sample code but while running I get the following error: "could not find encoder for codec id 28: encoder not found."
我想用无损压缩的opencv保存视频,这样我就不会丢失帧的任何细节。一切都适用于 xvid 编解码器,但当然这不是无损压缩,所以我发现 x264 编解码器是合适的。但是它不起作用,我尝试了以下示例代码,但在运行时出现以下错误:“找不到编解码器 ID 28 的编码器:未找到编码器。”
cv::VideoWriter makeVideo;
makeVideo.open("makevideo//newVideo.mp4", CV_FOURCC('X','2','6','4'), 30, cv::Size(1600,1200), true);
cv::Mat image = imread("makevideo//frames//111.png");
for(int i = 0; i < 200; i++)
makeVideo << image;
makeVideo.release();
I found that for this to work, I need to have ffmpeg support. I'm currently using opencv2.4.6 and in this discussion (how can I use the openCV FFMPEG video I/O rather than the DirectShow one in Windows?) someone mentioned that in opencv2.4 ffmpeg is automatically included. But its not working....
我发现要使其正常工作,我需要有 ffmpeg 支持。我目前正在使用 opencv2.4.6,并且在本次讨论中(如何在 Windows 中使用 openCV FFMPEG 视频 I/O 而不是 DirectShow ?)有人提到在 opencv2.4 中自动包含 ffmpeg。但它不起作用......
Here (How to compile OpenCV 2.3 with ffmpeg support with Visual Studio 2010) I found how to compile opencv and ffmpeg yourself on windows. I followed all the steps sucessfully but still I get the same error....
在这里(How to compile OpenCV 2.3 with ffmpeg support with Visual Studio 2010)我找到了如何在windows上自己编译opencv和ffmpeg。我成功地遵循了所有步骤,但仍然出现相同的错误....
回答by littleimp
I had the same problem and could not find a solution. So now I always use -1 as FOURCC and choose the x264 codec by hand.
我遇到了同样的问题,找不到解决方案。所以现在我总是使用 -1 作为 FOURCC 并手动选择 x264 编解码器。
回答by hao
I managed to work it out on ubuntu, write down for your reference.
我设法在ubuntu上解决了,写下来供您参考。
- In order to have
x264
codec work, you need to: Build FFmpeg with x264 enabled! - Before debug about OpenCV, please make sure you could generate x264 with ffmpeg, by itself.
- 为了让
x264
编解码器工作,您需要: 构建启用 x264 的FFmpeg ! - 在调试 OpenCV 之前,请确保您可以使用 ffmpeg 自行生成 x264。
Try the below command to verify H264
codec:
尝试以下命令来验证H264
编解码器:
ffmpeg -i x264-input.mp4 -vcodec libx264 -f mp4 x264-output.mp4
ffmpeg -i x264-input.mp4 -vcodec libx264 -f mp4 x264-output.mp4
Follow the FFmpeg officially docto install from source.
按照FFmpeg 官方文档从源代码安装。