Ubuntu20.04, 源码编译安装ffmpeg-4.3和opencv- 4.5.3

需要用到opencv4,于是编译ffmpeg和opencv

ffmpeg下载及编译

# 准备
sudo apt install aptitude
sudo aptitude install build-essential
sudo aptitude install yasm nasm
# 下载依赖
sudo apt-get install -y libgmp3-dev pkg-config gnutls-bin libaom-dev libass-dev libbluray-dev libfdk-aac-dev libmp3lame-dev libopencore-amrnb-dev libopencore-amrwb-dev libopenmpt-dev libopus-dev libshine-dev libsnappy-dev libsoxr-dev libspeex-dev libtheora-dev libtwolame-dev libvo-amrwbenc-dev libvpx-dev libwavpack-dev libwebp-dev libx264-dev libx265-dev libxvidcore-dev liblzma-dev
# 安装至/opt目录
wget https://ffmpeg.org/releases/ffmpeg-4.3.tar.gz && tar zxf ffmpeg-4.3.tar.gz 
sudo ./configure --enable-version3 --enable-gpl --enable-nonfree --enable-small --enable-libmp3lame --enable-libx264 --enable-libx265 --enable-libvpx --enable-libtheora --enable-libvorbis --enable-libopus --enable-libfdk-aac --enable-libass --enable-libwebp --enable-librtmp --enable-postproc --enable-libfreetype --enable-pthreads --enable-openssl --enable-shared --disable-debug --disable-doc --disable-ffplay --extra-cflags="-I/opt/ffmpeg/include" --extra-ldflags="-L/opt/ffmpeg/lib" --extra-libs="-lpthread -lm" --prefix="/opt/ffmpeg" && sudo make -j8 && sudo make install && sudo make distclean

opencv4下载编译

# 准备
sudo apt-get update
sudo apt-get upgrade
sudo apt-get install build-essential cmake
# 下载依赖
sudo apt-get install libavcodec-dev libavformat-dev libswscale-dev libv4l-dev libxvidcore-dev libx264-dev libatlas-base-dev gfortran libgtk2.0-dev libjpeg-dev libpng-dev
sudo apt-get install python2.7-dev python3.7-dev

# 下载解压opencv4.5.3和opencv_contrib-4.5.3到同一个文件夹,我是/tmp文件夹,安装到/opt/opencv4
# 注意下列关于文件夹的路径需要更改
wget https://github.com/opencv/opencv/archive/4.5.3.zip --progress=dot:giga && \
    unzip 4.5.3.zip && \
    rm 4.5.3.zip && \
    wget https://github.com/opencv/opencv_contrib/archive/4.5.3.zip --progress=dot:giga && \
    unzip 4.5.3.zip && \
    rm 4.5.3.zip && \
    cd /tmp/opencv-4.5.3 && \
    mkdir -p build && cd build && \
	sudo cmake -D WITH_TBB=ON -D WITH_EIGEN=ON -D OPENCV_GENERATE_PKGCONFIG=ON  -D BUILD_DOCS=ON -D BUILD_TESTS=OFF -D BUILD_PERF_TESTS=OFF -D BUILD_EXAMPLES=OFF  -D WITH_OPENCL=OFF -D WITH_CUDA=OFF -D BUILD_opencv_gpu=OFF -D BUILD_opencv_gpuarithm=OFF -D BUILD_opencv_gpubgsegm=OFF -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/opt/opencv4-D OPENCV_EXTRA_MODULES_PATH=/tmp/opencv_contrib-4.5.3/modules/ .. && sudo make -j8 &&   sudo make install

二者一起配置环境变量

sudo vim ~/.bashrc
# 打开文件之后,在最后添加:
export PATH=/opt/ffmpeg/bin:/opt/opencv4/bin:$PATH
export LD_LIBRARY_PATH=/opt/ffmpeg/lib:/opt/opencv4/lib
export PKG_CONFIG_PATH=/opt/ffmpeg/lib/pkgconfig:/opt/opencv4/lib/pkgconfig
# 激活环境
source ~/.bashrc

# ldconfig
ldconfig && echo "/opt/ffmpeg/lib" >> /etc/ld.so.conf.d/ffmpeg.conf && \
    echo "/opt/opencv4/lib" >> /etc/ld.so.conf.d/opencv4.conf && \
    ldconfig

检验

# ffmpeg
fhy@ffine:/tmp$ ffmpeg -version
ffmpeg version 4.3 Copyright (c) 2000-2020 the FFmpeg developers
built with gcc 9 (Ubuntu 9.4.0-1ubuntu1~20.04.1)
configuration: --enable-version3 --enable-gpl --enable-nonfree --enable-small --enable-libmp3lame --enable-libx264 --enable-libx265 --enable-libvpx --enable-libtheora --enable-libvorbis --enable-libopus --enable-libfdk-aac --enable-libass --enable-libwebp --enable-librtmp --enable-postproc --enable-libfreetype --enable-pthreads --enable-openssl --enable-shared --disable-debug --disable-doc --disable-ffplay --extra-cflags=-I/opt/ffmpeg/include --extra-ldflags=-L/opt/ffmpeg/lib --extra-libs='-lpthread -lm' --prefix=/opt/ffmpeg
libavutil      56. 51.100 / 56. 51.100
libavcodec     58. 91.100 / 58. 91.100
libavformat    58. 45.100 / 58. 45.100
libavdevice    58. 10.100 / 58. 10.100
libavfilter     7. 85.100 /  7. 85.100
libswscale      5.  7.100 /  5.  7.100
libswresample   3.  7.100 /  3.  7.100
libpostproc    55.  7.100 / 55.  7.100

# opencv
fhy@ffine:/tmp$ pkg-config --modversion opencv4 || opencv_version
4.5.3
# opencv详细的链接信息
fhy@ffine:/tmp$ pkg-config --libs opencv4
-L/opt/opencv4/lib -lopencv_gapi -lopencv_stitching -lopencv_aruco -lopencv_barcode -lopencv_bgsegm -lopencv_bioinspired -lopencv_ccalib -lopencv_dnn_objdetect -lopencv_dnn_superres -lopencv_dpm -lopencv_face -lopencv_freetype -lopencv_fuzzy -lopencv_hdf -lopencv_hfs -lopencv_img_hash -lopencv_intensity_transform -lopencv_line_descriptor -lopencv_mcc -lopencv_quality -lopencv_rapid -lopencv_reg -lopencv_rgbd -lopencv_saliency -lopencv_stereo -lopencv_structured_light -lopencv_phase_unwrapping -lopencv_superres -lopencv_optflow -lopencv_surface_matching -lopencv_tracking -lopencv_highgui -lopencv_datasets -lopencv_text -lopencv_plot -lopencv_videostab -lopencv_videoio -lopencv_xfeatures2d -lopencv_shape -lopencv_ml -lopencv_ximgproc -lopencv_video -lopencv_dnn -lopencv_xobjdetect -lopencv_objdetect -lopencv_calib3d -lopencv_imgcodecs -lopencv_features2d -lopencv_flann -lopencv_xphoto -lopencv_photo -lopencv_imgproc -lopencv_core

你可能感兴趣的:(way,linux,运维,opencv)