ffmpeg或opencv以rtsp讀取網絡攝像頭時,網絡不通,程序持續等待或等待幾十秒

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;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章