ubuntu
step1:依赖项
安装FFmpeg的依赖项:yasm,libx264(依赖yasm),libfaac,libmp3lame,libtheora,libvorbis,libxvid,libxext,libxfixes.命令如下:
apt-get install yasm aptitude install libx264-dev sudo aptitude install libfaac-dev libmp3lame-dev libtheora-dev libvorbis-dev libxvidcore-dev libxext-dev libxfixes-dev
step2:下载ffmpeg编译配置
cd ~/ffmpeg_sources
wget http://ffmpeg.org/releases/ffmpeg-snapshot.tar.bz2
tar xjvf ffmpeg-snapshot.tar.bz2
cd ffmpeg
# (后面的–enable-nonfree –enable-pic –enable-shared很重要)
PATH="$HOME/bin:$PATH" PKG_CONFIG_PATH="$HOME/ffmpeg_build/lib/pkgconfig" ./configure --prefix="$HOME/ffmpeg_build" --pkg-config-flags="--static" --extra-cflags="-I$HOME/ffmpeg_build/include" --extra-ldflags="-L$HOME/ffmpeg_build/lib" --bindir="$HOME/bin" --enable-gpl --enable-libass --enable-libfdk-aac --enable-libfreetype --enable-libmp3lame --enable-libopus --enable-libtheora --enable-libvorbis --enable-libvpx --enable-libx264 --enable-libx265 --enable-nonfree --enable-shared
PATH="$HOME/bin:$PATH" make
make install
make distclean
hash -r
ffmpeg -version
step3:安装Opencv
cd ~/OpenCV
wget -c http://sourceforge.net/projects/opencvlibrary/files/opencv-unix/3.1.0/opencv-3.1.0.zip
unzip opencv-3.1.0.zip
cd opencv-3.1.0
cmake -D CMAKE_BUILD_TYPE=RELEASE
-D INSTALL_C_EXAMPLES=OFF
-D INSTALL_PYTHON_EXAMPLES=ON
-D CMAKE_INSTALL_PREFIX=/usr/local
-D PYTHON_INCLUDE_DIR=/home/python/anaconda2/include/python2.7/
-D PYTHON_INCLUDE_DIR2=/home/python/anaconda2/include/python2.7
-D PYTHON_LIBRARY=/home/python/anaconda2/lib/libpython2.7.so
-D PYTHON_PACKAGES_PATH=/home/python/anaconda2/lib/python2.7/site-packages/
-D BUILD_EXAMPLES=ON
-D BUILD_NEW_PYTHON_SUPPORT=ON
-D PYTHON2_LIBRARY=/home/python/anaconda2/lib/libpython2.7.so
-D BUILD_opencv_python3=OFF
-D BUILD_opencv_python2=ON /home/python/OpenCV/opencv-3.1.0
make
sudo make install
sudo ldconfig
cd /home/python/anaconda2/lib/python2.7/site-packages/
ln -s /usr/local/lib/python2.7/site-packages/cv2.so cv2.so
注意:cmake后面的参数要按实际情况,具体看CMakeLists.txt
step4:测试安装
source activate py2
python -c "import cv2;print(cv2.__version__)"
python -c "import cv2;cap=cv2.VideoCapture(0);print(cap.isOpened())"
windows
电脑上ffmpeg跟python没关系【下载ffmpeng】
F:\ffmpeg\bin,并加入环境变量。
opencv安装到python虚拟环境中
第一步:将opencv安装目录下D:\ruanjian\opencv\build\python\2.7\x64的cv2.pyd复制到Anaconda安装目录D:\Anaconda\Lib\site-packages下。
第二步:将D:\ruanjian\opencv\build\x64\vc12\bin下的opencv_ffmpeg_64.dll和opencv_ffmpeg.dll复制到D:\Anaconda下,并改名为opencv_ffmpeg310_64.dll,其中310指代的是opencv的版本号
进入虚拟环境
conda install -c https://conda.anaconda.org/menpo opencv3==3.1.0
验证
python -c "import cv2;cap=cv2.VideoCapture(0);print(cap.isOpened())"
python -c "import cv2;print(cv2.__version__)"
python -c "import cv2;cap=cv2.VideoCapture('C:\\Users\\<username>\\Desktop\\ava_extraction_script\\videos\\NEQ7Wpf-EtI.avi');print(cap.isOpened())" # windows