ubuntu14.04安装配置FFmpeg+opencv-3.1.0+python 调用opencv模块

http://blog.csdn.net/u010106759/article/details/51931815

 

ubuntu14.04安装配置FFmpeg+opencv-3.1.0+python 调用opencv模块

这次因为写python时候要调opencv的函数,在ubuntu14.04安装了opencv,中间出现一个安装错误:

/usr/bin/ld: /usr/local/lib/libavcodec.a(avpacket.o): relocation R_X86_64_32 against `.rodata.str1.1' can not be used when making a shared object; recompile with -fPIC
/usr/local/lib/libavcodec.a: error adding symbols: Bad value
collect2: error: ld returned 1 exit status
make[2]: *** [lib/libopencv_videoio.so.3.0.0] Error 1
make[1]: *** [modules/videoio/CMakeFiles/opencv_videoio.dir/all] Error 2
make: *** [all] Error 2
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

导致大半夜时间都在解决(编译时间太长了),这个是FFmpeg软件安装和配置失败造成的,具体原因在配置时候未设置成正确的共享参数(编译生成动态链接库需要参数共享)

安装编译配置FFmpeg

step1:依赖项

安装FFmpeg的依赖项:yasm,libx264(依赖yasm),libfaac,libmp3lame,libtheora,libvorbis,libxvid,libxext,libxfixes.命令如下:
安装yasm

apt-get install yasm 
  • 1

安装libx264

aptitude install libx264-dev 
  • 1

安装其余软件

sudo aptitude install libfaac-dev libmp3lame-dev libtheora-dev libvorbis-dev libxvidcore-dev  libxext-dev libxfixes-dev
  • 1

step2:下载编译配置

下载FFmpeg:官网网址,解压,进入到解压的目录
编译很重要,这个配置错了会导致上面提到编译opencv时的错误

./configure --prefix=/usr/local/ffmpeg --enable-gpl --enable-version3 --enable-nonfree --enable-postproc --enable-pthreads --enable-libfaac --enable-libmp3lame --enable-libtheora --enable-libx264 --enable-libxvid --enable-x11grab --enable-libvorbis  --enable-nonfree --enable-pic --enable-shared
  • 1

后面的–enable-nonfree –enable-pic –enable-shared很重要
使用prefix指定安装路径,则需要/ect/profile配置文件修改对应的路径配置,让系统正确找到FFmpeg的安装路径:(简单的安装方法是不指定安装路径,就不用配置相关路径,如果一直有错误,可以尝试不指定安装路径来安装,但这样或许对之后删除管理较为麻烦)

vi /etc/profile
  • 1

在文件末尾加上两句话:

export FFMPEG_HOME=/usr/local/ffmpeg 
export PATH=$FFMPEG_HOME/bin:$PATH
  • 1
  • 2

使生效

source profile
  • 1

step3:安装

make
make install
  • 1
  • 2

安装之后在/usr/local/ffmpeg会看到有三个目录:bin 执行文件目录,lib 静态,动态链接库目录,include 编程用到的头文件

step4:检查安装和配置是否正确

ffmpeg -version 
  • 1

出现如下指令表示安装正确(configuration的配置一定要一致):

configuration: --enable-nonfree --enable-pic --enable-shared
libavutil      55. 28.100 / 55. 28.100
libavcodec     57. 48.101 / 57. 48.101
libavformat    57. 41.100 / 57. 41.100
libavdevice    57.  0.101 / 57.  0.101
libavfilter     6. 47.100 /  6. 47.100
libswscale      4.  1.100 /  4.  1.100
libswresample   2.  1.100 /  2.  1.100
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

注意:
1:为了防止执行程序找不到库文件,可以将/usr/local/ffmpeg/lib目录设置到LD_LIBRARY_PATH环境变量
2:若出现error while loading shared libraries: libavdevice.so.52的错误修改/etc/ld.so.conf 在最后一行加上/usr/local/ffmpeg/libldconfig -v,并修改 /usr/local/ffmpeg/lib目录下的文件权限为777
3:在ubuntu 上安装ffmpeg后,如果你命令ffmpeg不管用,把编译后文件中/usr/local/ffmpeg/bin/ffmpeg文件复制到/usr/ bin当中

安装Opencv

step1:下载:官网,解压到opencv

step2:安装依赖项

sudo apt-get -qq install libopencv-dev build-essential checkinstall cmake pkg-config yasm libjpeg-dev libjasper-dev libavcodec-dev libavformat-dev libswscale-dev libdc1394-22-dev libxine-dev libgstreamer0.10-dev libgstreamer-plugins-base0.10-dev libv4l-dev python-dev python-numpy libtbb-dev libqt4-dev libgtk2.0-dev libfaac-dev libmp3lame-dev libopencore-amrnb-dev libopencore-amrwb-dev libtheora-dev libvorbis-dev libxvidcore-dev x264 v4l-utils 
  • 1

ffmpeg已经安装过,无需再装

step3:创建编译目录,编译

cd ~/opencv
mkdir release
cd release
cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/usr/local ..
make
sudo make install
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

编译过程时间较长

step4:测试,参考文献[5].

#python调用opencv模块

step1:安装python-opencv,numpy

sudo apt-get install python-opencv
sudo apt-get install python-numpy
  • 1
  • 2

step2:测试调用

python
import cv

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.