ffmpeg接收视频流的时候 初始化经常失败导致再次上线需要花很长时间
连接视频初始化失败会报如下错误:
Could not find codec parameters for stream 0 (Video: h264, none): unspecified size
Consider increasing the value for the 'analyzeduration' and 'probesize' options
结合
https://www.jianshu.com/p/37d705aa0e01
和我们自己的业务做了一些修改
首先修改了probesize的大小,把它调整大了,我担心高清的视频会出现获取不全的情况;
其次修改了analyzeduration的值 由之前1秒设置为5秒钟
另外还新增了一些优化参数
int StreamInputCache::Init(const ProcessRecord& pr) {
_pFormatCtx = avformat_alloc_context();
_aviobuffer = (unsigned char*)av_malloc(_buffer_len);
if (_aviobuffer == 0) {
dzlog_info("Couldn't open input stream.\n");
return -1;
}
//add by wupengf 2021-03-24 解决累积延迟问题
_pFormatCtx->probesize = 10000000;//5 000 000
_pFormatCtx->flags |= AVFMT_FLAG_NOBUFFER;
av_opt_set(_pFormatCtx->priv_data,"preset","ultrafast",0);
//AV_TIME_BASE = 1000 000
_pFormatCtx->max_analyze_duration = 5 * AV_TIME_BASE;
_pFormatCtx->pb = avio_alloc_context(_aviobuffer, _buffer_len, 0, (void*)this, cbRead, NULL, NULL);
//analyzeduration probesize
/* Open an input stream and read the header. */
int ret = 0;
AVDictionary* options = NULL;
av_dict_set(&options, "fflags", "nobuffer", 0);
av_dict_set(&options, "rtsp_transport", "tcp", 0);
if ((ret = avformat_open_input(&_pFormatCtx, 0, 0, 0)) < 0) {
dzlog_info("Could not open input memory. errno = %x, ret = %d\n", errno, ret);
return -1;
}
av_dict_free(&options);
if (_bVideo) {
if (avformat_find_stream_info(_pFormatCtx, 0) < 0) {
dzlog_info("Failed to retrieve input stream information\n");
return -1;
}
if (fabs(pr._vfps - 0.0f) > FLT_EPSILON) {
auto s = _pFormatCtx;
int video_index = 0;
s->streams[video_index]->avg_frame_rate.den = 1000;
s->streams[video_index]->avg_frame_rate.num = pr._vfps * 1000;
s->streams[video_index]->r_frame_rate.den = 1000;
s->streams[video_index]->r_frame_rate.num = pr._vfps * 1000;
}
}
else {
if (avformat_find_stream_info(_pFormatCtx, 0) < 0) {
dzlog_info("Failed to retrieve input stream information\n");
return -1;
}
}
_pBuff->setRemoveReaded(1);
return 0;
}
版权声明:本文为weixin_42575806原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。