ffmpeg 获取 rtsp 视频流 c++ 代码实现

时间: 2023-10-02 21:10:55 浏览: 45
以下是获取 RTSP 视频流并转换为 OpenCV Mat 格式的 C++ 代码示例,需要使用 FFmpeg 库: ```c++ #include <opencv2/opencv.hpp> extern "C" { #include <libavformat/avformat.h> #include <libavcodec/avcodec.h> #include <libswscale/swscale.h> } int main(int argc, char* argv[]) { AVFormatContext* pFormatCtx = nullptr; AVCodecContext* pCodecCtx = nullptr; AVCodec* pCodec = nullptr; AVFrame* pFrame = nullptr; AVPacket* packet = nullptr; struct SwsContext* img_convert_ctx = nullptr; av_register_all(); avformat_network_init(); // 填写 RTSP 地址 const char* url = "rtsp://example.com/stream"; if (avformat_open_input(&pFormatCtx, url, nullptr, nullptr) != 0) { std::cerr << "Failed to open input stream." << std::endl; return -1; } if (avformat_find_stream_info(pFormatCtx, nullptr) < 0) { std::cerr << "Failed to find stream information." << std::endl; return -1; } // 找到视频流 int videoIndex = -1; for (int i = 0; i < pFormatCtx->nb_streams; i++) { if (pFormatCtx->streams[i]->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) { videoIndex = i; break; } } if (videoIndex == -1) { std::cerr << "Failed to find video stream." << std::endl; return -1; } // 获取视频解码器 pCodecCtx = avcodec_alloc_context3(nullptr); avcodec_parameters_to_context(pCodecCtx, pFormatCtx->streams[videoIndex]->codecpar); pCodec = avcodec_find_decoder(pCodecCtx->codec_id); if (pCodec == nullptr) { std::cerr << "Failed to find codec." << std::endl; return -1; } if (avcodec_open2(pCodecCtx, pCodec, nullptr) < 0) { std::cerr << "Failed to open codec." << std::endl; return -1; } // 分配内存 pFrame = av_frame_alloc(); packet = av_packet_alloc(); // 初始化转换器 img_convert_ctx = sws_getContext(pCodecCtx->width, pCodecCtx->height, pCodecCtx->pix_fmt, pCodecCtx->width, pCodecCtx->height, AV_PIX_FMT_BGR24, SWS_BICUBIC, nullptr, nullptr, nullptr); while (av_read_frame(pFormatCtx, packet) >= 0) { if (packet->stream_index != videoIndex) { av_packet_unref(packet); continue; } // 解码 int ret = avcodec_send_packet(pCodecCtx, packet); if (ret < 0) { std::cerr << "Error sending a packet for decoding." << std::endl; break; } while (ret >= 0) { ret = avcodec_receive_frame(pCodecCtx, pFrame); if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF) { break; } else if (ret < 0) { std::cerr << "Error during decoding." << std::endl; break; } // 转换为 OpenCV Mat 格式 cv::Mat frame(pFrame->height, pFrame->width, CV_8UC3); sws_scale(img_convert_ctx, pFrame->data, pFrame->linesize, 0, pCodecCtx->height, frame.data, frame.step); // 显示图像 cv::imshow("Frame", frame); cv::waitKey(1); av_frame_unref(pFrame); } av_packet_unref(packet); } // 释放资源 av_packet_free(&packet); av_frame_free(&pFrame); avcodec_close(pCodecCtx); avcodec_free_context(&pCodecCtx); avformat_close_input(&pFormatCtx); sws_freeContext(img_convert_ctx); return 0; } ``` 需要注意的是,该代码示例仅适用于单个 RTSP 视频流的情况。如果需要同时处理多个 RTSP 视频流,需要使用多线程或多进程来实现。

相关推荐

最新推荐

recommend-type

Python_从合成数据代码和模型中学习.zip

Python_从合成数据代码和模型中学习
recommend-type

基于Javascript的enroll微信小程序报名设计源码

本项目是基于Javascript的enroll微信小程序报名设计源码,包含35个文件,其中包括8个JSON文件、7个JavaScript文件、6个WXSS文件、5个WXML文件、3个JPG图片文件、3个PNG图片文件、1个Gitignore文件、1个Markdown文件和1个ICO文件。这个项目是一个微信小程序报名设计,旨在通过Javascript和微信小程序技术栈实现,为用户提供便捷的报名服务。
recommend-type

