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

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

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

14,045 Views
toanjunifer
Senior Contributor I

Hi NXP,

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(){    
   cout << "Built with OpenCV " << CV_VERSION << endl;    
   Mat image;    
   Mat src;    
   VideoCapture capture;    
   capture.open(2);    
   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);    
   writer.open(filename, codec, fps, sizeFrame, isColor);    
   cout << "Started writing video... " << endl;    
   for (int i = 0 ; i < 60 ; i ++)    
   {        
      capture >> image;        
      Mat xframe;        
      resize(image,xframe,sizeFrame);        
      writer.write(xframe);        
    
   }    
   cout << "Write complete !" << endl;    
   capture.release();    
   writer.release();    
   return 0;
}

Thank you so much,

Toan

Labels (1)
3 Replies

11,857 Views
toanjunifer
Senior Contributor I

Thank you Bio_TICFSL‌,

I'm going to try your guide. I will notice you soon if possible :smileyhappy:

0 Kudos

11,857 Views
Bio_TICFSL
NXP TechSupport
NXP TechSupport

Hello Toan,

According to below i.mx comparison table i.MX6UL has not video/graphic hardware accelerators

https://www.nxp.com/docs/en/brochure/FLYRIMXPRDCMPR.pdf 

and section Parser / Demuxer specifications iMX6 Linux Release Notes is not applicable

to i.MX6UL EVK. So encoding can be implemented only in software, as example one can refer

to "SW plugins" on link i.mx8M EVK board encoder and decoder 

Regards

11,857 Views
toanjunifer
Senior Contributor I

Hi Bio_TICFSL‌,

I tried follow your guide. But It's still not working. I tried install X264 and openh264.

- Install X264:

   $ git clone https://code.videolan.org/videolan/x264.git

   $ ./configure

   $ make

   $ make install

- Install Openh264

   $ git clone GitHub - cisco/openh264: Open Source H.264 Codec 

   $ ./configure

   $ make

   $ make install

  

- I check "ffmpeg" command and get:

