‘AVCodecContext’ {aka ‘struct AVCodecContext’} has no member named ‘par’
时间: 2024-04-22 16:26:55 浏览: 37
在较新版本的FFmpeg中,`AVCodecContext`结构体(即`struct AVCodecContext`)不再具有名为`par`的成员。相反,使用`codecpar`成员来访问编解码器参数。
以下是对代码进行更新的示例,以解决`AVCodecContext`没有成员`par`的问题:
```cpp
#include <iostream>
#include <string>
extern "C" {
#include <libavcodec/avcodec.h>
#include <libavformat/avformat.h>
#include <libavutil/timestamp.h>
}
int main() {
// 初始化FFmpeg库
av_register_all();
avformat_network_init();
// 打开RTSP流
AVFormatContext* formatContext = nullptr;
if (avformat_open_input(&formatContext, "rtsp://10.31.3.156:8554/video", nullptr, nullptr) != 0) {
std::cerr << "Failed to open RTSP stream." << std::endl;
return 1;
}
// 查找视频流信息
if (avformat_find_stream_info(formatContext, nullptr) < 0) {
std::cerr << "Failed to find stream information." << std::endl;
avformat_close_input(&formatContext);
return 1;
}
// 查找视频流
AVStream* videoStream = nullptr;
int videoStreamIndex = -1;
for (unsigned int i = 0; i < formatContext->nb_streams; i++) {
if (formatContext->streams[i]->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) {
videoStream = formatContext->streams[i];
videoStreamIndex = i;
break;
}
}
if (videoStreamIndex == -1) {
std::cerr << "Failed to find video stream." << std::endl;
avformat_close_input(&formatContext);
return 1;
}
// 查找视频解码器
AVCodec* videoCodec = avcodec_find_decoder(videoStream->codecpar->codec_id);
if (!videoCodec) {
std::cerr << "Failed to find video decoder." << std::endl;
avformat_close_input(&formatContext);
return 1;
}
// 打开视频解码器
AVCodecContext* videoCodecContext = avcodec_alloc_context3(videoCodec);
if (!videoCodecContext) {
std::cerr << "Failed to allocate video codec context." << std::endl;
avformat_close_input(&formatContext);
return 1;
}
if (avcodec_parameters_to_context(videoCodecContext, videoStream->codecpar) < 0) {
std::cerr << "Failed to copy video codec parameters." << std::endl;
avcodec_free_context(&videoCodecContext);
avformat_close_input(&formatContext);
return 1;
}
if (avcodec_open2(videoCodecContext, videoCodec, nullptr) < 0) {
std::cerr << "Failed to open video codec." << std::endl;
avcodec_free_context(&videoCodecContext);
avformat_close_input(&formatContext);
return 1;
}
// 其他代码...
// 清理资源
avcodec_free_context(&videoCodecContext);
avformat_close_input(&formatContext);
return 0;
}
```
请注意,更新后的代码使用`videoStream->codecpar`来访问视频流的编解码器参数,而不是使用`videoStream->codec->par`。
希望这个更新后的示例对您有所帮助。如果您有任何疑问,请随时提问。
相关推荐
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.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)
![](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)
![](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)
![](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)
![](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)