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;
}
相关推荐
小豆庄2 小时前
测试视频里的人脸车牌敏感信息自动化脱敏方案
计算机视觉·目标跟踪·ffmpeg·音视频·边缘计算·视频·敏捷流程
光泽雨4 小时前
WPF 音视频四大核心方案对比
wpf·音视频
apcipot_rain4 小时前
C++转Java日记(02)
java·开发语言·c++
揽秀亭长4 小时前
音频转乐谱如何实现?四类AI 扒谱工具的流程
音视频
杜 硕4 小时前
从零开始的C++生活——第七讲——02
开发语言·c++·生活
Rabitebla12 小时前
【Linux 底层原理】进程(十):进程等待详解---从僵尸进程到 wait、waitpid 与 status
linux·c++·笔记·学习·visual studio
西西弗Sisyphus14 小时前
C++ 超时互斥锁(Timed Mutex):std::timed_mutex 与 std::unique_lock
c++
今天要早睡_17 小时前
C++01_入门基础复习
开发语言·c++
Android系统攻城狮17 小时前
Linux FFmpeg深度解析之avcodec_default_get_encode_buffer调用流程与实战(二十八)
linux·运维·服务器·ffmpeg·车载音视频·ffmpeg音视频
Fuyo_111919 小时前
C++ 运行时多态与虚函数机制全面剖析【面试重点】
开发语言·c++·面试