ffmpeg H264 编解码配置

时间:2022-12-26 04:40:13

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);
     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,可提高解码速度(低延时)。

    如需交流请加qq群流媒体/Ffmpeg/音视频 127903734 ,或加QQ350197870。