| 98 | } |
| 99 | |
| 100 | void FFVideoEncoder::init(const sibr::Vector2i & size) |
| 101 | { |
| 102 | #ifndef HEADLESS |
| 103 | w = size[0]; |
| 104 | h = size[1]; |
| 105 | |
| 106 | auto out_file = filepath.c_str(); |
| 107 | |
| 108 | |
| 109 | pFormatCtx = avformat_alloc_context(); |
| 110 | |
| 111 | fmt = av_guess_format(NULL, out_file, NULL); |
| 112 | pFormatCtx->oformat = fmt; |
| 113 | |
| 114 | const bool isH264 = pFormatCtx->oformat->video_codec == AV_CODEC_ID_H264; |
| 115 | if(isH264){ |
| 116 | SIBR_LOG << "[FFMPEG] Found H264 codec." << std::endl; |
| 117 | } else { |
| 118 | SIBR_LOG << "[FFMPEG] Found codec with ID " << pFormatCtx->oformat->video_codec << " (not H264)." << std::endl; |
| 119 | } |
| 120 | |
| 121 | if (avio_open(&pFormatCtx->pb, out_file, AVIO_FLAG_READ_WRITE) < 0) { |
| 122 | SIBR_WRG << "[FFMPEG] Could not open file " << filepath << std::endl; |
| 123 | return; |
| 124 | } |
| 125 | |
| 126 | pCodec = avcodec_find_encoder(pFormatCtx->oformat->video_codec); |
| 127 | if (!pCodec) { |
| 128 | SIBR_WRG << "[FFMPEG] Could not find codec." << std::endl; |
| 129 | return; |
| 130 | } |
| 131 | |
| 132 | video_st = avformat_new_stream(pFormatCtx, pCodec); |
| 133 | |
| 134 | if (video_st == NULL) { |
| 135 | SIBR_WRG << "[FFMPEG] Could not create stream." << std::endl; |
| 136 | return; |
| 137 | } |
| 138 | |
| 139 | pCodecCtx = video_st->codec; |
| 140 | pCodecCtx->codec_id = fmt->video_codec; |
| 141 | pCodecCtx->codec_type = AVMEDIA_TYPE_VIDEO; |
| 142 | pCodecCtx->pix_fmt = AV_PIX_FMT_YUV420P; |
| 143 | pCodecCtx->width = w; |
| 144 | pCodecCtx->height = h; |
| 145 | pCodecCtx->gop_size = 10; |
| 146 | pCodecCtx->time_base.num = 1; |
| 147 | pCodecCtx->time_base.den = (int)std::round(fps); |
| 148 | |
| 149 | // Required for the header to be well-formed and compatible with Powerpoint/MediaPlayer/... |
| 150 | if (pFormatCtx->oformat->flags & AVFMT_GLOBALHEADER) { |
| 151 | pCodecCtx->flags |= AV_CODEC_FLAG_GLOBAL_HEADER; |
| 152 | } |
| 153 | |
| 154 | //H.264 specific options. |
| 155 | AVDictionary *param = 0; |
| 156 | if (pCodecCtx->codec_id == AV_CODEC_ID_H264) { |
| 157 | av_dict_set(¶m, "preset", "slow", 0); |