目录


下面看一个简单的 demo。
- #include
- #include
-
- 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
来看一个 demo
- #include
- #include
-
- 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
实战:实现一个简单的 ls 命令
- #include
- #include
-
- 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