Opencv Tutorial VideoWriter: Cut video in Real time

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;
                }
         }
    }
}







Next Post Previous Post
12 Comments
  • Teknozla
    Teknozla December 21, 2015 at 8:08 AM

    Really helpful post, thanks for sharing

  • Vl
    Vl December 21, 2015 at 1:06 PM

    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

  • Unknown
    Unknown January 3, 2017 at 11:08 AM

    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:
    http://stackoverflow.com/questions/41304795/videowriter-function-for-opencv-3-0-0

    • Vl
      Vl January 28, 2017 at 11:37 PM

      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

  • YouLoseBellyFat
    YouLoseBellyFat April 7, 2017 at 5:47 AM

    applets for java for all people

  • Unknown
    Unknown November 1, 2017 at 10:40 PM

    How the mat object write to rtmp stream?

  • Boatt WN
    Boatt WN December 2, 2017 at 12:38 AM

    Very Good.

    Goldenslot
    Goldenslot
    gclub
    สูตรบาคาร่า
    sbobet

  • ralax
    ralax December 6, 2017 at 1:06 AM

    t is a very good knowledge

    แทงบอลออนไลน์


    แทงบอล sbobet


    หวยออนไลน์


    ทางเข้า sbobet


    ทางเข้า m8bet

  • thanida
    thanida November 21, 2018 at 7:38 PM

    Great post. Keep it up. Thanks for advice. It is wonderful information for me ,gclub

  • byodbuzz05
    byodbuzz05 December 13, 2018 at 1:40 AM

    Apple Clips is a video editing app that lets users create and share short videos complete with special effects, text, and graphics.
    One of the more interesting features of Clips is Live Titles, which let you create animated captions by talking as you record.
    On Facebook, users play up to 85 percent of videos without sound, making this one of the more valuable video social media tools for boosting engagement rates.

  • byodbuzz05
    byodbuzz05 December 17, 2018 at 10:11 PM

    Apple Clips is a video editing app that lets users create and share short videos complete with special effects, text, and graphics.
    One of the more interesting features of Clips is Live Titles, which let you create animated captions by talking as you record.
    On Facebook, users play up to 85 percent of videos without sound, making this one of the more valuable video social media tools for boosting engagement rates.

  • supersonic100g9
    supersonic100g9 June 19, 2019 at 8:34 PM

    นิสิตเป็นกรุ๊ปลูกค้าโดยมากที่ประสพปัญหากับการไม่มีบัตรเครดิต แต่ว่ามีความสำคัญที่จะต้องเช่ารถยนต์สำหรับในการใช้งาน ซึ่งโน่นก็เลยทำให้การเช่ารถยนต์เป็นได้ ยากถ้าหากนิสิตมิได้มีการทำบัตรเครดิตมาก่อน บริการเช่ารถยนต์ไม่ใช้บัตรเครดิตของ BANANA rent-a-car
    .
    บริษัทรถเช่า
    รถเช่าสุวรรณภูมิ
    รถเช่าระหว่างซ่อม
    รถเช่าไม่ใช้บัตรเครดิต
    เช่ารถเก๋ง
    รถรับส่งสนามบิน

Add Comment
comment url