ffmpeg拉流rtmp音频实时数据有延时的解决方法

来源:互联网 发布:2016淘宝刷客 编辑:程序博客网 时间:2024/05/16 17:03

最近在做一个从rtmp服务器中拉流音频实时数据会延迟播放的问题,从rtmp播放端<拉流音频数据端>发现,是探测时间太长了,超过了5s,播放数据就延迟播放了5second,

卡在了这个函数:avformat_find_stream_info(),我通过ffplay的以下命令可以解决播放延时的问题:

 

ffmpeg ffplay播放延时大问题:播放延时参数设置


参考网址:http://blog.csdn.net/cai6811376/article/details/52637158

使用ffplay播放视频源时,rtsp/rtmp等,会有一定的延时,这里我们可以通过设置ffplay播放参数将延时控制到最小。

ffplay.exe -i rtmp://xxxxxxx -fflags nobuffer 
减少缓冲

也可以减少分析码流的时间

ffplay.exe -i rtmp://xxxxxxx -analyzeduration 1000000 
码流分析时间设置,单位为微秒

RTSP低延时播放:

ffplay.exe -i rtsp://xxx -fflags nobuffer -analyzeduration 1000000 -rtsp_transport tcp

通过代码可以通过如下方式设置:

ffmpeg中avformat_stream_info函数阻塞时间太长

在使用ffmpeg播放网络流中,在执行到avformat_stream_info函数会阻塞5秒左右,这样造成播放等待时间过长,影响

用户体验,经试验,修改函数里面AVFormatContext参数,probesize和max_analyze_duration值大小

通过AVDictionary来改变AVFormatContext结构体里参数。


AVDictionary * avdic = NULL;

av_dict_set(&avdic,"probesize","2048",0);

av_dict_set(&avdic,"max_analyze_duration","1000",0);


avforamt_open_input(&pFormatCtx,url, NULL, &avdic);

avformat_find_stream_info(pFormatCtx, NULL);

我的实际代码如下:

AVDictionary* pOptions = NULL;pFormatCtx->probesize = 1 *1024;pFormatCtx->max_analyze_duration = 1 * AV_TIME_BASE;// Retrieve stream informationif(avformat_find_stream_info(pFormatCtx,&pOptions)<0){printf("Couldn't find stream information.\n");return -1;}
以下代码也是可以的:

AVDictionary * avdic = NULL;av_dict_set(&avdic,"probesize","2048",0);av_dict_set(&avdic,"max_analyze_duration","10",0);avformat_open_input(&pFormatCtx,url, NULL, &avdic);avformat_find_stream_info(pFormatCtx, NULL);

这样就不会有太大的延时问题,我设置了服务器的缓冲大小等方式没有用,通过上述方法就可以了。