Reputation: 3
I am trying to write a captured video to a file using OpenCV in C++ inside visualstudio 2013. Programs seems to capture the video from the webcam on my laptop and also able to save each frame a image but when I write the frames into a video file, I end up with a 6kb file. Program gives me no error as I have followed the instruction from the OpenCV document.
I am pasting the program for review. Please suggest how may I make it a successful program.
Thank you.
#include <stdio.h>
#include <iostream>
#include "opencv2\core\core.hpp"
#include "opencv2\highgui\highgui.hpp"
using namespace std;
using namespace cv;
int main()
{
VideoCapture video_capture(0);
if (!video_capture.isOpened())
{
cout << "Error in opening video feed!" << endl;
getchar();
return -1;
}
// Creating the window to view video feed
String window_name = "Video_Feed";
namedWindow(window_name, CV_WINDOW_AUTOSIZE);
//
Mat frame;
// Filename
String filename = "...\\first_recording.avi";
// four character code
int fcc = CV_FOURCC('M', 'P', '4', '2');
// frames per sec
int fps = 10;
// frame size
Size frame_size(CV_CAP_PROP_FRAME_WIDTH, CV_CAP_PROP_FRAME_HEIGHT);
VideoWriter video_writer = VideoWriter(filename,fcc,fps,frame_size,true);
if (!video_writer.isOpened())// || video_writer.isOpened == NULL)
{
cout << "Error in opening video writer feed!" << endl;
getchar();
return -1;
}
int frame_count = 0;
while (frame_count < 100)
{
bool cap_success = video_capture.read(frame);
if (!cap_success)
{
cout << "Error in capturing the image from the camera feed!" << endl;
getchar();
break;
}
imshow(window_name, frame);
//imwrite("cap.jpg", frame);
video_writer.write(frame);
switch (waitKey(10))
{
case 27:
return 0;
break;
}
frame_count++;
}
//scvReleaseVideoWriter;
destroyWindow(window_name);
return 0;
}
Upvotes: 0
Views: 2291
Reputation: 394
Please find the below piece of code to write the video file.
int main(int argc, char* argv[])
{
// Load input video
cv::VideoCapture input_cap("test8.avi");
if (!input_cap.isOpened())
{
std::cout << "!!! Input video could not be opened" << std::endl;
return -1;
}
// Setup output video
cv::VideoWriter output_cap("output.avi",
input_cap.get(CV_CAP_PROP_FOURCC),
input_cap.get(CV_CAP_PROP_FPS),
cv::Size(input_cap.get(CV_CAP_PROP_FRAME_WIDTH), input_cap.get(CV_CAP_PROP_FRAME_HEIGHT)));
if (!output_cap.isOpened())
{
std::cout << "!!! Output video could not be opened" << std::endl;
return -1;
}
// Loop to read frames from the input capture and write it to the output capture
cv::Mat frame;
while (true)
{
if (!input_cap.read(frame))
break;
output_cap.write(frame);
}
// Release capture interfaces
input_cap.release();
output_cap.release();
return 0;
}
Upvotes: 1