/**
* Open directory for reading.
*
* @param s directory read context. Pointer to a NULL pointer must be passed.
* @param url directory to be listed.
* @param options A dictionary filled with protocol-private options. On return
* this parameter will be destroyed and replaced with a dictionary
* containing options that were not found. May be NULL.
* @return >=0 on success or negative on error.
*/
int avio_open_dir(AVIODirContext **s, const char *url, AVDictionary **options);
/**
* Get next directory entry.
*
* Returned entry must be freed with avio_free_directory_entry(). In particular
* it may outlive AVIODirContext.
*
* @param s directory read context.
* @param[out] next next entry or NULL when no more entries.
* @return >=0 on success or negative on error. End of list is not considered an
* error.
*/
int avio_read_dir(AVIODirContext *s, AVIODirEntry **next);
/**
* Close directory.
*
* @note Entries created using avio_read_dir() are not deleted and must be
* freeded with avio_free_directory_entry().
*
* @param s directory read context.
* @return >=0 on success or negative on error.
*/
int avio_close_dir(AVIODirContext **s);
操作目录重要的结构体
AVIODirContext
操作目录的上下文,当我们avio_open_dir,就会生成一个AVIODirContext,avio_read_dir时候需要传入这个AVIODirContext
AVIODirEntry
目录项,用于存放文件名,文件大小等信息
/**
* Describes single entry of the directory.
*
* Only name and type fields are guaranteed be set.
* Rest of fields are protocol or/and platform dependent and might be unknown.
*/
typedef struct AVIODirEntry {
char *name; /**< Filename */
int type; /**< Type of the entry */
int utf8; /**< Set to 1 when name is encoded with UTF-8, 0 otherwise.
Name can be encoded with UTF-8 even though 0 is set. */
int64_t size; /**< File size in bytes, -1 if unknown. */
int64_t modification_timestamp; /**< Time of last modification in microseconds since unix
epoch, -1 if unknown. */
int64_t access_timestamp; /**< Time of last access in microseconds since unix epoch,
-1 if unknown. */
int64_t status_change_timestamp; /**< Time of last status change in microseconds since unix
epoch, -1 if unknown. */
int64_t user_id; /**< User ID of owner, -1 if unknown. */
int64_t group_id; /**< Group ID of owner, -1 if unknown. */
int64_t filemode; /**< Unix file mode, -1 if unknown. */
} AVIODirEntry;
typedef struct AVIODirContext {
struct URLContext *url_context;
} AVIODirContext;
lsDir.c
#include <libavutil/log.h>
#include <libavformat/avformat.h>
int main(int argc,char* argv[]){
AVIODirContext *ctx = NULL;
AVIODirEntry *entry = NULL;
//返回信息,成功失败
int ret;
av_log_set_level(AV_LOG_INFO);
avio_open_dir(&ctx,"./",NULL);
if (ret<0){
//av_err2str可以将错误码转换成string
av_log(NULL,AV_LOG_ERROR,"cant open dir :%s\n",av_err2str(ret));
goto _fail;
}
//打开目录后,访问目录中的每一项
while(1){
avio_read_dir(ctx,&entry);
if (ret<0){
av_log(NULL,AV_LOG_ERROR,"cant read dir :%s\n",av_err2str(ret));
}
if (!entry){
break;
}
av_log(NULL,AV_LOG_INFO,"%ld--%s\n",entry->size,entry->name);
//打印完信息后,释放到entry
avio_free_directory_entry(&entry);
}
_fail:
avio_close_dir(&ctx);
return 0;
}