Linux下开发ffmpeg(5),目录操作

12 篇文章 0 订阅
11 篇文章 0 订阅

目录


操作目录重要函数

/**
 * 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;
}

编译,运行

root@zhangyu-virtual-machine:/home/zhangyu/test# gcc -g -o lsDir lsDir.c `pkg-config --libs --cflags libavformat libavutil`
root@zhangyu-virtual-machine:/home/zhangyu/test# ./lsDir 
461--ffmpegfile.c
20504--lsDir
19320--file
19072--enum
796--lsDir.c
19192--point
147--fun.c
19120--mylog
18984--struct
210--enum.c
346--point.c
171--struct.c
255--funpoint.c
60--hello.c
12288--.fun.c.swp
227--mylog.c
19120--log2
791--\
18872--hello
19376--ffmpegfile
83--for.c
355--file.c
19304--funpoint
18952--for
19056--fun
root@zhangyu-virtual-machine:/home/zhangyu/test# vim lsDir.c
root@zhangyu-virtual-machine:/home/zhangyu/test# ./lsDir 
461--ffmpegfile.c
20504--lsDir
19320--file
19072--enum
796--lsDir.c
19192--point
147--fun.c
19120--mylog
18984--struct
210--enum.c
346--point.c
171--struct.c
255--funpoint.c
60--hello.c
12288--.fun.c.swp
227--mylog.c
19120--log2
791--\
18872--hello
19376--ffmpegfile
83--for.c
355--file.c
19304--funpoint
18952--for
19056--fun
root@zhangyu-virtual-machine:/home/zhangyu/test# 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值