OpenCV视频编码器参数修改

OpenCV版本

  • OpenCV 4.1.2

视频编码

  • h264视频编码参考代码,outputVideo.write(frame) 需要有视频raw数据;
#include <opencv2/opencv.hpp>
#include <opencv2/imgproc/imgproc.hpp>

int main(){
    // init
    cv::VideoWriter outputVideo;
    cv::Size S = cv::Size(width_output, height_output);
    int fourcc = cv::VideoWriter::fourcc('H','2','6','4');
    int fps = 25;
    outputVideo.open(output_file, fourcc, fps, S);
    
    // write frame
    outputVideo.write(frame);
    
    // deinit
    outputVideo.release();
    
    return 0;    
}

h264视频编码参数调整

  • 修改文件 master/modules/videoio/src/cap_ffmpeg_impl.hpp
#if LIBAVUTIL_BUILD > CALC_FFMPEG_VERSION(51,11,0)
    /* Some settings for libx264 encoding, restore dummy values for gop_size
     and qmin since they will be set to reasonable defaults by the libx264
     preset system. Also, use a crf encode with the default quality rating,
     this seems easier than finding an appropriate default bitrate. */
    if (c->codec_id == AV_CODEC_ID_H264) {
      c->gop_size = -1;
      c->qmin = -1;
      c->bit_rate = 0;
      if (c->priv_data)
          av_opt_set(c->priv_data,"crf","21", 0);
          av_opt_set(c->priv_data,"preset","faster", 0);
    }
#endif

上面的代码,修改了crf参数,23修改为21
添加了preset参数的设置;
大家可以参考一下

发布了141 篇原创文章 · 获赞 107 · 访问量 30万+

猜你喜欢

转载自blog.csdn.net/ternence_hsu/article/details/103336026