avformat_open_input() 是一个函数,通常用于打开输入媒体流并读取其头部信息。具体位于 libavformat 组件中。
int avformat_open_input(AVFormatContext **ps, const char *url, AVInputFormat *fmt, AVDictionary **options);
参数说明:
AVFormatContext **ps:这是一个指向 AVFormatContext 结构体的指针的指针。成功调用此函数后,AVFormatContext 结构体将被分配并填充有关媒体流的信息。
const char *url:要打开的媒体文件的 URL 或路径。这可以是本地文件路径、HTTP URL 或其他支持的协议。
AVInputFormat *fmt:指定输入格式的 AVInputFormat 结构体的指针。如果此参数为 NULL,则函数将根据文件扩展名或内容自动检测输入格式。
AVDictionary **options:一个指向 AVDictionary 结构体的指针的指针,用于传递打开媒体文件时的选项。这些选项可以控制打开文件或网络流时的各种行为。
返回值:
如果成功打开流并读取了头部信息,函数将返回 0。
如果出现错误,将返回一个负的错误码。
使用 avformat_open_input() 之后,通常需要使用 avformat_close_input() 函数来释放 AVFormatContext 及其相关资源。
例子:
extern "C"
{
//编码库
#include <libavcodec/avcodec.h>
//封装格式库
#include <libavformat/avformat.h>
//解码库
#include <libswscale/swscale.h>
//各种设备的输入输出
#include <libavdevice/avdevice.h>
//多媒体处理库 工具库
#include <libavutil/opt.h>
}
int main()
{
const char* path = "xx.mp4";
AVDictionary* opt=NULL;
AVFormatContext* pFormat = NULL;
int ret = avformat_open_input(&pFormat,path,NULL,NULL);
printf("ret: %d",ret);
if (ret)
{
printf(" avformat_open_input failed\n");
return -1;
}
printf(" avformat_open_input success\n");
return 0;
}