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的相关代码如下:

void ffmpegDir() {
    int ret;
    av_log_set_level(AV_LOG_INFO);
    // 上下文
    AVIODirContext *ctx = NULL;
    AVIODirEntry *entry = NULL;
    // 打开目录
    ret = avio_open_dir(&ctx, "./", NULL);
    if (ret < 0) {
        av_log(NULL, AV_LOG_ERROR, "cant open dir");
        return;
    }
    else {
        av_log(NULL, AV_LOG_INFO, "open success");
    }

    while (1) {
        // 读取目录
        ret = avio_read_dir(ctx, &entry);
        if (ret < 0) {
            av_log(NULL, AV_LOG_ERROR, "cant read dir");
            // 防止内存泄漏
            goto __fail;
        }
        else {
            av_log(NULL, AV_LOG_INFO, "read dir success");
        }
        // 末尾
        if (!entry) {
            break;
        }
        printf("entry->name = %s", entry->name);
        // 释放资源
        avio_free_directory_entry(&entry);
    }
    // 释放资源
__fail:
    avio_close_dir(&ctx);
}

猜你喜欢

转载自www.cnblogs.com/renhui/p/10391204.html