Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- int ret;
- const int dst_width = 640;
- const int dst_height = 480;
- const AVRational dst_fps = {30, 1};
- std::string file_name = std::string(argv[1]) + ".mp4";
- std::vector<uint8_t> imgbuf(dst_height * dst_width * 3 + 16);
- cv::Mat image(dst_height, dst_width, CV_8UC3, imgbuf.data(), dst_width * 3);
- avcodec_register_all();
- av_log_set_level(AV_LOG_VERBOSE);
- AVFormatContext* outctx = nullptr;
- AVOutputFormat* outputFormat = NULL;
- outputFormat = av_guess_format("mp4", NULL, NULL);
- //AVOutputFormat outputFormat;
- //outputFormat.video_codec = AV_CODEC_ID_H264;
- ret = avformat_alloc_output_context2(&outctx, outputFormat, nullptr,
- file_name.c_str());
- if (ret < 0) {
- std::cerr << "fail to avformat_alloc_output_context2(" << file_name
- << "): ret=" << ret;
- return 2;
- }
- // open output IO context
- ret = avio_open2(&outctx->pb, file_name.c_str(), AVIO_FLAG_WRITE, nullptr,
- nullptr);
- if (ret < 0) {
- std::cerr << "fail to avio_open2: ret=" << ret;
- return 2;
- }
- AVCodec* vcodec = avcodec_find_encoder_by_name("nvenc");
- AVStream* vstrm = avformat_new_stream(outctx, vcodec);
- if (!vstrm) {
- std::cerr << "fail to avformat_new_stream";
- return 2;
- }
- avcodec_get_context_defaults3(vstrm->codec, vcodec);
- vstrm->codec->width = dst_width;
- vstrm->codec->height = dst_height;
- vstrm->codec->pix_fmt = vcodec->pix_fmts[0];
- vstrm->codec->time_base = vstrm->time_base = av_inv_q(dst_fps);
- vstrm->r_frame_rate = vstrm->avg_frame_rate = dst_fps;
- if (outctx->oformat->flags & AVFMT_GLOBALHEADER)
- vstrm->codec->flags |= AV_CODEC_FLAG_GLOBAL_HEADER;
- // open video encoder
- ret = avcodec_open2(vstrm->codec, vcodec, nullptr);
- if (ret < 0) {
- std::cerr << "fail to avcodec_open2: ret=" << ret;
- return 2;
- }
- std::cout << "outfile: " << file_name.c_str() << "\n"
- << "format: " << outctx->oformat->name << "\n"
- << "vcodec: " << vcodec->name << "\n"
- << "size: " << dst_width << 'x' << dst_height << "\n"
- << "fps: " << av_q2d(dst_fps) << "\n"
- << "pixfmt: " << av_get_pix_fmt_name(vstrm->codec->pix_fmt) << "\n"
- << std::flush;
- // initialize sample scaler
- SwsContext* swsctx = sws_getCachedContext(
- nullptr, dst_width, dst_height, AV_PIX_FMT_BGR24, dst_width, dst_height,
- vstrm->codec->pix_fmt, SWS_BICUBIC, nullptr, nullptr, nullptr);
- if (!swsctx) {
- std::cerr << "fail to sws_getCachedContext";
- return 2;
- }
- AVFrame* frame = av_frame_alloc();
- std::vector<uint8_t> framebuf(
- avpicture_get_size(vstrm->codec->pix_fmt, dst_width, dst_height));
- avpicture_fill(reinterpret_cast<AVPicture*>(frame), framebuf.data(),
- vstrm->codec->pix_fmt, dst_width, dst_height);
- frame->width = dst_width;
- frame->height = dst_height;
- frame->format = static_cast<int>(vstrm->codec->pix_fmt);
- running.store(true);
- avformat_write_header(outctx, nullptr);
- int64_t frame_pts = 0;
- unsigned nb_frames = 0;
- bool end_of_stream = false;
- int got_pkt = 0;
- signal(SIGINT, INThandler);
- while (running.load()) {
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement