/**
* Open an input stream and read the header. The codecs are not opened.
* The stream must be closed with avformat_close_input().
*
* @param ps Pointer to user-supplied AVFormatContext (allocated by avformat_alloc_context).
* May be a pointer to NULL, in which case an AVFormatContext is allocated by this
* function and written into ps.
* Note that a user-supplied AVFormatContext will be freed on failure.
* @param url URL of the stream to open.
* @param fmt If non-NULL, this parameter forces a specific input format.
* Otherwise the format is autodetected.
* @param options A dictionary filled with AVFormatContext and demuxer-private options.
* On return this parameter will be destroyed and replaced with a dict containing
* options that were not found. May be NULL.
*
* @return 0 on success, a negative AVERROR on failure.
*
* @note If you want to use custom IO, preallocate the format context and set its pb field.
*/
int avformat_open_input(AVFormatContext **ps, const char *url, ff_const59 AVInputFormat *fmt, AVDictionary **options);
该函数用于打开多媒体数据并且获得一些相关的信息
参数分析:
1.AVFormatContext **ps:
指向用户提供的AVFormatContext的指针(由avformat_alloc_context分配,avformat_free_context释放)或者指向NULL的指针,在这种情况下,AVFormatContext由函数并写入ps
注意:两种情况:如果是自己申请空间的要自己释放,如果传入NULL只需要调用关闭就会自动释放
2.const char *url:打开的视音频流的url,并会保存到AVFormatContext的url中
3.AVInputFormat *fmt:强制指定AVFormatContext中AVInputFormat的。这个参数一般情况下可以设置为NULL,这样FFmpeg可以自动检测AVInputFormat。
4.AVDictionary **options:附加的一些选项,一般情况下可以设置为NULL。
//第一种自己申请
AVFormatContext *pFormatCtx = avformat_alloc_context();
if(avformat_open_input(&pFormatCtx,filepath,NULL,NULL)!=0){
printf("Couldn't open input stream.\n");
return -1;
}
avformat_close_input(&pFormatCtx);
avformat_free_context(pFormatCtx);
//第二种传入NULL
AVFormatContext *pFormatCtx = NULL;
if(avformat_open_input(&pFormatCtx,filepath,NULL,NULL)!=0){
printf("Couldn't open input stream.\n");
return -1;
}
avformat_close_input(&pFormatCtx);//会自动释放