02 编写C语言,调用ffmpeg
假设定位到目录
/home/jiangqianghua/Desktop/ffmpeg
里面builded保存ffmpeg编译好的文件夹
里面ctest是编写c测试文件夹
1 配置环境变量
>>vim /etc/profile
末尾添加
LD_LIBRARY_PATH=/home/jiangqianghua/Desktop/ffmpeg/ffmpeg-2.8.13/builded/lib
export LD_LIBRARY_PATH
>> source /etc/profile
或者
>>export LD_LIBRARY_PATH=LD_LIBRARY_PATH=/home/jiangqianghua/Desktop/ffmpeg/ffmpeg-2.8.13/builded/lib/
测试
>> ldd ./ffmpeg
2 编写C语言代码
ffmpegtest.c
#define __STDC_CONSTANT_MACROS
#ifdef __cplusplus
extern "C"
{
#endif
#include "libavcodec/avcodec.h"
#include "libavformat/avformat.h"
#include "libavfilter/avfilter.h"
#ifdef __cplusplus
};
#endif
/**
* AVFormat Support Information
*/
char * avformatinfo(){
char *info=(char *)malloc(40000);
memset(info,0,40000);
av_register_all();
AVInputFormat *if_temp = av_iformat_next(NULL);
AVOutputFormat *of_temp = av_oformat_next(NULL);
//Input
while(if_temp!=NULL){
sprintf(info, "%s[In ] %10s\n", info, if_temp->name);
if_temp=if_temp->next;
}
//Output
while (of_temp != NULL){
sprintf(info, "%s[Out] %10s\n", info, of_temp->name);
of_temp = of_temp->next;
}
return info;
}
/**
* AVCodec Support Information
*/
char * avcodecinfo()
{
char *info=(char *)malloc(40000);
memset(info,0,40000);
av_register_all();
AVCodec *c_temp = av_codec_next(NULL);
while(c_temp!=NULL){
if (c_temp->decode!=NULL){
sprintf(info, "%s[Dec]", info);
}
else{
sprintf(info, "%s[Enc]", info);
}
switch (c_temp->type){
case AVMEDIA_TYPE_VIDEO:
sprintf(info, "%s[Video]", info);
break;
case AVMEDIA_TYPE_AUDIO:
sprintf(info, "%s[Audio]", info);
break;
default:
sprintf(info, "%s[Other]", info);
break;
}
sprintf(info, "%s %10s\n", info, c_temp->name);
c_temp=c_temp->next;
}
return info;
}
/**
* AVFilter Support Information
*/
char * avfilterinfo()
{
char *info=(char *)malloc(40000);
memset(info,0,40000);
avfilter_register_all();
AVFilter *f_temp = (AVFilter *)avfilter_next(NULL);
while (f_temp != NULL){
sprintf(info, "%s[%15s]\n", info, f_temp->name);
f_temp=f_temp->next;
}
return info;
}
/**
* Configuration Information
*/
char * configurationinfo()
{
char *info=(char *)malloc(40000);
memset(info,0,40000);
av_register_all();
sprintf(info, "%s\n", avcodec_configuration());
return info;
}
int main(int argc, char* argv[])
{
char *infostr=NULL;
infostr=configurationinfo();
printf("\n<>\n%s",infostr);
free(infostr);
infostr=avformatinfo();
printf("\n<>\n%s",infostr);
free(infostr);
infostr=avcodecinfo();
printf("\n<>\n%s",infostr);
free(infostr);
infostr=avfilterinfo();
printf("\n<>\n%s",infostr);
free(infostr);
return 0;
}
执行命令
>>sudo gcc -I ../builded/include/ ffmpegtest.c -o ffmpegtest -L ../builded/lib/ -lavcodec -lavdevice -lavfilter -lavformat -lavutil -lswresample -lswscale
运行
>>sudo ./ffmpegtest
注意事项运行会出现so找不到,提示
ffmpeg: error while loading shared libraries: libavfilter.so.1: cannot open shared object file: No such file or directory
解决办法
编辑/etc/ld.so.conf文件加入如下内容
保存退出后执行ldconfig命令
/home/jiangqianghua/Desktop/ffmpeg/ffmpeg-2.8.13/builded/lib
参考地址