int avio_open(AVIOContext **s, const char *url, int flags);
int avio_open2(AVIOContext **s, const char *url, int flags,
const AVIOInterruptCB *int_cb, AVDictionary **options);
从源码中来看avio_open就是调用avio_open2()来实现的,只是把avio_open2()的最后两个参数置NULL,
s:会创建一个AVIOContext对象。
url:输入输出的地址,可以是文件地址,也可以是网络地址,比如udp://,rtmp://等。
flags:打开地址的方式,有三个宏。
AVIO_FLAG_READ:只读。
AVIO_FLAG_WRITE:只写。
AVIO_FLAG_READ_WRITE:读写。
int_cb
协议级的中断回调。
options
每个协议的私有选项,比如udp可控制包大小等。
函数会根据URL路径来识别是哪种协议,其实文件在ffmpeg中也是协议,是file://…,但是这不符合我们书写的习惯,如果识别出是文件的话,函数会自动帮我们在前面补充file://…,而不用我们自己写。
if (!(ofmt->flags & AVFMT_NOFILE)) {// 若不是自动打开文件的模式,则需要此手动操作
AVDictionary*dic = NULL;
av_dict_set(&dic, "pkt_size", "1316", 0); //Maximum UDP packet size
//av_dict_set(&dic, "fifo_size", "18800", 0);
//av_dict_set(&dic, "buffer_size", "1000000", 0);
//av_dict_set(&dic, "bitrate", "11000000", 0);
//av_dict_set(&dic, "buffer_size", "1000000", 0);//1316
av_dict_set(&dic, "reuse", "1", 0);
ret = avio_open2(&ofmt_ctx->pb, out_filename, AVIO_FLAG_WRITE, NULL, &dic);
}
libavdevice库是libavformat库的一个补充。它提供各类不一样的平台相关的复用器和解复用器,例如grabbing device(gdigrab Windows下录屏接口),音频捕获和回放等,libavdevice中的复用器大部分是AVFMT_NOFILE类型,AVFMT_NOFILE类型的复用器,不须要调用avio_open和avio_close对I/O进行操做,AVFMT_NOFILE类型的复用器都有本身的I/O函数。
不单单libavdevice中的大部分是AVFMT_NOFILE类型,libavformat也存在很多的解复用器。
关于AVFMT_NOFILE,参考:[AVFMT_NOFILE解析](https://www.shangmayuan.com/a/027018e619644bbc8782f7e9.html)
![](https://i-blog.csdnimg.cn/blog_migrate/0d6108b524cdd0722dbddcd12096157e.jpeg)
![](https://i-blog.csdnimg.cn/blog_migrate/1c1655332b870f79ad91e0b03a52d7df.jpeg)
![](https://i-blog.csdnimg.cn/blog_migrate/0d6108b524cdd0722dbddcd12096157e.jpeg)
/**
- Create and initialize a AVIOContext for accessing the
- resource indicated by url.
- @note When the resource indicated by url has been opened in
- read+write mode, the AVIOContext can be used only for writing.
- @param s Used to return the pointer to the created AVIOContext.
- In case of failure the pointed to value is set to NULL.
- @param url resource to access
- @param flags flags which control how the resource indicated by url
- is to be opened
- @return >= 0 in case of success, a negative value corresponding to an
- AVERROR code in case of failure
*/
int avio_open(AVIOContext **s, const char *url, int flags);
/**
- Create and initialize a AVIOContext for accessing the
- resource indicated by url.
- @note When the resource indicated by url has been opened in
- read+write mode, the AVIOContext can be used only for writing.
- @param s Used to return the pointer to the created AVIOContext.
- In case of failure the pointed to value is set to NULL.
- @param url resource to access
- @param flags flags which control how the resource indicated by url
- is to be opened
- @param int_cb an interrupt callback to be used at the protocols level
- @param options A dictionary filled with protocol-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 in case of success, a negative value corresponding to an
- AVERROR code in case of failure
*/
int avio_open2(AVIOContext **s, const char *url, int flags,
const AVIOInterruptCB *int_cb, AVDictionary **options);