ffmpeg打开RTSP慢的解决方法

记录一下ffmpeg打开海康NVR的RTSP总是开始耗时太长 竟达2/3秒之久.开始以为是这里拖慢的 av_dict_set(&opts, "rtsp_transport", "tcp", 0); //设置tcp or udp av_dict_set(&opts, "stimeout", "3000000", 0); //设置超时3秒看来不是的,这里对延迟是有处理机制.调试发现avformat_find_stream_info(pFo
摘要由CSDN通过智能技术生成

记录一下ffmpeg打开海康NVR的RTSP总是开始耗时太长 竟达2/3秒之久. 代码工程

开始以为是rtsp_transport这里拖慢的

    av_dict_set(&opts, "rtsp_transport", "tcp", 0);     //设置tcp or udp
    av_dict_set(&opts, "stimeout", "3000000", 0);       //设置超时3秒

看来不是的,这里对延迟是有处理机制. 调试才发现

avformat_find_stream_info(pFormatCtx, NULL)

耗时很是严重,查找资料 发现中间做了不少的事,一气呵成. 

在WIN下貌似不做这个处理一样能处理.速度上去了

播放如下:

int CGdiPlay::run()
{
    hdcDes = GetDC(m_hWnd);
    hdcSrc = CreateCompatibleDC(hdcDes);
    packet = av_packet_alloc();
    CalcVideoWH(m_hWnd);

    ::CoInitialize(NULL);
    while (bStateFlag)
    {
        if (packet == NULL)
        {
            packet = av_packet_alloc();
            av_usleep(20 * 1000);
            continue;
        }
        if (av_read_frame(pFo
  • 1
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
FFmpeg 6.0版本不存在,最新版本是4.4。无论哪个版本,使用FFmpeg打开rtsp流并将其传递给OpenCV库进行处理的一般步骤如下: 1. 引入FFmpeg和OpenCV库的头文件: ```c++ #include <libavcodec/avcodec.h> #include <libavformat/avformat.h> #include <libswscale/swscale.h> #include <opencv2/opencv.hpp> ``` 2. 初始化FFmpeg库: ```c++ av_register_all(); avformat_network_init(); ``` 3. 打开rtsp流并获取流信息: ```c++ AVFormatContext* formatContext = avformat_alloc_context(); if (avformat_open_input(&formatContext, "rtsp://xxx.xxx.xxx.xxx:xxxx/", NULL, NULL) != 0) { avformat_free_context(formatContext); return -1; } if (avformat_find_stream_info(formatContext, NULL) < 0) { avformat_close_input(&formatContext); avformat_free_context(formatContext); return -1; } int videoStreamIndex = -1; for (int i = 0; i < formatContext->nb_streams; i++) { if (formatContext->streams[i]->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) { videoStreamIndex = i; break; } } if (videoStreamIndex == -1) { avformat_close_input(&formatContext); avformat_free_context(formatContext); return -1; } AVStream* videoStream = formatContext->streams[videoStreamIndex]; AVCodecParameters* videoCodecParameters = videoStream->codecpar; AVCodec* videoCodec = avcodec_find_decoder(videoCodecParameters->codec_id); if (!videoCodec) { avformat_close_input(&formatContext); avformat_free_context(formatContext); return -1; } AVCodecContext* videoCodecContext = avcodec_alloc_context3(videoCodec); if (!videoCodecContext) { avformat_close_input(&formatContext); avformat_free_context(formatContext); return -1; } if (avcodec_parameters_to_context(videoCodecContext, videoCodecParameters) < 0) { avcodec_close(videoCodecContext); avcodec_free_context(&videoCodecContext); avformat_close_input(&formatContext); avformat_free_context(formatContext); return -1; } if (avcodec_open2(videoCodecContext, videoCodec, NULL) < 0) { avcodec_close(videoCodecContext); avcodec_free_context(&videoCodecContext); avformat_close_input(&formatContext); avformat_free_context(formatContext); return -1; } ``` 4. 创建OpenCV窗口和图像缓冲区: ```c++ cv::namedWindow("Video", cv::WINDOW_NORMAL); cv::Mat frame; ``` 5. 解码rtsp流并将其传递给OpenCV进行处理: ```c++ AVPacket packet; AVFrame* frameYUV = av_frame_alloc(); AVFrame* frameRGB = av_frame_alloc(); struct SwsContext* swsContext = sws_getContext(videoCodecContext->width, videoCodecContext->height, videoCodecContext->pix_fmt, videoCodecContext->width, videoCodecContext->height, AV_PIX_FMT_BGR24, SWS_BILINEAR, NULL, NULL, NULL); while (av_read_frame(formatContext, &packet) >= 0) { if (packet.stream_index == videoStreamIndex) { avcodec_send_packet(videoCodecContext, &packet); while (avcodec_receive_frame(videoCodecContext, frameYUV) == 0) { sws_scale(swsContext, frameYUV->data, frameYUV->linesize, 0, videoCodecContext->height, frameRGB->data, frameRGB->linesize); cv::Mat srcMat(videoCodecContext->height, videoCodecContext->width, CV_8UC3, frameRGB->data[0]); cv::cvtColor(srcMat, frame, cv::COLOR_BGR2RGB); cv::imshow("Video", frame); cv::waitKey(1); } } av_packet_unref(&packet); } ``` 6. 释放FFmpeg和OpenCV资源: ```c++ sws_freeContext(swsContext); av_frame_free(&frameYUV); av_frame_free(&frameRGB); avcodec_close(videoCodecContext); avcodec_free_context(&videoCodecContext); avformat_close_input(&formatContext); avformat_free_context(formatContext); cv::destroyAllWindows(); ``` 以上步骤提供了一个基本的框架,具体实现可能会因为不同的需求和环境而有所不同,需要根据实际情况进行调整和修改。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值