video_writer.cpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. #include <iostream>
  2. #include "opencv2/opencv_modules.hpp"
  3. #if defined(HAVE_OPENCV_CUDACODEC) && defined(_WIN32)
  4. #include <vector>
  5. #include <numeric>
  6. #include "opencv2/core.hpp"
  7. #include "opencv2/cudacodec.hpp"
  8. #include "opencv2/highgui.hpp"
  9. using namespace cv;
  10. int main(int argc, const char* argv[])
  11. {
  12. if (argc != 2)
  13. {
  14. std::cerr << "Usage : video_writer <input video file>" << std::endl;
  15. return -1;
  16. }
  17. const double FPS = 25.0;
  18. cv::VideoCapture reader(argv[1]);
  19. if (!reader.isOpened())
  20. {
  21. std::cerr << "Can't open input video file" << std::endl;
  22. return -1;
  23. }
  24. cv::cuda::printShortCudaDeviceInfo(cv::cuda::getDevice());
  25. cv::VideoWriter writer;
  26. cv::Ptr<cv::cudacodec::VideoWriter> d_writer;
  27. cv::Mat frame;
  28. cv::cuda::GpuMat d_frame;
  29. std::vector<double> cpu_times;
  30. std::vector<double> gpu_times;
  31. TickMeter tm;
  32. for (int i = 1;; ++i)
  33. {
  34. std::cout << "Read " << i << " frame" << std::endl;
  35. reader >> frame;
  36. if (frame.empty())
  37. {
  38. std::cout << "Stop" << std::endl;
  39. break;
  40. }
  41. if (!writer.isOpened())
  42. {
  43. std::cout << "Frame Size : " << frame.cols << "x" << frame.rows << std::endl;
  44. std::cout << "Open CPU Writer" << std::endl;
  45. if (!writer.open("output_cpu.avi", cv::VideoWriter::fourcc('X', 'V', 'I', 'D'), FPS, frame.size()))
  46. return -1;
  47. }
  48. if (d_writer.empty())
  49. {
  50. std::cout << "Open CUDA Writer" << std::endl;
  51. const cv::String outputFilename = "output_gpu.avi";
  52. d_writer = cv::cudacodec::createVideoWriter(outputFilename, frame.size(), FPS);
  53. }
  54. d_frame.upload(frame);
  55. std::cout << "Write " << i << " frame" << std::endl;
  56. tm.reset(); tm.start();
  57. writer.write(frame);
  58. tm.stop();
  59. cpu_times.push_back(tm.getTimeMilli());
  60. tm.reset(); tm.start();
  61. d_writer->write(d_frame);
  62. tm.stop();
  63. gpu_times.push_back(tm.getTimeMilli());
  64. }
  65. std::cout << std::endl << "Results:" << std::endl;
  66. std::sort(cpu_times.begin(), cpu_times.end());
  67. std::sort(gpu_times.begin(), gpu_times.end());
  68. double cpu_avg = std::accumulate(cpu_times.begin(), cpu_times.end(), 0.0) / cpu_times.size();
  69. double gpu_avg = std::accumulate(gpu_times.begin(), gpu_times.end(), 0.0) / gpu_times.size();
  70. std::cout << "CPU [XVID] : Avg : " << cpu_avg << " ms FPS : " << 1000.0 / cpu_avg << std::endl;
  71. std::cout << "GPU [H264] : Avg : " << gpu_avg << " ms FPS : " << 1000.0 / gpu_avg << std::endl;
  72. return 0;
  73. }
  74. #else
  75. int main()
  76. {
  77. std::cout << "OpenCV was built without CUDA Video encoding support\n" << std::endl;
  78. return 0;
  79. }
  80. #endif