0

I am using openCV 2.4.10, Visual Studio 2010, Win 7.
I want to write a program that can split a long video into sub videos according to a special requirement. My problem now is I cannot output the video with openCV. Following is my code.

std::string inputFileName = "aa.avi";
std::string outFileName = "bb.avi";
cv::VideoCapture cap(inputFileName);

double dWidth = cap.get(CV_CAP_PROP_FRAME_WIDTH);
double dHeight = cap.get(CV_CAP_PROP_FRAME_HEIGHT);
double totalFrames = cap.get(CV_CAP_PROP_FRAME_COUNT);
int frameRate = cap.get(CV_CAP_PROP_FPS);
int ex = static_cast<int>(cap.get(CV_CAP_PROP_FOURCC));
cv::Size inputSize = cv::Size((int) cap.get(CV_CAP_PROP_FRAME_WIDTH), (int) cap.get(CV_CAP_PROP_FRAME_HEIGHT));

cv::VideoWriter outCap;

if(!outCap.open(outFileName, ex, frameRate, inputSize, true))
        return 0;

cv::Mat frame;
while(cap.read(frame)){
    outCap << frame;

    // my requirement checking
    if(requirHit(frame)){
        outCap.release();
        return 0;
    }
}

I get the following error.

Could not find encoder for codec id 28: Encoder not found

How to get the encoder I needed?

I have searched the web that may be I can use another encoder but my original task is to split the video, I don't want to change the encoding method. What can I do?

sflee
  • 1,659
  • 5
  • 32
  • 63
  • Check this point for insight: http://stackoverflow.com/questions/28163201/writing-a-video-file-using-h-264-compression-in-opencv/28188331#28188331 - You are probably using an encoder that isn't supported on your computer. You need to either install the right codec for the video, or change the codec. – rayryeng May 07 '15 at 02:17
  • Do you mean I have to set `fourcc` variable first? But I already get the encoding format by `static_cast(cap.get(CV_CAP_PROP_FOURCC))` already. – sflee May 07 '15 at 02:24
  • 1
    Yes I changed my comment. I didn't see that. I suspect that you're using an encoding scheme that isn't supported on your computer. You may have to install the proper codec to do so... or change the encoding scheme. That's weird though... for Windows, I know that OpenCV has XVID that is supported, which I'm assuming that's what you want due to the AVI container. – rayryeng May 07 '15 at 02:31
  • Basically, I just want to split the video and I don't want to re-encode the format, any way you could suggest that I can do it with openCV? – sflee May 07 '15 at 02:40
  • 1
    The easiest thing would be to use FFMPEG straight up in command-line. http://unix.stackexchange.com/questions/22585/split-video-file-into-pieces-with-ffmpeg . I wouldn't even use OpenCV at all. Do you **have** to use OpenCV? – rayryeng May 07 '15 at 02:41
  • I have to use openCV because I need to determine when should I cut the video. Previously, I used the openCV to find the time that I should cut the video and then use ffmpeg with command `-ss xxx -to xxx`to cut it, but the problem is that the time calculated by openCV is shifted and I don't know why /.\ That's why I am now considering using openCV to split the movie directly~"~ – sflee May 07 '15 at 02:50
  • Btw, if ffmpeg can cut the video by frame number, I think my problem can also be solved /.\ – sflee May 07 '15 at 02:51
  • maybe you could save the video from openCV with a different codec (e.g. motion jpeg or raw) and postprocess the splitted videos with ffmpeg to fit your original codec. – Micka May 07 '15 at 14:51
  • 1
    on Windows, standard opencv ffmpeg doesnt provide many codecs for ENCODING (e.g. h.264 isn't supported afaik), so maybe you have to recompile ffmpeg and/or openCV – Micka May 07 '15 at 14:53

0 Answers0