ffmpeg 获取 rtsp 视频流 c++ 代码实现
时间: 2023-10-02 17:08:44 浏览: 523
下面是一个简单的 C++ 代码实现,使用 FFmpeg 库获取 RTSP 视频流:
```
extern "C"
{
#include <libavcodec/avcodec.h>
#include <libavformat/avformat.h>
#include <libavutil/imgutils.h>
#include <libswscale/swscale.h>
#include <libavutil/opt.h>
}
#include <iostream>
using namespace std;
int main()
{
int ret;
// 初始化 FFmpeg 库
av_register_all();
// 打开 RTSP 链接
AVFormatContext* fmt_ctx = NULL;
ret = avformat_open_input(&fmt_ctx, "rtsp://xxx.xxx.xxx.xxx:xxxx/xxx", NULL, NULL);
if (ret < 0)
{
cerr << "Could not open RTSP input: " << av_err2str(ret) << endl;
return -1;
}
// 查找流信息
ret = avformat_find_stream_info(fmt_ctx, NULL);
if (ret < 0)
{
cerr << "Could not find RTSP stream information: " << av_err2str(ret) << endl;
avformat_close_input(&fmt_ctx);
return -1;
}
// 找到视频流
AVCodec* video_codec = NULL;
int video_stream_index = av_find_best_stream(fmt_ctx, AVMEDIA_TYPE_VIDEO, -1, -1, &video_codec, 0);
if (video_stream_index < 0)
{
cerr << "Could not find RTSP video stream: " << av_err2str(video_stream_index) << endl;
avformat_close_input(&fmt_ctx);
return -1;
}
// 打开视频编解码器
AVCodecContext* codec_ctx = avcodec_alloc_context3(video_codec);
if (!codec_ctx)
{
cerr << "Could not allocate video codec context" << endl;
avformat_close_input(&fmt_ctx);
return -1;
}
ret = avcodec_parameters_to_context(codec_ctx, fmt_ctx->streams[video_stream_index]->codecpar);
if (ret < 0)
{
cerr << "Could not copy codec parameters to context: " << av_err2str(ret) << endl;
avcodec_free_context(&codec_ctx);
avformat_close_input(&fmt_ctx);
return -1;
}
ret = avcodec_open2(codec_ctx, video_codec, NULL);
if (ret < 0)
{
cerr << "Could not open video codec: " << av_err2str(ret) << endl;
avcodec_free_context(&codec_ctx);
avformat_close_input(&fmt_ctx);
return -1;
}
// 准备解码视频帧
AVFrame* frame = av_frame_alloc();
if (!frame)
{
cerr << "Could not allocate video frame" << endl;
avcodec_free_context(&codec_ctx);
avformat_close_input(&fmt_ctx);
return -1;
}
AVPacket packet;
av_init_packet(&packet);
// 初始化视频转换器
SwsContext* sws_ctx = sws_getContext(codec_ctx->width, codec_ctx->height, codec_ctx->pix_fmt,
codec_ctx->width, codec_ctx->height, AV_PIX_FMT_RGB24,
0, NULL, NULL, NULL);
if (!sws_ctx)
{
cerr << "Could not initialize video converter" << endl;
av_frame_free(&frame);
avcodec_free_context(&codec_ctx);
avformat_close_input(&fmt_ctx);
return -1;
}
// 循环读取视频帧
int frame_count = 0;
while (av_read_frame(fmt_ctx, &packet) >= 0)
{
// 只处理视频帧
if (packet.stream_index == video_stream_index)
{
// 解码视频帧
ret = avcodec_send_packet(codec_ctx, &packet);
if (ret < 0)
{
cerr << "Error sending a packet for decoding: " << av_err2str(ret) << endl;
break;
}
while (ret >= 0)
{
ret = avcodec_receive_frame(codec_ctx, frame);
if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF)
{
break;
}
else if (ret < 0)
{
cerr << "Error during decoding: " << av_err2str(ret) << endl;
break;
}
// 转换视频帧格式
uint8_t* data[1] = { (uint8_t*)frame->data[0] };
int linesize[1] = { frame->linesize[0] };
sws_scale(sws_ctx, data, linesize, 0, codec_ctx->height, frame->data, frame->linesize);
// 在这里可以对视频帧进行处理
frame_count++;
}
}
av_packet_unref(&packet);
}
// 释放资源
sws_freeContext(sws_ctx);
av_frame_free(&frame);
avcodec_free_context(&codec_ctx);
avformat_close_input(&fmt_ctx);
cout << "Processed " << frame_count << " video frames" << endl;
return 0;
}
```
请注意,此代码仅用于演示目的,实际使用需要进行适当的错误处理和资源释放。