AnsweredAssumed Answered

How to write *.mp4 video with OpenCV-C++?

Question asked by on Feb 13, 2020
Latest reply on Feb 21, 2020 by


My name is Toan. Currently, I'm using OpenCV-C++ to write video *.mp4 type. I can write video .avi type but It's take a lot of storage. About 1Mb/1s with 640x480 resolution and 15 FPS. I'm using iMX6UL-EVK board(Linux).

I built without error but no output .mp4 file. And in python code (OpenCV-Python), this board can write .mp4 video with "mp4v".

I tried with "mp4v", "xvid", "divx", "h264", "x264" but not working. So what can I do now? Or may you show me others type of video which not take much of storage ?

This is my code:

#include "opencv2/opencv.hpp"
#include <iostream>
using namespace cv;
using namespace std;
int main(){   
<< "Built with OpenCV " << CV_VERSION << endl;   
   Mat image;   
   Mat src;   
   VideoCapture capture;   
>> src;   
bool isColor = (src.type() == CV_8UC3);   
VideoWriter writer;   
int codec = VideoWriter::fourcc('M', 'P', '4', 'V');      
double fps = 15.0;   
   string filename
= "live.mp4";   
   Size sizeFrame(640,480);   
.open(filename, codec, fps, sizeFrame, isColor);   
<< "Started writing video... " << endl;   
   for (int i = 0 ; i < 60 ; i ++)   
>> image;       
      Mat xframe;       
<< "Write complete !" << endl;   
   return 0;

Thank you so much,