ffmpeg或opencv以rtsp读取网络摄像头时,网络不通,程序持续等待或等待几十秒
在OpenCV3.4.7的 opencv\sources\modules\videoio\src\cap_ffmpeg_mpl.hpp,如下打开流超时时间为30s。
#define LIBAVFORMAT_INTERRUPT_OPEN_TIMEOUT_MS 30000
#define LIBAVFORMAT_INTERRUPT_READ_TIMEOUT_MS 30000
因程序同时使用的ffmpeg和OpenCV的库
优化如下:使用ffmpeg的api监测rtsp的网络状态,然后用opencv得VideoCapture读取摄像头数据。
bool checkRTSPNet(std::string url)
{
bool ret = false;
AVFormatContext* pFormatCtx = NULL;
pFormatCtx = avformat_alloc_context();
AVDictionary* options = NULL;
av_dict_set(&options, "max_delay", "500000", 0);
av_dict_set(&options, "stimeout", "500000", 0); //设置超时断开连接时间(ms) 500ms
av_dict_set(&options, "rtsp_transport", "tcp", 0); //以tcp方式打开;tcp/udp
if( avformat_open_input(&pFormatCtx, url.c_str(), NULL, &options) != 0 )
{
printf("Couldn't open input stream, {0},", url);
}
else
{
avformat_close_input(&pFormatCtx);
ret = true;
}
avformat_free_context(pFormatCtx);
return ret;
}
版权声明:本文为qq_38795209原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。