ffmpeg H264 编解码配置

/ C++ / 没有评论 / 1756浏览

ffmpeg H264 编解码配置

ffmpeg H264编解码前面有文章介绍下,本文主要介绍一些参数配置。编码:

int InitEncoderCodec( int iWidth, int iHeight)
{
    AVCodec *  pH264Codec = avcodec_find_encoder(AV_CODEC_ID_H264);
    if(NULL == pH264Codec)
    {
        printf("%s", "avcodec_find_encoder failed");
        return  -1;
    }
    outPutEncContext = avcodec_alloc_context3(pH264Codec);
    outPutEncContext->gop_size = 30;
    outPutEncContext->has_b_frames = 0;
    outPutEncContext->max_b_frames = 0;
    outPutEncContext->codec_id = pH264Codec->id;
    outPutEncContext->time_base.num = 2;
    outPutEncContext->time_base.den = 15;
    outPutEncContext->pix_fmt            = *pH264Codec->pix_fmts;
    outPutEncContext->width              =  iWidth;
    outPutEncContext->height             = iHeight;
    outPutEncContext->qmin = 34;
    outPutEncContext->qmax = 50;
    AVDictionary *options = nullptr;
    outPutEncContext->flags |= AV_CODEC_FLAG_GLOBAL_HEADER;
    av_opt_set(outPutEncContext->priv_data,"tune","zerolatency",0);<br>     av_dict_set(&options,"preset","ultrafast",0);
    int ret = avcodec_open2(outPutEncContext, pH264Codec, &options);
    if (ret < 0)
    {
        printf("%s", "open codec failed");
        return  ret;
    }
    return 1;
}

解码:  

codecContext->flags |=CODEC_FLAG_LOW_DELAY;

编码codec 的time_base实际上表示视频的帧率,qmin,qmax决定了编码的质量,qmin,qmax越大编码的质量越差。zerolatency参数的作用是提搞编码的实时性。解码codec 加上CODEC_FLAG_LOW_DELAY,可提高解码速度(低延时)。