FFmpeg开发实战(二):FFmpeg 文件操作

FFmpeg 提供了丰富的API供我们使用,下面我们来讲述一下文件操作相关的API:

  • FFmpeg 删除文件:avpriv_io_delete()
  • FFmpeg 重命名文件:avpriv_io_move()
  • FFmpeg 打开目录:avio_open_dir()
  • FFmpeg 读取目录:avio_read_dir();
  • FFmpeg 关闭目录:avio_close_dir()

使用FFmpeg文件操作API实现删除和重命名的实例代码如下:

// FFmpeg 删除文件操作
void ffmpegDelFile() {
    int ret;
    ret = avpriv_io_delete("1.txt");  // 在项目目录下创建的文件(测试时需要创建好)
    printf("Del File Code : %d \n", ret);
    if (ret < 0) {
        av_log(NULL, AV_LOG_ERROR, "Failed to delete file \n");
    } else {
        av_log(NULL, AV_LOG_INFO, "Delete File Success!\n ");
    }
}

// FFmpeg 重命名或移动文件
void ffmpegMoveFile(char* src, char* dst) {
    int ret;
    ret = avpriv_io_move(src, dst);
    printf("Move File Code : %d \n", ret);
    // 重命名时,如果文件不存在,ret也会0
    if (ret < 0) {
        av_log(NULL, AV_LOG_ERROR, "Failed to Move File %s!\n ", src);
    } else {
        av_log(NULL, AV_LOG_INFO, "Success Move File %s!\n", src);
    }
}

使用FFmpeg文件操作API实现读取文件目录及输出文件目录List的相关代码如下:

// FFmpeg 目录操作
void ffmpegDir() {

    int ret;
    
    // 上下文
    AVIODirContext *dirCtx = NULL;
    AVIODirEntry *dirEntry = NULL;
    
    // 注意Windows下会返回-40,也就是Function not implement,方法未实现,也就是说windows下不支持此方法
    ret = avio_open_dir(&dirCtx, "include", NULL);  

    if (ret < 0) {
        // 输出错误日志
        printf("cant open dir,msg = %s", av_err2str(ret));
        return;
    }

    av_log(NULL, AV_LOG_INFO, "Open Dir Success!");

    while (1){
        ret = avio_read_dir(dirCtx, &dirEntry);
        if (ret < 0) {
            printf("cant read dir : %s", av_err2str(ret));
            // 防止内存泄漏
            goto __failed;
        }
        av_log(NULL, AV_LOG_INFO, "read dir success");
        if (!dirEntry) {
            break;
        }
        printf("Entry Name = %s", dirEntry->name);
        // 释放资源
        avio_free_directory_entry(&dirEntry);
    }
// 释放资源
__failed:
    avio_close_dir(&dirCtx);
}

 

posted @ 2019-02-17 18:13  灰色飘零  阅读(2683)  评论(3编辑  收藏  举报