0.前言
在网上看了很多aarch64编译的opencv带ffmpeg的教程,但大多都写的不明不白,而且难以复现(试了几十次)。终于,在参考了两篇blog之后知道了怎么从零复现。首先说结论:大多ffmpeg=NO的原因是没有配置pkg-config变量。
参考:opencv+ffmpeg交叉编译_ubuntu opencv ffmpeg交叉编译-CSDN博客
参考:RK1808交叉编译opencv with ffmpeg(解决FFMPEG始终为NO)
1.系统和文件准备
ubuntu18.04
opencv-3.4.16
ffmpeg-4.2.2
gcc-linaro-6.3.1-2017.05-x86_64_aarch64-linux-gnu
x264-master
xvidcore-1.3.7
zlib-1.2.11.tar
1.1 全新的ubuntu18.04(安装略)
1.2 将所有材料放在一起并解压(建议)
1.3 创建arm64文件夹(用来放编译好的文件)
cd /home/hmz
mkdir arm64
1.4 安装必要插件
sudo apt install make
sudo apt install cmake
sudo apt install cmake-gui
sudo apt install pkg-config
2.开始编译
2.1 先配置aarch64
sudo gedit /etc/profile
在最后面添加
export PATH=$PATH:/home/hmz/work/gcc-linaro-6.3.1-2017.05-x86_64_aarch64-linux-gnu/bin
保存,关闭gedit,使环境变量生效
source /etc/profile
打开命令行,输入
aarch64-linux-gnu-gcc -v
2.2 编译zlib
进入zlib-1.2.11,打开命令行输入:
./configure --prefix=/home/hmz/arm64
gedit Makefile
修改如下地方:
CC=aarch64-linux-gnu-gcc
LDSHARED=aarch64-linux-gnu-gcc -shared -Wl,-soname,libz.so.1,--version-script,zlib.map
AR=aarch64-linux-gnu-ar
RANLIB=aarch64-linux-gnu-ranlib
make
sudo make install
2.3 编译x264
进入x264-master,打开命令行输入:
CC=aarch64-linux-gnu-gcc ./configure --enable-shared --host=aarch64-linux-gnu --disable-asm --prefix=/home/hmz/arm64
make
sudo make install
2.4 编译xvidcore
进入xvidcore-1.3.7/xvidcore/build/generic,打开命令行输入:
./configure --prefix=/home/hmz/arm64 --host=aarch64-linux-gnu --disable-assembly
make
sudo make install
2.5 编译ffmpeg
进入ffmpeg-4.2.2,打开命令行输入:
./configure --prefix=/home/hmz/arm64 --enable-shared --disable-static --enable-gpl --enable-cross-compile --arch=aarch64 --disable-stripping --target-os=linux --enable-libx264 --enable-libxvid --cc=aarch64-linux-gnu-gcc --enable-swscale --extra-ldflags=-L/home/hmz/arm64/lib --extra-cflags=-I/home/hmz/arm64/include --pkg-config=PKGCONFIG --ar=aarch64-linux-gnu-ar --ranlib=aarch64-linux-gnu-ranlib
有两条警告,但是没关系
WARNING: PKGCONFIG not found, library detection may fail.
WARNING: using libx264 without pkg-config
继续
make
sudo make install
2.6 编译opencv
进入opencv-3.4.16,打开命令行输入:
mkdir build
cd build
gedit toolchain.cmake
输入以下内容:
set( CMAKE_SYSTEM_NAME Linux )
set( CMAKE_SYSTEM_PROCESSOR arm64 )
set( CMAKE_C_COMPILER aarch64-linux-gnu-gcc )
set( CMAKE_CXX_COMPILER aarch64-linux-gnu-g++ )
set( CMAKE_FIND_ROOT_PATH "/home/hmz/arm64" ) #这里要和上面编译安装的目录对应
set( CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER )
set( CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY )
set( CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY )
set(CMAKE_C_FLAGS -Wl,-rpath-link=/home/hmz/arm64/lib)
set(CMAKE_CXX_FLAGS -Wl,-rpath-link=/home/hmz/arm64/lib)
保存后关闭,命令行输入:
cmake -DCMAKE_TOOLCHAIN_FILE=toolchain.cmake ../
这时候ffmpeg是NO的
继续输入:
sudo gedit /etc/profile
在最后面加上:
export PKG_CONFIG_PATH=$PKG_CONFIG_PATH:/home/hmz/arm64/lib/pkgconfig
输入:
source /etc/profile
打开cmake-gui,修改地址
点击Add_Entry,增加项:OPENCV_ENABLE_PKG_CONFIG 并打勾
点击OK,关闭窗口,再点击Configure,可以看到ffmpeg变为YES
搜索:CMAKE_INSTALL_PREFIX,设置Value(安装目录):/home/hmz/arm64
再点击一次Configure,然后点击Generate,关闭窗口,命令行输入
make
sudo make install
至此编译完成,安装好的库在目录:/home/hmz/arm64