ffmpeg version 4.1.1 Copyright (c) 2000-2019 the FFmpeg developers
built with gcc 8.3.0 (GCC)
configuration: --disable-stripping --enable-pic --enable-shared --enable-pthreads --disable-libxcb --disable-libxcb-shm --disable-libxcb-xfixes --disable-libxcb-shape --cross-prefix=arm-poky-linux-gnueabi- --ld='arm-poky-linux-gnueabi-gcc -mfpu=neon -mfloat-abi=hard -mcpu=cortex-a7 --sysroot=/home/toanrd/LastDistro/build-x11/tmp/work/cortexa7hf-neon-poky-linux-gnueabi/ffmpeg/4.1.1-r0/recipe-sysroot' --cc='arm-poky-linux-gnueabi-gcc -mfpu=neon -mfloat-abi=hard -mcpu=cortex-a7 --sysroot=/home/toanrd/LastDistro/build-x11/tmp/work/cortexa7hf-neon-poky-linux-gnueabi/ffmpeg/4.1.1-r0/recipe-sysroot' --cxx='arm-poky-linux-gnueabi-g++ -mfpu=neon -mfloat-abi=hard -mcpu=cortex-a7 --sysroot=/home/toanrd/LastDistro/build-x11/tmp/work/cortexa7hf-neon-poky-linux-gnueabi/ffmpeg/4.1.1-r0/recipe-sysroot' --arch=arm --target-os=linux --enable-cross-compile --extra-cflags=' -O2 -pipe -g -feliminate-unused-debug-types -fmacro-prefix-map=/home/toanrd/LastDistro/build-x11/tmp/work/cortexa7hf-neon-poky-linux-gnueabi/ffmpeg/4.1.1-r0=/usr/src/debug/ffmpeg/4.1.1-r0 -fdebug-prefix-map=/home/toanrd/LastDistro/build-x11/tmp/work/cortexa7hf-neon-poky-linux-gnueabi/ffmpeg/4.1.1-r0=/usr/src/debug/ffmpeg/4.1.1-r0 -fdebug-prefix-map=/home/toanrd/LastDistro/build-x11/tmp/work/cortexa7hf-neon-poky-linux-gnueabi/ffmpeg/4.1.1-r0/recipe-sysroot= -fdebug-prefix-map=/home/toanrd/LastDistro/build-x11/tmp/work/cortexa7hf-neon-poky-linux-gnueabi/ffmpeg/4.1.1-r0/recipe-sysroot-native= -mfpu=neon -mfloat-abi=hard -mcpu=cortex-a7 --sysroot=/home/toanrd/LastDistro/build-x11/tmp/work/cortexa7hf-neon-poky-linux-gnueabi/ffmpeg/4.1.1-r0/recipe-sysroot' --extra-ldflags='-Wl,-O1 -Wl,--hash-style=gnu -Wl,--as-needed' --sysroot=/home/toanrd/LastDistro/build-x11/tmp/work/cortexa7hf-neon-poky-linux-gnueabi/ffmpeg/4.1.1-r0/recipe-sysroot --enable-hardcoded-tables --libdir=/usr/lib --shlibdir=/usr/lib --datadir=/usr/share/ffmpeg --disable-mipsdsp --disable-mipsdspr2 --cpu=cortex-a7 --pkg-config=pkg-config --enable-avcodec --enable-avdevice --enable-avfilter --enable-avformat --enable-avresample --enable-bzlib --enable-gpl --disable-libgsm --disable-indev=jack --disable-libvorbis --enable-lzma --disable-libmp3lame --disable-openssl --enable-postproc --disable-sdl2 --disable-libspeex --enable-swresample --enable-swscale --enable-libtheora --disable-vaapi --disable-vdpau --disable-libvpx --enable-libx264 --enable-outdev=xv
libavutil 56. 22.100 / 56. 22.100
libavcodec 58. 35.100 / 58. 35.100
libavformat 58. 20.100 / 58. 20.100
libavdevice 58. 5.100 / 58. 5.100
libavfilter 7. 40.101 / 7. 40.101
libavresample 4. 0. 0 / 4. 0. 0
libswscale 5. 3.100 / 5. 3.100
libswresample 3. 3.100 / 3. 3.100
libpostproc 55. 3.100 / 55. 3.100
Hyper fast Audio and Video encoder
usage: ffmpeg [options] [[infile options] -i infile]... {[outfile options] outfile}...

Use -h to get full help or, even better, run 'man ffmpeg'

- I also check with:

   $ gst-inspect-1.0 | grep x264

x264: x264enc: x264enc
 typefindfunctions: video/x-h264: h264, x264, 264

To install OpenCV-C++ on iMX6UL-EVK, I do cross-compile: streaming, write image are okay, it's make sure OpenCV-C++ cross-compile is okay.

This is my code:

#include <opencv2/opencv.hpp>
#include <opencv2/core.hpp>
#include <opencv2/videoio.hpp>
#include <opencv2/highgui.hpp>
#include <iostream>
#include <stdio.h>

using namespace cv;
using namespace std;

int main()
{
    cout << "Built with OpenCV " << CV_VERSION << endl;
    Mat image;
    Mat src;
    Mat frame;
    VideoCapture capture;
    capture.open(1);
    VideoWriter writer;
    int codec = VideoWriter::fourcc('m', 'p', '4', 'v');
    double fps = 15.0;                    
    string filename = "live.mp4";      
    writer.open(filename, codec, codec, Size(640,480), true);
    cout << "Started writing video... " << endl;
    for(int i=0;i<60;i++)
    {
        capture >> image;
        resize(image, frame, Size(640,480));
        writer.write(frame);
    }
    return 0;
}

So did I missing something or wrong way some where ?

Thank you so much,

Toan

0 Kudos