2012-10-17 15:12:04 +08:00
|
|
|
#include <iostream>
|
2013-06-04 17:32:35 +08:00
|
|
|
|
|
|
|
#include "opencv2/opencv_modules.hpp"
|
|
|
|
|
2022-10-28 16:03:51 +08:00
|
|
|
#if defined(HAVE_OPENCV_CUDACODEC)
|
2013-06-04 17:32:35 +08:00
|
|
|
|
2012-10-17 15:12:04 +08:00
|
|
|
#include <vector>
|
|
|
|
#include <numeric>
|
|
|
|
|
2013-06-04 17:32:35 +08:00
|
|
|
#include "opencv2/core.hpp"
|
2013-07-23 18:29:21 +08:00
|
|
|
#include "opencv2/cudacodec.hpp"
|
2013-06-04 17:32:35 +08:00
|
|
|
#include "opencv2/highgui.hpp"
|
2012-10-17 15:12:04 +08:00
|
|
|
|
2016-12-17 21:48:29 +08:00
|
|
|
using namespace cv;
|
2012-10-17 15:12:04 +08:00
|
|
|
int main(int argc, const char* argv[])
|
|
|
|
{
|
|
|
|
if (argc != 2)
|
|
|
|
{
|
|
|
|
std::cerr << "Usage : video_writer <input video file>" << std::endl;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2022-10-28 16:03:51 +08:00
|
|
|
constexpr double fps = 25.0;
|
2012-10-17 15:12:04 +08:00
|
|
|
|
|
|
|
cv::VideoCapture reader(argv[1]);
|
|
|
|
|
|
|
|
if (!reader.isOpened())
|
|
|
|
{
|
|
|
|
std::cerr << "Can't open input video file" << std::endl;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2013-08-26 15:34:37 +08:00
|
|
|
cv::cuda::printShortCudaDeviceInfo(cv::cuda::getDevice());
|
2012-10-17 15:12:04 +08:00
|
|
|
|
|
|
|
cv::VideoWriter writer;
|
2013-07-23 16:33:11 +08:00
|
|
|
cv::Ptr<cv::cudacodec::VideoWriter> d_writer;
|
2012-10-17 15:12:04 +08:00
|
|
|
|
|
|
|
cv::Mat frame;
|
2013-08-26 15:34:37 +08:00
|
|
|
cv::cuda::GpuMat d_frame;
|
2022-10-28 16:03:51 +08:00
|
|
|
cv::cuda::Stream stream;
|
2012-10-17 15:12:04 +08:00
|
|
|
|
|
|
|
for (int i = 1;; ++i)
|
|
|
|
{
|
|
|
|
std::cout << "Read " << i << " frame" << std::endl;
|
|
|
|
reader >> frame;
|
|
|
|
if (frame.empty())
|
|
|
|
{
|
|
|
|
std::cout << "Stop" << std::endl;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!writer.isOpened())
|
|
|
|
{
|
|
|
|
std::cout << "Frame Size : " << frame.cols << "x" << frame.rows << std::endl;
|
|
|
|
std::cout << "Open CPU Writer" << std::endl;
|
2022-10-28 16:03:51 +08:00
|
|
|
const String outputFilename = "output_cpu.avi";
|
|
|
|
if (!writer.open(outputFilename, cv::VideoWriter::fourcc('X', 'V', 'I', 'D'), fps, frame.size()))
|
2012-10-17 15:12:04 +08:00
|
|
|
return -1;
|
2022-10-28 16:03:51 +08:00
|
|
|
std::cout << "Writing to " << outputFilename << std::endl;
|
2012-10-17 15:12:04 +08:00
|
|
|
}
|
|
|
|
|
2013-04-22 18:04:27 +08:00
|
|
|
if (d_writer.empty())
|
2012-10-17 15:12:04 +08:00
|
|
|
{
|
2013-07-24 17:55:18 +08:00
|
|
|
std::cout << "Open CUDA Writer" << std::endl;
|
2022-10-28 16:03:51 +08:00
|
|
|
const cv::String outputFilename = "output_gpu.h264";
|
|
|
|
d_writer = cv::cudacodec::createVideoWriter(outputFilename, frame.size(), cv::cudacodec::Codec::H264, fps, cv::cudacodec::ColorFormat::BGR, 0, stream);
|
|
|
|
std::cout << "Writing to " << outputFilename << std::endl;
|
2012-10-17 15:12:04 +08:00
|
|
|
}
|
|
|
|
|
2022-10-28 16:03:51 +08:00
|
|
|
d_frame.upload(frame, stream);
|
2012-10-17 15:12:04 +08:00
|
|
|
std::cout << "Write " << i << " frame" << std::endl;
|
|
|
|
writer.write(frame);
|
2013-04-22 18:04:27 +08:00
|
|
|
d_writer->write(d_frame);
|
2012-10-17 15:12:04 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2013-06-04 17:32:35 +08:00
|
|
|
|
|
|
|
#else
|
|
|
|
|
|
|
|
int main()
|
|
|
|
{
|
2013-07-24 17:55:18 +08:00
|
|
|
std::cout << "OpenCV was built without CUDA Video encoding support\n" << std::endl;
|
2013-06-04 17:32:35 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|