Advertisement
Guest User

Untitled

a guest
Feb 14th, 2017
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.19 KB | None | 0 0
  1. int ret;
  2. const int dst_width = 640;
  3. const int dst_height = 480;
  4. const AVRational dst_fps = {30, 1};
  5.  
  6. std::string file_name = std::string(argv[1]) + ".mp4";
  7.  
  8. std::vector<uint8_t> imgbuf(dst_height * dst_width * 3 + 16);
  9. cv::Mat image(dst_height, dst_width, CV_8UC3, imgbuf.data(), dst_width * 3);
  10.  
  11. avcodec_register_all();
  12. av_log_set_level(AV_LOG_VERBOSE);
  13.  
  14. AVFormatContext* outctx = nullptr;
  15. AVOutputFormat* outputFormat = NULL;
  16. outputFormat = av_guess_format("mp4", NULL, NULL);
  17. //AVOutputFormat outputFormat;
  18. //outputFormat.video_codec = AV_CODEC_ID_H264;
  19. ret = avformat_alloc_output_context2(&outctx, outputFormat, nullptr,
  20. file_name.c_str());
  21. if (ret < 0) {
  22. std::cerr << "fail to avformat_alloc_output_context2(" << file_name
  23. << "): ret=" << ret;
  24. return 2;
  25. }
  26.  
  27. // open output IO context
  28. ret = avio_open2(&outctx->pb, file_name.c_str(), AVIO_FLAG_WRITE, nullptr,
  29. nullptr);
  30. if (ret < 0) {
  31. std::cerr << "fail to avio_open2: ret=" << ret;
  32. return 2;
  33. }
  34.  
  35. AVCodec* vcodec = avcodec_find_encoder_by_name("nvenc");
  36. AVStream* vstrm = avformat_new_stream(outctx, vcodec);
  37. if (!vstrm) {
  38. std::cerr << "fail to avformat_new_stream";
  39. return 2;
  40. }
  41.  
  42. avcodec_get_context_defaults3(vstrm->codec, vcodec);
  43. vstrm->codec->width = dst_width;
  44. vstrm->codec->height = dst_height;
  45. vstrm->codec->pix_fmt = vcodec->pix_fmts[0];
  46. vstrm->codec->time_base = vstrm->time_base = av_inv_q(dst_fps);
  47. vstrm->r_frame_rate = vstrm->avg_frame_rate = dst_fps;
  48. if (outctx->oformat->flags & AVFMT_GLOBALHEADER)
  49. vstrm->codec->flags |= AV_CODEC_FLAG_GLOBAL_HEADER;
  50.  
  51. // open video encoder
  52. ret = avcodec_open2(vstrm->codec, vcodec, nullptr);
  53. if (ret < 0) {
  54. std::cerr << "fail to avcodec_open2: ret=" << ret;
  55. return 2;
  56. }
  57.  
  58. std::cout << "outfile: " << file_name.c_str() << "\n"
  59. << "format: " << outctx->oformat->name << "\n"
  60. << "vcodec: " << vcodec->name << "\n"
  61. << "size: " << dst_width << 'x' << dst_height << "\n"
  62. << "fps: " << av_q2d(dst_fps) << "\n"
  63. << "pixfmt: " << av_get_pix_fmt_name(vstrm->codec->pix_fmt) << "\n"
  64. << std::flush;
  65.  
  66. // initialize sample scaler
  67. SwsContext* swsctx = sws_getCachedContext(
  68. nullptr, dst_width, dst_height, AV_PIX_FMT_BGR24, dst_width, dst_height,
  69. vstrm->codec->pix_fmt, SWS_BICUBIC, nullptr, nullptr, nullptr);
  70. if (!swsctx) {
  71. std::cerr << "fail to sws_getCachedContext";
  72. return 2;
  73. }
  74.  
  75. AVFrame* frame = av_frame_alloc();
  76. std::vector<uint8_t> framebuf(
  77. avpicture_get_size(vstrm->codec->pix_fmt, dst_width, dst_height));
  78. avpicture_fill(reinterpret_cast<AVPicture*>(frame), framebuf.data(),
  79. vstrm->codec->pix_fmt, dst_width, dst_height);
  80. frame->width = dst_width;
  81. frame->height = dst_height;
  82. frame->format = static_cast<int>(vstrm->codec->pix_fmt);
  83.  
  84. running.store(true);
  85.  
  86. avformat_write_header(outctx, nullptr);
  87. int64_t frame_pts = 0;
  88. unsigned nb_frames = 0;
  89. bool end_of_stream = false;
  90. int got_pkt = 0;
  91.  
  92. signal(SIGINT, INThandler);
  93.  
  94. while (running.load()) {
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement