Opencv video writer tutorial for simple video recording into the file.

Tutorial VideoWriter Cut video in Real time


In this tutorial, I would like to show you how to use the video writer in OpenCV. This is a little bit boring task, but there are some insights to be highlighted for successful use. 

Let me introduce my Setup

Openv Visual Studio 2015

I am working with Visual Studio 2015 and Opencv 3. How to install opencv and build your own program can be found here. Install Opencv Visual Studio 2015
Opencv 3 prebuild lib is shipped with VC11 and VC12 libs. This libs are built for Visual Studio 2012 and 13. You can use them if you have an older version of Visual Studio and include the project. If you can use Visual Studio 2015 with these libs be there is some problem with compatibility. This issue could be solved by redistribution of Windows pack with older dll.
 I definitely prefer to build your Opencv libs for appropriate Visual Studio Version by Cmake. It is an easy straightforward process that assumes only basic knowledge of opencv dependency, Cmake, and Visual Studio basics.

Build your own Opencv 3 for Visual Studio 2015 !! 

This process takes about one hour of your time but saves a lot of time of possible future troubles. Believe me, I went through it. 

Opencv 3 Video editing and writer

I would like to add something strange and a little bit different to this tutorial. Let me show you code that is simple and contain 

Real-time video cutting 

This code shows you how to use OpenCV as a simple real-time video editing tool. The program opens video sources as files, rtsp streams, or web cameras. 

Only small recapitulation

Select your VideoCapture source. 

  • This source let you access your web camera 
VideoCapture capture(0);
  • This source let you access rtsp stream if you have installed FFMPEG
VideoCapture capture("rtsp://USER:PASS@xxx.xxx.xxx.xxx/axis-media/media.amp?camera=2");
  • This video capture format let you access file in project directory
VideoCapture capture("input.mp4");


Video editing cutting on the fly

Back to the example. Just choosing your video source depends on you. 
This program lets you catch some keyboard events and record video frames only if this event occurred. In other words, The program writes frames between two keyboard inputs into an external video file. 
You can watch the long video and cut only interesting parts on the fly or press the button if something happens on the RTSP security camera stream. 

Opencv Video cut, record code example





This program works on Windows using GetKeyState. Let me use the same sizes input and format for simplicity. In some future tutorials, I will show more about this broad topic. 

Record video if left Shitf key changes state

#include <Windows.h>
#include <fstream>
#include <iostream>
#include "opencv2\highgui.hpp"
#include "opencv2\imgproc.hpp"
#include "opencv2/imgcodecs/imgcodecs.hpp"
#include "opencv2/videoio/videoio.hpp"
using namespace cv;
using namespace std;

int main(int argcconst char** argv)
{
   Mat LoadedImage;
   // Video capture from file  opt.MOV in the project directory
   VideoCapture cap("opt.MOV");
        // This is one of the most important things
        // Sizes
        //Your VideoWriter Size must correspond with the input video.
        // Size of your output video 
   Size SizeOfFrame = cv::Size800600);
         // On Windows write video into Result.wmv with codec W M V 2 at 30 FPS 
         // and use your predefined Size for simplicity 
   VideoWriter video("Result.wmv"CV_FOURCC('W''M''V''2'), 30,
  SizeOfFrame, true);
  for (;;)
        {
        bool Is = cap.grab();
        if (Is == false) {
            cout << "cannot grab video frame" << endl;
        }
         else {
              // Receive video from your source 
              cap.retrieve(LoadedImage, CV_CAP_OPENNI_BGR_IMAGE);
                        // Resize your video to your VideoWriter size
                        // Again sizes must correspond 
             resize(LoadedImage, LoadedImage, Size(800600));
                       // Preview video all frames
             namedWindow("Video", WINDOW_AUTOSIZE);
             imshow("Video", LoadedImage);
             waitKey(10);
          // check of left shift key change its state 
          // if Left Shift is pressed write video to file
             if (GetKeyState(VK_LSHIFT) == true )
                 {
                 cout << "Saving video" << endl;
                 // Save video into file if  GetKeyState(VK_LSHIFT)  state changes
                 video.write(LoadedImage);
                 waitKey(10);
                 }else { 
                 // else nothing to write  only show preview
                  cout << "Only Frame preview" << endl;
                }
         }
    }
}