ffmpeg为何用c语言编译,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

参考地址

以下是一个使用C语言调用FFmpeg的简单实例: ```c #include <stdio.h> #include <libavcodec/avcodec.h> #include <libavformat/avformat.h> #include <libavutil/imgutils.h> #include <libswscale/swscale.h> int main() { // 注册所有的FFmpeg组件 av_register_all(); // 打开输入文件 AVFormatContext *formatContext = NULL; if (avformat_open_input(&formatContext, "input.mp4", NULL, NULL) != 0) { printf("无法打开输入文件\n"); return -1; } // 获取流信息 if (avformat_find_stream_info(formatContext, NULL) < 0) { printf("无法获取流信息\n"); return -1; } // 寻找视频流 int videoStreamIndex = -1; for (int i = 0; i < formatContext->nb_streams; i++) { if (formatContext->streams[i]->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) { videoStreamIndex = i; break; } } if (videoStreamIndex == -1) { printf("无法找到视频流\n"); return -1; } // 获取视频解码器 AVCodecParameters *codecParameters = formatContext->streams[videoStreamIndex]->codecpar; AVCodec *codec = avcodec_find_decoder(codecParameters->codec_id); if (codec == NULL) { printf("无法找到解码器\n"); return -1; } // 创建解码器上下文 AVCodecContext *codecContext = avcodec_alloc_context3(codec); if (avcodec_parameters_to_context(codecContext, codecParameters) < 0) { printf("无法创建解码器上下文\n"); return -1; } // 打开解码器 if (avcodec_open2(codecContext, codec, NULL) < 0) { printf("无法打开解码器\n"); return -1; } // 读取视频帧 AVPacket packet; while (av_read_frame(formatContext, &packet) >= 0) { if (packet.stream_index == videoStreamIndex) { // 解码视频帧 AVFrame *frame = av_frame_alloc(); int response = avcodec_send_packet(codecContext, &packet); if (response < 0) { printf("解码出错\n"); break; } response = avcodec_receive_frame(codecContext, frame); if (response == AVERROR(EAGAIN) || response == AVERROR_EOF) { av_frame_free(&frame); continue; } else if (response < 0) { printf("解码出错\n"); break; } // 处理视频帧 printf("解码视频帧,宽度:%d,高度:%d\n", frame->width, frame->height); av_frame_free(&frame); } av_packet_unref(&packet); } // 清理资源 avcodec_free_context(&codecContext); avformat_close_input(&formatContext); return 0; } ``` 在这个示例中,我们使用了FFmpeg库来打开输入文件("input.mp4"),获取视频流信息,寻找视频流,并将视频帧进行解码和处理。你可以根据你的需求进行进一步的操作,比如对解码后的视频帧进行渲染或保存等。 请确保已经正确安装了FFmpeg库,并在编译时链接了相关的库文件(如libavcodec、libavformat等)。你可以使用如下命令进行编译: ```shell gcc -o ffmpeg_example ffmpeg_example.c -lavcodec -lavformat -lavutil -lswscale ``` 编译完成后,你可以运行生成的可执行文件来执行示例程序。请将输入文件("input.mp4")放置在可执行文件所在的目录中。 希望这个示例能帮助你开始使用C语言调用FFmpeg进行视频处理。如有需要,你可以根据实际情况进行进一步的定制和扩展。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值