目录
FFmpeg日志系统
FFmpeg文件与目录操作
FFmpeg文件的删除与重命名
FFmpeg操作目录及list的实现
操作目录重要函数
操作目录重要结构体
FFmpeg日志系统
下面看一个简单的 demo。
#include <stdio.h>
#include <libavutil/log.h>
int main(int argc,char* argv[])
{
av_log_set_level(AV_LOG_DEBUG);
av_log(NULL,AV_LOG_INFO,"Hello World!,%s\n","aaa");
return 0;
}
编译运行,结果如下:
wj@ubuntu:~/FFmpeg$ gcc -g -o ffmpeg_log ffmpeg_log.c -lavutil
wj@ubuntu:~/FFmpeg$ ./ffmpeg_log
Hello World!,aaa
FFmpeg文件与目录操作
FFmpeg文件的删除与重命名
- 文件删除函数:avpriv_io_delete()
- 文件重命名:avpriv_io_move()
来看一个 demo
#include <stdio.h>
#include <libavformat/avformat.h>
int main(int argc,char* argv[])
{
int ret = 0;
ret = avpriv_io_move("111.txt","222.txt");
if(ret < 0)
{
av_log(NULL,AV_LOG_ERROR,"Failed to rename\n");
}
av_log(NULL,AV_LOG_INFO,"Success to rename\n");
//delete url
ret = avpriv_io_delete("./mytestfile.txt");
if(ret<0)
{
av_log(NULL,AV_LOG_ERROR,"Failed to delete file mytestfile.txt\n");
return -1;
}
av_log(NULL,"Success to delete mytestfile.txt");
return 0;
}
sudo apt-get install libavformat-dev
wj@ubuntu:~/FFmpeg$ gcc -g -o ffmpeg_del ffmpeg_file.c -lavformat -lavutil
ffmpeg_file.c: In function ‘main’:
ffmpeg_file.c:8:11: warning: implicit declaration of function ‘avpriv_io_move’ [-Wimplicit-function-declaration]
8 | ret = avpriv_io_move("111.txt","222.txt");
| ^~~~~~~~~~~~~~
ffmpeg_file.c:18:11: warning: implicit declaration of function ‘avpriv_io_delete’ [-Wimplicit-function-declaration]
18 | ret = avpriv_io_delete("./mytestfile.txt");
| ^~~~~~~~~~~~~~~~
wj@ubuntu:~/FFmpeg$ ./ffmpeg_del
Success to rename
Failed to delete file mytestfile.txt
FFmpeg操作目录及list的实现
操作目录重要函数
- avio_open_dir()
- avio_read_dir()
- avio_close_dir()
操作目录重要结构体
- AVIODirContext 操作目录的上下文
- AVIODirEntry 目录项。用于存放文件名,文件大小等信息。
实战:实现一个简单的 ls 命令
#include <stdio.h>
#include <libavformat/avformat.h>
int main(int argc,char* argv[])
{
int ret = 0;
AVIODirContext* ctx = NULL;
AVIODirEntry* entry=NULL;
av_log_set_level(AV_LOG_INFO);
ret = avio_open_dir(&ctx,"./",NULL);
if(ret < 0)
{
av_log(NULL,AV_LOG_ERROR,"Can not open dir:%s\n",av_err2str(ret));
return -1;
}
while(1)
{
ret = avio_read_dir(ctx,&entry);
if(ret < 0)
{
av_log(NULL,AV_LOG_ERROR,"can not dir:%s\n",av_err2str(ret));
//return -1;
goto __fail; //avoid memleak
}
if(!entry)
{
break;
}
av_log(NULL,AV_LOG_INFO,"%12"PRId64" %s \n",entry->size,entry->name);
avio_free_directory_entry(&entry);
}
__fail:
avio_close_dir(&ctx);
return 0;
}
编译运行,如下所示:
wj@ubuntu:~/FFmpeg$ gcc -g -o ffmpeg_ls ffmpeg_ls.c -lavformat -lavutil
wj@ubuntu:~/FFmpeg$ ./ffmpeg_ls
190 ffmpeg_log.c
127296 ffmpeg_log
0 222.txt
579 ffmpeg_file.c
4096 .vscode
848 ffmpeg_ls.c
20800 ffmpeg_ls
19776 ffmpeg_del