Opencv Tutorial VideoWriter: Cut video in Real time


My favourite

  • Opencv tutorial people detection
  • Head people cascade download
  • Opencv tutorial optical flow
  • Opencv Video stabilization
  • Opencv car dataset download
  • Opencv tutorial Transparent mask
  • Opencv videowriter
  • Opencv FFMPEG
  • Opencv Canny edge and hough lines
  • Opencv Tutorial video writer (VideoWriter)

    Share this for more tutorials and computer vision post from me.. Thanks best Vladimir
    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 little bit boring task, but there is 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 you can find 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 older version of Visual Studio and include the project. If you can use Visual Studio 2015 with this libs may be there is some problem with compatibility. This issue could be solved by redistribution windows pack with older dll.
     I definitely prefer build your Opencv libs for appropriate Visual Studio Version by Cmake. It is easy straightforward process which assumes only basic knowledge of opencv dependency, Cmake and Visual Studio basics.

    Buil your own Opencv 3 for Visual Studio 2015 !! 

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

    Opencv 3 Video editing and writer

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

    Real time video cutting 

    This code show you how to use Opencv as simple real-time video editing tool. The program opens video source as file, rtsp stream or web camera. 

    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://");
    • 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 choose your video source depends on you. 
    This program let you catch some keyboard events and record video frames only if this events occurred. In other words, The program write frames between two keyboard inputs into external video file. 
    You can watch the long video and cut only interesting parts on the fly or press button if something happened on the RTSP security camera stream. 

    Opencv Video cut, record code example

    This program works on windows using GetKeyState. Let me use same sizes input and format for simplicity. In some future tutorial, 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 argc, const char** argv)

    Mat LoadedImage;
            // Video capture from file  opt.MOV in project directory
    VideoCapture cap("opt.MOV");

    // This is one of the most important thing
            // Sizes
            //Your VideoWriter Size must correspond with input video.

             // Size of your output video
    Size SizeOfFrame = cv::Size( 800, 600);

             // On windows write video into Result.wmv with codec W M V 2 at 30 FPS 
             // and use your predefined Size for siplicity 

    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(800, 600));

                            // Preview video all frames
    namedWindow("Video", WINDOW_AUTOSIZE);
    imshow("Video", LoadedImage);
    // 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

    }else { 
                                    // else nothing to write  only show preview
    cout << "Only Frame preview" << endl;




    1. Really helpful post, thanks for sharing

    2. Yes, It is easy, basic video capture. One and only important Advice is that source have to match video writer frame size. Second is opencv 3.0.0 on windows and included
      #include "opencv2/imgcodecs/imgcodecs.hpp"
      #include "opencv2/videoio/videoio.hpp"
      works well with wmv format. I have not tried install with and without FFMPEG yet.
      I am glad that is helpful. Thx

    3. Hi,

      I am working with Opencv 3.0.0 and C++
      I want to record the video using camera. I worked well for opencv2.4.10 but doesnt work with opencv3.0.0
      The program runs without any error but no video file is saved.
      please refer to the code in the post:

      1. There is difference in systems on windows from the codec use wmv. I do not have any problem. Also important is if the file is created without any content. Probably size of video frame (mat) rows cols entered to video writer doesn't match size in video writer definition. Simple try resize before writing to same size as defined in video writer. More sad thinks is sound support in opencv ;). Thanks for sharing

    4. How the mat object write to rtmp stream?



    Follow by Email

    Powered by Blogger.