### OpenCV安裝(Linux)

### OpenCV安裝(Linux)

@(gr_self)[ffmpeg | openCV]c++

#@author:       gr
#@date:         2015-09-02
#@email:        forgerui@gmail.com

1.安裝基礎依賴包

yum install autoconf automake gcc gcc-c++ git libtool make nasm pkgconfig zlib-devel

2. 安裝其它依賴

Yasm

Yasm is an assembler used by x264 and FFmpeg.git

mkdir ~/ffmpeg_sources
cd ~/ffmpeg_sources  
curl -O http://www.tortall.net/projects/yasm/releases/yasm-1.2.0.tar.gz  
tar xzvf yasm-1.2.0.tar.gz  
cd yasm-1.2.0  
./configure --prefix="$HOME/ffmpeg_build" --bindir="$HOME/bin"  
make  
make install  
make distclean  
export "PATH=$PATH:$HOME/bin"

libx264

H.264 video encoder.github

cd ~/ffmpeg_sources  
git clone --depth 1 git://git.videolan.org/x264  
cd x264  
./configure --prefix="$HOME/ffmpeg_build" --bindir="$HOME/bin" --enable-static  
make  
make install  
make distclean

libx265

cd ~/ffmpeg_sources 
wget https://bitbucket.org/multicoreware/x265/downloads/x265_1.7.tar.gz
    (ffmpeg3.2.5, 須要新的x265, 能夠使用2.4版本,wget https://bitbucket.org/multicoreware/x265/downloads/x265_2.4.tar.gz)
tar xzvf x265_1.7.tar.gz
cd x265_1.7/build/linux
./make-Makefiles.bash
make -j20
make install

若是出現找不到ccmake命令,能夠直接修改make-Makefiles.bash將ccmake改成cmake,或者安裝sudo apt install cmake-curses-gui。web

libfdk_aac

AAC audio encoder.bash

cd ~/ffmpeg_sources  
git clone --depth 1 git://git.code.sf.net/p/opencore-amr/fdk-aac  
cd fdk-aac  
autoreconf -fiv  
./configure --prefix="$HOME/ffmpeg_build" --disable-shared  
make  
make install  
make distclean

libmp3lame

Opus audio decoder and encoder.curl

# wget http://nchc.dl.sourceforge.net/project/lame/lame/3.99/lame-3.99.5.tar.gz
# tar xzvf lame-3.99.5.tar.gz
# cd lame-3.99.5
# ./configure --prefix="$HOME/ffmpeg_build"
# make 
# make install

opus

cd ~/ffmpeg_sources  
curl -O http://downloads.xiph.org/releases/opus/opus-1.1.tar.gz  
tar xzvf opus-1.1.tar.gz  
cd opus-1.1  
./configure --prefix="$HOME/ffmpeg_build" --disable-shared  
make  
make install  
make distclean

libogg

Ogg bitstream library. Required by libtheora and libvorbis.ide

cd ~/ffmpeg_sources  
curl -O http://downloads.xiph.org/releases/ogg/libogg-1.3.1.tar.gz  
tar xzvf libogg-1.3.1.tar.gz  
cd libogg-1.3.1  
./configure --prefix="$HOME/ffmpeg_build" --disable-shared  
make  
make install  
make distclean

libvorbis

Vorbis audio encoder. Requires libogg.svn

cd ~/ffmpeg_sources  
curl -O http://downloads.xiph.org/releases/vorbis/libvorbis-1.3.4.tar.gz  
tar xzvf libvorbis-1.3.4.tar.gz  
cd libvorbis-1.3.4  
./configure --prefix="$HOME/ffmpeg_build" --with-ogg="$HOME/ffmpeg_build" --disable-shared  
make  
make install  
make distclean

libvpx

VP8/VP9 video encoder.ui

cd ~/ffmpeg_sources  
git clone --depth 1 https://chromium.googlesource.com/webm/libvpx.git  
cd libvpx  
./configure --prefix="$HOME/ffmpeg_build" --disable-examples  
make  
make install  
make clean

注意:因爲國內最近沒法訪問Google因此libvpx沒法git clone ,此時能夠去別處下載:http://yunpan.cn/QhNk5tDqQV3MM 訪問密碼 45f6

xvid

cd ~/ffmpeg_sources
svn checkout http://svn.xvid.org/trunk --username anonymous
cd trunk/xvidcore/build/generic
./configure  
make && make install
cp /usr/local/lib/libxvidcore.so /usr/lib
ldconfig

libav

wget http://libav.org/releases/libav-10.7.tar.gz
 tar xzvf libav-10.7.tar.gz
 cd libav-10.7
 ./configure --enable-shared --extra-cflags=-fPIC 
 make && make install

FFmpeg

cd ~/ffmpeg_sources
git clone --depth 1 git://source.ffmpeg.org/ffmpeg
cd ffmpeg
PKG_CONFIG_PATH="$HOME/ffmpeg_build/lib/pkgconfig"
export PKG_CONFIG_PATH
./configure --prefix="$HOME/ffmpeg_build" --enable-shared --extra-cflags=-fPIC --extra-cflags="-I$HOME/ffmpeg_build/include" --extra-ldflags="-L$HOME/ffmpeg_build/lib" --bindir="$HOME/bin" --extra-libs=-ldl --enable-gpl --enable-nonfree --enable-libfdk_aac --enable-libmp3lame --enable-libopus --enable-libvorbis --enable-libvpx --enable-libx264 --enable-libx265 --enable-libxvid
make
make install
make distclean
hash -r
. ~/.bash_profile

安裝結束查看版本

ffpmeg -version

3. OpenCV

cd opencv
mkdir -p build && cd build
cmake ../
make all -j24 && make install
  1. 若是須要裝一個intel加速包ippcv,這個東西下載很慢,而且有時會斷掉,能夠手動下載以後再安裝。
  2. 若是是opencv3以上,要使用SIFT等特徵,須要一個第三方包,能夠到github上(https://github.com/opencv/opencv_contrib)找對應的release版本下來,再經過-DOPENCV_EXTRA_MODULES_PATH= /modules去配置編譯。
  3. 若是要把opencv編譯成靜態庫,加上-D BUILD_SHARED_LIBS=NO

4. Problems

問題一:

/usr/lib64/gcc/x86_64-linux/4.2.0/../../../../x86_64-linux/bin/ld: /usr/local/libavcodec.a(z.o): relocation R_X86_64_32 against `a local symbol' can not be used when making a shared object; recompile with -fPIC

解決方法:
加上fPIC選項,./configure --enable-shared --extra-cflags=-fPIC

問題二:

In file included from /home/aman/opencv/opencv/modules/videoio/src/cap_ffmpeg.cpp:45:0:
/home/aman/opencv/opencv/modules/videoio/src/cap_ffmpeg_impl.hpp: In function ‘int icv_av_write_frame_FFMPEG(AVFormatContext*, AVStream*, uint8_t*, uint32_t, AVFrame*)’:
/home/aman/opencv/opencv/modules/videoio/src/cap_ffmpeg_impl.hpp:1269:72: error: ‘avcodec_encode_video’ was not declared in this scope
     out_size = avcodec_encode_video(c, outbuf, outbuf_size, picture);

This is because libav has changed API and removed some stuff it's still used in OpenCV. Ubuntu 14.10 uses libav API version 11 and OpenCV still uses API version 9. The problem is that CODEC_ID_* was changed to AV_CODEC_ID_*.

解決方法:
以前下載的libav版本太新,opencv不支持。從新下載libav9,編譯安裝。

問題三:

裝了許多視頻解碼器,仍沒法播放視頻。

解決方法:
cmake ../發現v4l沒有找到,安裝v4l。命令:yum install libv4l-devel

相關文章
相關標籤/搜索