main.c
#include <stdio.h>
#define __STDC_CONSTANT_MACROS
#ifdef __cplusplus
extern "C" {
#endif
#include <libavcodec/avcodec.h>
#include <libavformat/avformat.h>
#include <libavfilter/avfilter.h>
#include <libavutil/log.h>
#ifdef __cplusplus
};
// C++中使用av_err2str宏
char av_error[AV_ERROR_MAX_STRING_SIZE] = { 0 };
#define av_err2str(errnum) \
av_make_error_string(av_error, AV_ERROR_MAX_STRING_SIZE, errnum)
#endif
int main(int argc,char *argv[])
{
/*
//日志函数
//**********************************************
av_log_set_level(AV_LOG_DEBUG);//
av_log(NULL,AV_LOG_INFO,"...%s\n","hello");
//printf("aaaaaa\n");
//**********************************************
//文件删除与重命名
int ret ;
ret=avpriv_io_move("/home/ky/ffmpeg_demo/111.txt","/home/ky/ffmpeg_demo/222.txt");//重命名制定路径的文件
if(ret<0)
{
av_log(NULL,AV_LOG_ERROR,"Failed to move file");
return -1;
}
else
{
av_log(NULL,AV_LOG_INFO,"success to move file");
}
/* ret=avpriv_io_delete("/home/ky/ffmpeg_demo/1.txt");//删除制定路径的文件
if(ret<0)
{
av_log(NULL,AV_LOG_ERROR,"Failed to delete file");
return -1;
}
*/
//操作目录 实现ls命令的功能
//**********************************************
int ret;
AVIODirContext *ctx=NULL;//操作目录上下文,相当于文件的flag 链接多个APID的桥梁
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't open dir:%s\n",av_err2str(ret));
goto _fail;
}
while(1)
{
ret=avio_read_dir(ctx,&entry);ma
if(ret<0) {
av_log(NULL, AV_LOG_ERROR, "can't read dir:%s\n",av_err2str(ret));
goto _fail;
}
if(!entry)
{
break;
}
av_log(NULL,AV_LOG_INFO,"%12" PRId64" %s \n",entry->size,entry->name,entry->group_id);
avio_free_directory_entry(&entry);
}
_fail:
avio_close_dir(&ctx);
return 0;
}
cmakelist.txt
cmake_minimum_required(VERSION 3.17)
project(ffmpeg_demo)
# 设置ffmpeg依赖库及头文件所在目录,并存进指定变量
set(ffmpeg_libs_DIR /usr/lib/x86_64-linux-gnu)
set(ffmpeg_headers_DIR /usr/include/x86_64-linux-gnu)
#对于find_package找不到的外部依赖库,可以用add_library添加
# SHARED表示添加的是动态库
# IMPORTED表示是引入已经存在的动态库
add_library( avcodec SHARED IMPORTED)
add_library( avfilter SHARED IMPORTED )
add_library( swresample SHARED IMPORTED )
add_library( swscale SHARED IMPORTED )
add_library( avformat SHARED IMPORTED )
add_library( avutil SHARED IMPORTED )
#指定所添加依赖库的导入路径
set_target_properties( avcodec PROPERTIES IMPORTED_LOCATION ${ffmpeg_libs_DIR}/libavcodec.so )
set_target_properties( avfilter PROPERTIES IMPORTED_LOCATION ${ffmpeg_libs_DIR}/libavfilter.so )
set_target_properties( swresample PROPERTIES IMPORTED_LOCATION ${ffmpeg_libs_DIR}/libswresample.so )
set_target_properties( swscale PROPERTIES IMPORTED_LOCATION ${ffmpeg_libs_DIR}/libswscale.so )
set_target_properties( avformat PROPERTIES IMPORTED_LOCATION ${ffmpeg_libs_DIR}/libavformat.so )
set_target_properties( avutil PROPERTIES IMPORTED_LOCATION ${ffmpeg_libs_DIR}/libavutil.so )
# 添加头文件路径到编译器的头文件搜索路径下,多个路径以空格分隔
include_directories( ${ffmpeg_headers_DIR} )
link_directories(${ffmpeg_libs_DIR} )
set(CMAKE_CXX_STANDARD 14)
add_executable(ffmpeg_demo main.cpp)
target_link_libraries(${PROJECT_NAME} avcodec avformat avutil swresample swscale swscale avfilter )
直接g++编译
g++ -o main main.cpp -l avformat -l avutil
参考链接
https://ffmpeg.org/doxygen/3.4/avio_8h.html#a7bb596b33ba395e488af6c0c21ca00d5
https://www.cnblogs.com/fandx/p/12123085.html
https://www.jianshu.com/p/719c6b50070e