美文网首页
FFmpeg 示例-avio_dir_cmd

FFmpeg 示例-avio_dir_cmd

作者: smallest_one | 来源:发表于2019-02-23 16:05 被阅读19次

    目录

    1. 参考
    2. 示例说明
    3. 示例代码

    1. 参考

    2. 示例说明

    示例程序[1]演示了如何通过AVIOContext操作文件资源。

    示例流程如下。


    FFmpeg_avio_dir_cmd.png

    主要函数说明:

    • avio_open_dir:根据指定的路径打开文件夹。
    • avio_read_dir:获取下一个文件夹中的条目。
    • avio_free_directory_entry:释放在avio_read_dir()中创建的AVIODirEntry对象。
    • avio_close_dir:关闭文件夹。
    • avpriv_io_delete:删除一个资源。
    • avpriv_io_move:移动或者重命名一个资源。

    说明:

    1. avio_open_dir(), avio_read_dir(), avio_free_directory_entry(), avio_close_dir()在FFmpeg的代码中除了示例程序[1]中有使用,其他地方没有使用的。
    2. avpriv_io_delete()和avpriv_io_move()在libavformat/dashenc.c中有使用到。

    3. 示例程序

    示例程序来自[1]。

    #include <libavcodec/avcodec.h>
    #include <libavformat/avformat.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>";
        case AVIO_ENTRY_UNKNOWN:
        default:
            break;
        }
        return "<UNKNOWN>";
    }
    
     static int list_op(const char *input_dir)
    {
        AVIODirEntry *entry = NULL;
        AVIODirContext *ctx = NULL;
        int cnt, ret;
        char filemode[4], uid_and_gid[20];
    
         if ((ret = avio_open_dir(&ctx, input_dir, NULL)) < 0) {
            av_log(NULL, AV_LOG_ERROR, "Cannot open directory: %s.\n", av_err2str(ret));
            goto fail;
        }
    
         cnt = 0;
        for (;;) {
            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;
            if (entry->filemode == -1) {
                snprintf(filemode, 4, "???");
            } else {
                snprintf(filemode, 4, "%3"PRIo64, entry->filemode);
            }
            snprintf(uid_and_gid, 20, "%"PRId64"(%"PRId64")", entry->user_id, entry->group_id);
            if (cnt == 0)
                av_log(NULL, AV_LOG_INFO, "%-9s %12s %30s %10s %s %16s %16s %16s\n",
                       "TYPE", "SIZE", "NAME", "UID(GID)", "UGO", "MODIFIED",
                       "ACCESSED", "STATUS_CHANGED");
            av_log(NULL, AV_LOG_INFO, "%-9s %12"PRId64" %30s %10s %s %16"PRId64" %16"PRId64" %16"PRId64"\n",
                   type_string(entry->type),
                   entry->size,
                   entry->name,
                   uid_and_gid,
                   filemode,
                   entry->modification_timestamp,
                   entry->access_timestamp,
                   entry->status_change_timestamp);
            avio_free_directory_entry(&entry);
            cnt++;
        };
    
       fail:
        avio_close_dir(&ctx);
        return ret;
    }
    
     static int del_op(const char *url)
    {
        int ret = avpriv_io_delete(url);
        if (ret < 0)
            av_log(NULL, AV_LOG_ERROR, "Cannot delete '%s': %s.\n", url, av_err2str(ret));
        return ret;
    }
    
     static int move_op(const char *src, const char *dst)
    {
        int ret = avpriv_io_move(src, dst);
        if (ret < 0)
            av_log(NULL, AV_LOG_ERROR, "Cannot move '%s' into '%s': %s.\n", src, dst, av_err2str(ret));
        return ret;
    }
    
    
     static void usage(const char *program_name)
    {
        fprintf(stderr, "usage: %s OPERATION entry1 [entry2]\n"
                "API example program to show how to manipulate resources "
                "accessed through AVIOContext.\n"
                "OPERATIONS:\n"
                "list      list content of the directory\n"
                "move      rename content in directory\n"
                "del       delete content in directory\n",
                program_name);
    }
    
     int main(int argc, char *argv[])
    {
        const char *op = NULL;
        int ret;
    
         av_log_set_level(AV_LOG_DEBUG);
    
         if (argc < 2) {
            usage(argv[0]);
            return 1;
        }
    
         op = argv[1];
        if (strcmp(op, "list") == 0) {
            if (argc < 3) {
                av_log(NULL, AV_LOG_INFO, "Missing argument for list operation.\n");
                ret = AVERROR(EINVAL);
            } else {
                ret = list_op(argv[2]);
            }
        } else if (strcmp(op, "del") == 0) {
            if (argc < 3) {
                av_log(NULL, AV_LOG_INFO, "Missing argument for del operation.\n");
                ret = AVERROR(EINVAL);
            } else {
                ret = del_op(argv[2]);
            }
        } else if (strcmp(op, "move") == 0) {
            if (argc < 4) {
                av_log(NULL, AV_LOG_INFO, "Missing argument for move operation.\n");
                ret = AVERROR(EINVAL);
            } else {
                ret = move_op(argv[2], argv[3]);
            }
        } else {
            av_log(NULL, AV_LOG_INFO, "Invalid operation %s\n", op);
            ret = AVERROR(EINVAL);
        }
    
         return ret < 0 ? 1 : 0;
    }
    

    通过示例获取文件夹中的文件列表信息的输出结果如下所示。


    avio_dir_cmd_output.PNG

    相关文章

      网友评论

          本文标题:FFmpeg 示例-avio_dir_cmd

          本文链接:https://www.haomeiwen.com/subject/ahrpyqtx.html