Maksim
Maksim

Reputation: 53

Save mp4 video opencv

I have mp4 video, and after drawing i need to save it. I am trying to use

VideoWriter video("outcpp.avi", CV_FOURCC('M', 'J', 'P', 'G'), 10, Size(1280, 720));

But after saving this file is broken. full code(i am using opencv 2.4.13):

#include <opencv2/opencv.hpp>
#include <iostream>
#include <fstream>
using namespace std;
using namespace cv;
Mat src;
void mouse_callback(int event, int x, int y, int, void*)
{
    if (event == EVENT_LBUTTONDOWN)
    {
        rectangle(src, Point(x, y), Point(x+10, y+10), Scalar(0, 255, 0));
        imshow("src", src);
    }
}

int main(void)
{       
    CvCapture* cap = cvCreateFileCapture("1.mp4");
    VideoWriter video("outcpp.avi", CV_FOURCC('M', 'J', 'P', 'G'), 10, Size(1280, 720));
    while (1)
    {
        src = cvQueryFrame(cap);
        namedWindow("src", WINDOW_AUTOSIZE);
        imshow("src", src);
        setMouseCallback("src", mouse_callback);
        video.write(src);
        waitKey(0);
    }

    return 0;
}

Upvotes: 0

Views: 674

Answers (1)

Gralex
Gralex

Reputation: 4485

Seams everything is working with VideoCapture. Why do you use C interface for reading and C++ for write?

I only move video.write after wait key to capture user clicks in video.

int main(void)
{
    vector<Rect> trafficLights;

    VideoCapture cap("/Users/alex/Documents/my_projects/hahaton_cams/kfu2.mp4");
    VideoWriter video("outcpp.avi", CV_FOURCC('M', 'J', 'P', 'G'), 10, Size(1280, 720));
    while (cap.read(src)) {
        namedWindow("src", WINDOW_AUTOSIZE);
        imshow("src", src);
        setMouseCallback("src", mouse_callback);

        if (waitKey(0) == 'q')
            break;
        video.write(src); // to capture user clicks
    }

    return 0;
}

OpenCV 4.2

Upvotes: 1

Related Questions