记录一次ffmpeg编译
网上搜的linux编译都是xxxx,懒得说了
不知道为什么ffmpeg-4.2.2
下我使用最大量编译都无法编译出include、lib
等文件夹
下面是一键添加滤镜的脚本(linux可用,手动打开注释)
#!/bin/bash
ff_version="ffmpeg-4.2.2"
ff="${ff_version}.tar.bz2"
main(){
cd ~/
if [ ! -d $ff_version ];then
if [ ! -f "$ff" ];then wget https://ffmpeg.org/releases/$ff;fi
tar -xjvf $ff
fi
cd $ff_version
if [ -f "Changelog" ];then
rm Changelog *.md COPYING.* CREDITS MAINTAINERS RELEASE* VERSION
fi
#add_filter_demo //Linux no problem , Mac can't
echo "IyEvYmluL2Jhc2gKCmlmIFsgISAtZCBidWlsZCBdO3RoZW4KICAgIG1rZGlyIGJ1aWxkCmZpCi4vY29uZmlndXJlIFwKICAgIC0tcHJlZml4PSQocHdkKS9idWlsZCBcCiAgICAtLWVuYWJsZS1zaGFyZWQgXAogICAgLS1kaXNhYmxlLWFzbSBcCiAgICAtLWRpc2FibGUtZG9jIFwKICAgIC0tZGlzYWJsZS1uZXR3b3JrIFwKICAgIC0tZGlzYWJsZS1mZnByb2JlIFwKICAgIC0tZGlzYWJsZS1mZnBsYXkgXAogICAgLS1kaXNhYmxlLWF2ZGV2aWNlIFwKCm1ha2UgLWogMTYgJiYgbWFrZSBpbnN0YWxsCgpraW5kPSIqLnNvIgpjaGVja19vcygpewogICAgY2FzZSAiJCh1bmFtZSkiIGluCiAgICAiRGFyd2luIikga2luZD0iKi5keWxpYiIgOzsKICAgIGVzYWMKfQoKY2hlY2tfb3MKCm1rZGlyIGxpYgpjcCAkKGZpbmQgLiAtbmFtZSAiJHtraW5kfSIpIC4vbGliLwo=" | base64 -d > load.sh && chmod +x load.sh
cat load.sh
echo -e "\033[31mAre you ok?[Y/n]\033[0m"
read ok
if [ "$ok" == "n" ];then return;fi
./load.sh
}
add_filter_demo(){
echo "I2luY2x1ZGUgImxpYmF2dXRpbC9pbnRlcm5hbC5oIgojaW5jbHVkZSAibGliY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cHJvcHMgPSBjb25maWdfaW5wdXQsCgl9LAoJeyBOVUxMIH0KfTsKIApzdGF0aWMgY29uc3QgQVZGaWx0ZXJQYWQgYXZmaWx0ZXJfdmZfdmxmbGlwX291dHB1dHNbXSA9IHsKCXsKCQkubmFtZSA9ICJkZWZhdWx0IiwKCQkudHlwZSA9IEFWTUVESUFfVFlQRV9WSURFTywKCX0sCgl7IE5VTEwgfQp9OwpBVkZpbHRlciBmZl92Zl92bGZsaXAgPSB7CgkubmFtZSA9ICJ2bGZsaXAiLAoJLmRlc2NyaXB0aW9uID0gTlVMTF9JRl9DT05GSUdfU01BTEwoImxpcGVpcmFuOnZlcnNpb246LT5GbGlwIHRoZSBpbnB1dCB2aWRlbyB2ZXJ0aWNhbGx5LiIpLAoJLnByaXZfc2l6ZSA9IHNpemVvZihGbGlwQ29udGV4dCksCgkucHJpdl9jbGFzcyA9ICZ2bGZsaXBfY2xhc3MsCgkuaW5wdXRzID0gYXZmaWx0ZXJfdmZfdmxmbGlwX2lucHV0cywKCS5vdXRwdXRzID0gYXZmaWx0ZXJfdmZfdmxmbGlwX291dHB1dHMsCgkuZmxhZ3MgPSBBVkZJTFRFUl9GTEFHX1NVUFBPUlRfVElNRUxJTkVfR0VORVJJQywKfTsK" | base64 -d > libavfilter/vf_vlflip.c
sed -i '/ff_vf_zscale/a\extern AVFilter ff_vf_vlflip;' libavfilter/allfilters.c
sed -i '/vf_zscale.o/a\OBJS-$(CONFIG_VLFLIP_FILTER) += vf_vlflip.o' libavfilter/Makefile
}
main
执行configurion+make后的发现ffmpeg报错找不到so
之前没有执行make install …
~/ffmpeg-4.2.2 $ ./ffmpeg
./ffmpeg: error while loading shared libraries: libavdevice.so.58: cannot open shared object file: No such file or directory
先不管它,遇到问题一个个解决,找到了根源就好办了
在ffmpeg-4.2.2
目录下用find . -name "*.so"
找了一下发现对于的so都在对应的模块里面
~/ffmpeg-4.2.2 $ find . -name "*.so"
./libavdevice/libavdevice.so
./libswscale/libswscale.so
./libavformat/libavformat.so
./libswresample/libswresample.so
./libavfilter/libavfilter.so
./libavutil/libavutil.so
./libavcodec/libavcodec.so
编写调用ffmpeg库api的源文件:
https://github.com/lokenetwork/FFmpeg-Principle/blob/main/input/main.c
#include <stdio.h>
#include "libavformat/avformat.h"
int main()
{
AVFormatContext *fmt_ctx = NULL;
int type = 2;
int err;
// 提示,要把 juren-30s.mp4 文件放到 Debug 目录下才能找到。
char filename[] = "juren-30s.mp4";
fmt_ctx = avformat_alloc_context();
if (!fmt_ctx) {
printf("error code %d \n",AVERROR(ENOMEM));
return 1;
}
if(1==type){
if((err = avformat_open_input(&fmt_ctx, filename,NULL,NULL)) < 0){
printf("error code %d \n",err);
}else{
printf("open success \n");
printf("filename - %s \n",fmt_ctx->filename);
printf("duration - %I64d \n",fmt_ctx->duration);
printf("nb_streams - %u \n",fmt_ctx->nb_streams);
for( int i=0; i < fmt_ctx->nb_streams; i++ ){
printf("stream codec_type - %d \n",fmt_ctx->streams[i]->codecpar->codec_type);
}
printf("iformat name - %s \n",fmt_ctx->iformat->name);
printf("iformat long name - %s \n",fmt_ctx->iformat->long_name);
}
}
if(2==type){
//设置探测大小
AVDictionary *format_opts = NULL;
av_dict_set(&format_opts, "probesize", "32", 0);
if((err = avformat_open_input(&fmt_ctx, filename,NULL,&format_opts)) < 0){
printf("error code %d \n",err);
}else{
avformat_find_stream_info(fmt_ctx,NULL);
printf("open success \n");
printf("filename - %s \n",fmt_ctx->filename);
printf("duration - %I64d \n",fmt_ctx->duration);
printf("nb_streams - %u \n",fmt_ctx->nb_streams);
for( int i=0; i < fmt_ctx->nb_streams; i++ ){
printf("stream codec_type - %d \n",fmt_ctx->streams[i]->codecpar->codec_type);
}
printf("iformat name - %s \n",fmt_ctx->iformat->name);
printf("iformat long name - %s \n",fmt_ctx->iformat->long_name);
}
av_dict_free(&format_opts);
}
return 0;
}
手动指定路径编译
gcc main.c -w -I ffmpeg-4.2.2 -L./ffmpeg-4.2.2/libavformat -lavformat -L./ffmpeg-4.2.2/libavutil -lavutil
(编译是可以编译的,但是还不能运行)
~ $ ./a.out
./a.out: error while loading shared libraries: libavformat.so.58: cannot open shared object file: No such file or directory
然后手动添加动态库路径到变量中
~ $ export LD_LIBRARY_PATH=~/ffmpeg-4.2.2/libavformat:~/ffmpeg-4.2.2/libavcodec:~/ffmpeg-4.2.2/libavutil:~/ffmpeg-4.2.2/libswresample
ok,可以运行了。经过前面的分析最终的暂时提出下面解答方法
我暂时的解决方法是将全部so文件手动转移到一个目录下来(注意改路径)
cd ffmpeg-4.2.2
mkdir lib
cp $(find . -name "*.so*") ./lib/
然后编译
export LD_LIBRARY_PATH=~/ffmpeg-4.2.2/lib #Mac DYLD_LIBRARY_PATH
gcc main.c -w -I ffmpeg-4.2.2 -L./ffmpeg-4.2.2/lib -lavformat -lavutil
然后就可以正常执行了
新增Mac
按照上面的脚本编译后即可正常运行(之前是因为没有执行make install)
clang main.c -I ffmpeg-4.2.2/build/include/ -L ffmpeg-4.2.2/build/lib/ -lavformat
./a.out
转载:https://blog.csdn.net/csdn546229768/article/details/128728748
查看评论