为了加深大家的印象,每篇FFmpeg初级开发的文章开头会出现FFmpeg的代码结构和初级开发我们的学习大纲,
FFmpeg的代码结构:
libavcodec: 编码器的实现
libavformat: 流协议,容器格式及基本IO访问的实现
libavfilter:音视频过滤器
libavutil:hash器,解码器和各种工具函数
libavdevice:访问捕获设备和回访设备的借口
libswresample:实现混音和重采样
libswscale:实现色彩转换和缩放功能
FFmpeg初级开发包括:
1. FFmpeg日志Log;
2. FFmpeg文件操作;
3. FFmpeg目录操作;
4. FFmpeg Meta信息;
5. FFmpeg抽取音频数据;
6. FFmpeg抽取视频H264数据;
7. FFmpeg格式转换;
8. FFmpeg音视频裁剪。
上一篇文章我们介绍了FFmpeg下文件的删除,重命名和移动。那么今天我们来介绍如何能打印出来文件目录,并且做一个仿ls命令的小程序
今天重点用到的有3个函数和2个结构体,他们分别是:
int avio_open_dir ( AVIODirContext ** s,
const char * url,
AVDictionary ** options
)
int avio_read_dir ( AVIODirContext * s,
AVIODirEntry ** next
)
int avio_close_dir ( AVIODirContext ** s )
AVIODirContext:通过这个上下文结构体,来告诉上面三个方法使用的是哪个目录
AVIODirEntry:通过这个结构体来获取目录中的所有项目。
下面是模仿ls命令的小程序:
#include<stdio.h>
#include<libavutil/log.h>
#include<libavformat/avformat.h>
int main(int argc, char* argv[]){
int result;
AVIODirContext *cxt = NULL;
AVIODirEntry *entry = NULL;
av_log_set_level(AV_LOG_INFO);
// 判断打开是否成功
result = avio_open_dir(&cxt, "./", NULL);
if(result < 0){
av_log(NULL, AV_LOG_ERROR, "Cant open dir: %s\n", av_err2str(result));
return -1;
}
while(1){
result = avio_read_dir(cxt, &entry);
// 判断读取是否成功
if(result < 0){
av_log(NULL, AV_LOG_ERROR, "Cant read dir: %s\n", av_err2str(result));
goto __fail;
}
if(!entry){
break;
}
// 打印id,size,type,modification_timestamp和name信息
av_log(NULL, AV_LOG_INFO, "id: %12"PRId64", size:%12"PRId64", type: %d, modification_timestamp: %lld, name: %s\n", entry->user_id, entry->size, entry->type, entry->modification_timestamp, entry->name);
avio_free_directory_entry(&entry);
}
__fail:
avio_close_dir(&cxt);
return 0;
}
编译的时候注意要链接libavformat和libutil库