ALINX黑金AX7020 PYNQ3.0.1镜像

编译好的黑金AX7020 PYNQ3.0.1镜像,功能已验证。 芯片ZYNQ7020,具体型号xc7z070clg400-2 受网盘单文件大小限制,使用分卷压缩的方式上传 注意!请自行下载所有的分卷后再解压
recommend-type

lol r3nzskin

lol skin move it to lol gamed dir
recommend-type

zigbee-cluster-library-specification

最新的zigbee-cluster-library-specification说明文档。
recommend-type

管理建模和仿真的文件

管理Boualem Benatallah引用此版本:布阿利姆·贝纳塔拉。管理建模和仿真。约瑟夫-傅立叶大学-格勒诺布尔第一大学,1996年。法语。NNT:电话:00345357HAL ID:电话:00345357https://theses.hal.science/tel-003453572008年12月9日提交HAL是一个多学科的开放存取档案馆,用于存放和传播科学研究论文,无论它们是否被公开。论文可以来自法国或国外的教学和研究机构,也可以来自公共或私人研究中心。L’archive ouverte pluridisciplinaire
recommend-type

MySQL数据库性能提升秘籍:揭秘性能下降幕后真凶及解决策略

![MySQL数据库性能提升秘籍:揭秘性能下降幕后真凶及解决策略](http://xiaoyuge.work/explain-sql/index/2.png) # 1. MySQL数据库性能优化概述** MySQL数据库性能优化是一项至关重要的任务,可以显著提高应用程序的响应时间和整体用户体验。优化涉及识别和解决影响数据库性能的因素,包括硬件资源瓶颈、软件配置不当和数据库设计缺陷。通过采取适当的优化策略,可以显著提升数据库性能,满足业务需求并提高用户满意度。 # 2. MySQL数据库性能下降的幕后真凶 ### 2.1 硬件资源瓶颈 #### 2.1.1 CPU利用率过高 **症状:
recommend-type

如何在unity创建按钮

在 Unity 中创建按钮的步骤如下: 1. 在 Unity 中创建一个 UI Canvas,选择 GameObject -> UI -> Canvas。 2. 在 Canvas 中创建一个按钮,选择 GameObject -> UI -> Button。 3. 在场景视图中调整按钮的位置和大小。 4. 在 Inspector 中设置按钮的文本、颜色、字体等属性。 5. 添加按钮的响应事件,选择按钮,在 Inspector 的 On Click () 中添加相应的方法。 这样就可以创建一个按钮了,你可以在游戏中使用它来触发相应的操作。
recommend-type

JSBSim Reference Manual

JSBSim参考手册,其中包含JSBSim简介,JSBSim配置文件xml的编写语法,编程手册以及一些应用实例等。其中有部分内容还没有写完,估计有生之年很难看到完整版了,但是内容还是很有参考价值的。
recommend-type

"互动学习:行动中的多样性与论文攻读经历"

多样性她- 事实上SCI NCES你的时间表ECOLEDO C Tora SC和NCESPOUR l’Ingén学习互动,互动学习以行动为中心的强化学习学会互动,互动学习,以行动为中心的强化学习计算机科学博士论文于2021年9月28日在Villeneuve d'Asq公开支持马修·瑟林评审团主席法布里斯·勒菲弗尔阿维尼翁大学教授论文指导奥利维尔·皮耶昆谷歌研究教授:智囊团论文联合主任菲利普·普雷教授,大学。里尔/CRISTAL/因里亚报告员奥利维耶·西格德索邦大学报告员卢多维奇·德诺耶教授,Facebook /索邦大学审查员越南圣迈IMT Atlantic高级讲师邀请弗洛里安·斯特鲁布博士,Deepmind对于那些及时看到自己错误的人...3谢谢你首先,我要感谢我的两位博士生导师Olivier和Philippe。奥利维尔,"站在巨人的肩膀上"这句话对你来说完全有意义了。从科学上讲,你知道在这篇论文的(许多)错误中,你是我可以依