使用ffmpeg6.0将h264原流muxing为mp4或者flv时出现马赛克现象

使用ffmpeg6.0将h264原流muxing为mp4或者flv时出现马赛克现象

我目前使用ffmpeg 6.0将h264原流封装成mp4、flv、或者rtmp格式,当码率超过1024kb/s时,最终画面底部有马赛克现象,怀疑是ffmpeg某些buffer参数设置导致画面被截断,我写的代码如下:

 avformat_alloc_output_context2(&m_outputFormatContext, NULL, "flv", m_outputUrl.c_str()); const AVOutputFormat* ofmt = m_outputFormatContext->oformat;
AVStream* outStream = avformat_new_stream(m_outputFormatContext, NULL);
outStream->codecpar->codec_type = AVMEDIA_TYPE_VIDEO;
outStream->codecpar->codec_id = AV_CODEC_ID_H264;
outStream->codecpar->width = track.width;
outStream->codecpar->height = track.height;
outStream->codecpar->profile = track.profile;
outStream->codecpar->codec_tag = 0;
outStream->codecpar->level = track.level;
if(track.profile<100){
    outStream->codecpar->format = AV_PIX_FMT_YUV420P;
}else{
    outStream->codecpar->format = AV_PIX_FMT_YUV444P;
}

int spsPpsLen = track.params1.size() + track.params2.size();
outStream->codecpar->extradata_size = spsPpsLen;
outStream->codecpar->extradata = (uint8_t*)av_malloc(spsPpsLen + AV_INPUT_BUFFER_PADDING_SIZE);
memcpy(outStream->codecpar->extradata, track.params1.data(), track.params1.size());
memcpy(outStream->codecpar->extradata+ track.params1.size(), track.params2.data(), track.params2.size());

AVDictionary *formatOpts = NULL;
av_dict_set(&formatOpts, "rw_timeout", "500000", 0); 
ret = avio_open2(&m_outputFormatContext->pb, m_outputUrl.c_str() , AVIO_FLAG_WRITE , NULL , &formatOpts);
ret = avformat_write_header(m_outputFormatContext, NULL);

av_interleaved_write_frame(m_outputFormatContext, packet);//write video packet

相关内容