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;
}
相关推荐
「QT(C++)开发工程师」7 小时前
C++ 观察者模式
java·c++·观察者模式
m0_706653237 小时前
高性能网络协议栈
开发语言·c++·算法
永远睡不够的入7 小时前
类和对象(上)
开发语言·c++·算法
橘颂TA7 小时前
【剑斩OFFER】算法的暴力美学——力扣 207 题:课程表
数据结构·c++·算法·leetcode·职场和发展
孞㐑¥8 小时前
算法—链表
开发语言·c++·经验分享·笔记·算法
f狐0狸x8 小时前
【C++修炼之路】C++容器:stack、queue、deque 基本用法详解
c++·stl
Vertira8 小时前
win10/10 下载并安装ffmpeg.exe 的官方详细方法 (已解决)
ffmpeg
Ronaldinho Gaúch8 小时前
leetcode279完全平方数
c++·算法·动态规划
王老师青少年编程8 小时前
2024信奥赛C++提高组csp-s复赛真题及题解:超速检测
c++·真题·csp·信奥赛·csp-s·提高组·超速检测
Howrun7778 小时前
C++_bind_可调用对象转化器
开发语言·c++·算法