ffmpeg 获取 rtsp 视频流 并解码保存为图片 c++ 代码实现
时间: 2023-02-07 18:54:01 浏览: 1005
ffmpeg 是一款非常强大的多媒体处理工具,可以用来获取 rtsp 视频流并解码保存为图片。下面是一个使用 ffmpeg 获取 rtsp 视频流并解码保存为图片的 C 代码实现:
```c
#include <stdio.h>
#include <libavformat/avformat.h>
#include <libavcodec/avcodec.h>
#include <libswscale/swscale.h>
int main(int argc, char* argv[])
{
// 初始化 FFmpeg 库
av_register_all();
// 打开 rtsp 视频流
AVFormatContext* pFormatCtx = NULL;
if (avformat_open_input(&pFormatCtx, "rtsp://your_rtsp_url", NULL, NULL) != 0)
{
printf("Couldn't open input stream.\n");
return -1;
}
// 查找视频流信息
if (avformat_find_stream_info(pFormatCtx, NULL) < 0)
{
printf("Couldn't find stream information.\n");
return -1;
}
// 找到视频流的编号
int videoStream = -1;
for (int i = 0; i < pFormatCtx->nb_streams; i++)
{
if (pFormatCtx->streams[i]->codec->codec_type == AVMEDIA_TYPE_VIDEO)
{
videoStream = i;
break;
}
}
if (videoStream == -1)
{
printf("Didn't find a video stream.\n");
return -1;
}
// 获取视频流的解码器上下文
AVCodecContext* pCodecCtx = pFormatCtx->streams[videoStream]->codec;
// 查找视频解码器
AVCodec* pCodec = avcodec_find_decoder(pCodecCtx->codec_id);
if (pCodec == NULL)
{
printf("Codec not found.\n");
return -1;
}
// 打开解码器
if (avcodec_open2(pCodecCtx, pCodec, NULL) < 0)
{
printf("Could not open codec.\n");
return -1;
}
// 分
阅读全文
相关推荐
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231044955.png)
![-](https://img-home.csdnimg.cn/images/20241231044955.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231044955.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)