1. $ sudo vim dir.c
include <stdio.h>
#include <inttypes.h>
#include <libavutil/log.h>
#include <libavutil/error.h>
#include <libavformat/avio.h>
//文件类型
static const char *type_string(int type)
{
switch (type) {
case AVIO_ENTRY_DIRECTORY:
return "<DIR>";
case AVIO_ENTRY_FILE:
return "<FILE>";
case AVIO_ENTRY_BLOCK_DEVICE:
return "<BLOCK DEVICE>";
case AVIO_ENTRY_CHARACTER_DEVICE:
return "<CHARACTER DEVICE>";
case AVIO_ENTRY_NAMED_PIPE:
return "<PIPE>";
case AVIO_ENTRY_SYMBOLIC_LINK:
return "<LINK>";
case AVIO_ENTRY_SOCKET:
return "<SOCKET>";
case AVIO_ENTRY_SERVER:
return "<SERVER>";
case AVIO_ENTRY_SHARE:
return "<SHARE>";
case AVIO_ENTRY_WORKGROUP:
return "<WORKGROUP>";
}
int main()
{
AVIODirEntry *entry = NULL;
AVIODirContext *ctx = NULL;
int ret;
av_log_set_level(AV_LOG_DEBUG);
if ((ret = avio_open_dir(&ctx, "./", NULL)) < 0) {
av_log(NULL, AV_LOG_ERROR, "Cannot open directory: %s.\n", av_err2str(ret));
goto fail;
}
while(1){
if ((ret = avio_read_dir(ctx, &entry)) < 0) {
av_log(NULL, AV_LOG_ERROR, "Cannot list directory: %s.\n", av_err2str(ret));
goto fail;
}
if (!entry) //当前目录中读不到列表
break;
av_log(NULL, AV_LOG_INFO, "%-9s %12"PRId64" %30s %16"PRId64" %16"PRId64" %16"PRId64"\n",
type_string(entry->type),
entry->size,
entry->name,
entry->modification_timestamp,
entry->access_timestamp,
entry->status_change_timestamp);
avio_free_directory_entry(&entry); //读完当前列表就close
}
fail:
avio_close_dir(&ctx); //当前路径关掉,entry相当于当前路径中的每个列表内容
return 0;
}
1)avio_open/read/close_dir要包含avio.h,av_err2str在error.h中,
2)PRId64需要#include <inttypes.h>,这是一种跨平台的书写方式,主要是为了同时支持32位和64位操作系统。PRId64表示64位整数,在32位系统中表示long long int,在64位系统中表示long int。相当于:
printf("%" "ld" "\n", value); //64bit OS
printf("%" "lld" "\n", value); //32bit OS
2. gcc编译,生成可执行dir文件
gcc -g -o dir dir.c `pkg-config -cflags --libs libavformat libavutil`
3. 执行dir
$ ./dir
<FILE> 744397468 aspect_ratio_SD188.ts 1725330324000000 1725330521000000 1725330324000000
<FILE> 19536 dir 1725454816000000 1725454828000000 1725454816000000
<FILE> 554 ffmpeg_file.c 1725421127000000 1725421132000000 1725421127000000
<DIR> 4096 ffmpeg 1725244680000000 1725358851000000 1725244680000000
<FILE> 0 10s.ts 1725330119000000 1725330136000000 1725330119000000
<FILE> 3993753600 out.yuv 1725251667000000 1725251967000000 1725251667000000
<FILE> 1818 dir.c 1725454814000000 1725454816000000 1725454814000000
<FILE> 496740 out.pcm 1725252158000000 1725252234000000 1725252158000000
<FILE> 17264 ffmpeg_log 1725419216000000 1725419216000000 1725419216000000
<FILE> 3932160 event_eachts_192.ts 1725250668000000 1725250726000000 1725250668000000
<DIR> 4096 minghao 1725421799000000 1725421725000000 1725421799000000
<FILE> 0 10.ts 1725330584000000 1725330521000000 1725330584000000
<FILE> 10635194 game.flv 1725359634000000 1725359652000000 1725359634000000
<FILE> 168 ffmpeg_log.c 1725416437000000 1725416446000000 1725416437000000
<FILE> 3237360 out.ts 1725329375000000 1725329428000000 1725329375000000