avio_open2函数分析

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)

ffmpeg是音视频必备,但即使从业数年,它似乎依然有无穷的秘密,感兴趣添加笔者微信:YQW1163720468,加入ffmpeg微信群讨论。但记得备注:ffmpeg爱好者

/**

  • 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);
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
`avio_open` 是 FFmpeg 中的一个函数,用于打开输入或输出的多媒体文件。 下面是 `avio_open` 的函数原型: ``` int avio_open(AVIOContext **s, const char *url, int flags); ``` 其中,`s` 是指向 `AVIOContext` 指针的指针,`url` 是输入/输出文件的 URL,`flags` 是打开文件的标志位。 使用 `avio_open` 函数打开文件的步骤如下: 1. 调用 `avio_alloc_context` 函数创建 `AVIOContext` 结构体对象。 ```c AVIOContext *avio_alloc_context( unsigned char *buffer, int buffer_size, int write_flag, void *opaque, int (*read_packet)(void *opaque, uint8_t *buf, int buf_size), int (*write_packet)(void *opaque, uint8_t *buf, int buf_size), int64_t (*seek)(void *opaque, int64_t offset, int whence) ); ``` 其中,`buffer_size` 是缓冲区大小,`read_packet` 是读取数据的回调函数,`write_packet` 是写入数据的回调函数,`seek` 是文件偏移量的回调函数,`opaque` 是用户自定义的参数,可以在回调函数中使用。 2. 调用 `avio_open` 函数打开文件。 ```c int ret = avio_open(&s, url, flags); if (ret < 0) { // 打开文件失败 return ret; } ``` 其中,`ret` 是返回值,小于 0 表示打开文件失败,大于等于 0 表示打开文件成功,返回的值是文件句柄。 3. 使用完毕后,调用 `avio_close` 函数关闭文件。 ```c avio_close(s); ``` 完整的使用示例代码如下: ```c #include <libavformat/avformat.h> int main(int argc, char *argv[]) { AVIOContext *s = NULL; const char *url = "input.mp4"; int flags = AVIO_FLAG_READ; // 创建 AVIOContext 对象 s = avio_alloc_context(NULL, 0, 0, NULL, NULL, NULL, NULL); if (!s) { printf("Failed to allocate AVIOContext\n"); return -1; } // 打开文件 int ret = avio_open(&s, url, flags); if (ret < 0) { printf("Failed to open file: %s\n", av_err2str(ret)); return -1; } // 使用 AVIOContext 对象读写文件 // 关闭文件 avio_close(s); return 0; } ``` 注意事项: 1. `AVIOContext` 结构体对象需要手动释放,使用 `avio_context_free` 函数。 2. 在使用完毕后,需要调用 `avio_close` 函数关闭文件。 3. 在使用 `avio_open` 函数打开文件前,需要先调用 `av_register_all` 函数注册 FFmpeg 中的所有组件。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

、、、、南山小雨、、、、

分享对你有帮助,打赏一下吧!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值