7

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."

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....

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....

Gonzalo Garcia
  • 6,192
  • 2
  • 29
  • 32
alcon
  • 123
  • 1
  • 1
  • 11
  • What is the actual ffmpeg command and can you also show the complete ffmpeg console output? – llogan Nov 12 '13 at 18:27
  • How do you mean the actual ffmpeg command? Opencv will automatically call the right ffmpeg commands when I use the code that I included in my first post? – alcon Nov 13 '13 at 17:36
  • How can you know it is the correct command without seeing it? – llogan Nov 13 '13 at 19:11

2 Answers2

5

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.

littleimp
  • 1,159
  • 9
  • 13
  • If you put -1 as FOURCC a window will open where you can pick a codec. – littleimp Sep 09 '14 at 10:14
  • True. Or set "0" for uncompressed video (make sure you have enough space in disk first). – LovaBill Dec 29 '14 at 16:05
  • 2
    I put -1 and my options are MS Video 1 and Indeo. How do I get codecs from this millennium? – Seth Feb 12 '15 at 22:09
  • OpenCV simply shows the codecs installed on your machine. So basically just install any codec you would like to use (make sure you install the encoder because some codecs just contain the decoder). – littleimp Feb 13 '15 at 11:40
  • Usually they have some sort of installer (depends on your operating system) – littleimp Aug 09 '21 at 19:32
5

I managed to work it out on ubuntu, write down for your reference.

  1. In order to have x264 codec work, you need to: Build FFmpeg with x264 enabled!
  2. Before debug about OpenCV, please make sure you could generate x264 with ffmpeg, by itself.

Try the below command to verify H264 codec:

ffmpeg -i x264-input.mp4 -vcodec libx264 -f mp4 x264-output.mp4

Follow the FFmpeg officially doc to install from source.

hao
  • 1,144
  • 12
  • 15
  • Why is this down voted? Yes the question is for windows... but this answer at least appears to be sensible and relevant, and as of now, the only real attempt to answer the OP. – RTbecard Feb 21 '20 at 15:08