FFmpeg 基本API avformat_open_input函数内部调用流程分析

1、avformat_open_input 函数定义说明

avformat_open_input 是 FFmpeg 库中的一个函数,用于打开一个输入 URL,并创建一个 AVFormatContext 结构体的指针。在使用 FFmpeg 进行音视频处理时,通常需要先调用这个函数来创建一个 AVFormatContext 对象。

函数原型如下:

cpp 复制代码
int avformat_open_input(AVFormatContext **ps, const char *url, AVInputFormat *fmt, AVDictionary **options);

参数说明:

  • AVFormatContext** 指向 AVFormatContext 对象指针的地址。函数会分配或初始化此上下文。
  • const char* url媒体文件的路径或URL。可以是本地文件,也支持网络流(如rtmp://, http://)。
  • AVInputFormat* fmt强制指定输入文件的封装格式。通常传 NULL,让FFmpeg自动探测格式。
  • AVDictionary** options一个字典,用于传递特定于格式或设备的额外选项。可以为 NULL。

2、avformat_open_input详细创建过程说明

详细过程已经在 FFmpeg 播放播放 HTTP网络流读取数据过程分析 详细说明具体创建过程。

3、avformat_open_input调用流程说明

4、avformat_open_input使用实例

cpp 复制代码
#include <libavformat/avformat.h>

int main() {
    AVFormatContext *formatCtx = NULL;
    const char *filename = "test.mp4";
    
    // 打开输入文件
    int ret = avformat_open_input(&formatCtx, filename, NULL, NULL);
    if (ret < 0) {
        char errbuf[128];
        av_strerror(ret, errbuf, sizeof(errbuf));
        fprintf(stderr, "无法打开输入文件 '%s': %s\n", filename, errbuf);
        return -1;
    }

    /****** 其他操作 *****/
    // 关闭并释放资源
    avformat_close_input(&formatCtx);
    return 0;
}
相关推荐
xingyuzhisuan3 小时前
团队实践:引入 Vera 1.1 之后,视频内容团队工作流重构
人工智能·重构·音视频
djjjx.3 小时前
【 C++ 】多态
开发语言·c++·多态
鸿芯微控科技4 小时前
MFC关断后还有流量怎么办?零流量、阀门泄漏、压差与Python分析
c++·python·mfc·质量流量控制器·关断泄漏·零流量测试
美狐美颜SDK开放平台4 小时前
第三方美颜SDK接入教程:直播APP实现实时美颜、滤镜与美型效果的方法
人工智能·深度学习·音视频·sdk·美颜sdk·视频美颜sdk·直播app开发
ShineWinsu15 小时前
对于C++:C++11中lambda、function、bind的解析
c++·面试·笔试·开发·lambda·bind·function
码匠许师傅15 小时前
【C++ 面试真题】聊聊 C++ 的拷贝构造与拷贝赋值
java·c++·面试
AI服务老曹16 小时前
边云协同视频分析性能优化指南:多站点部署下的资源瓶颈突破与调优实战
性能优化·音视频
旖旎夜光16 小时前
LeetCode 3:无重复字符的最长子串(滑动窗口) —— 题解
数据结构·c++·算法·leetcode·滑动窗口
汉字萌萌哒16 小时前
2024CSP-J入门级C++真题详解
开发语言·c++
hy.z_77717 小时前
【C++】13. 继承
c++