python 使用cv2读取mp4视频失败的解决方法

python 使用cv2读取mp4视频失败的解决方法

  • cv2读取视频失败描述
    • 相关环境配置
    • 代码部分
    • terminal输出
    • 读取失败分析
  • 失败解决方案
  • 最终解决方案
    • 修改opencv版本
    • 安装相关配置
    • 结果展示

如果试遍全网方法仍用cv2读取不了视频,那么以下将是你解决问题前读过的最后一篇文章!!!

创作背景:博主今天很生气,也很郁闷,花了一下午时间,啥都没做,用cv2读取mp4视频,一直Opened()输出为Flase,一直显示打印出来的视频fps为0!!!这是为什么???在尝试了全网近乎所有解决cv2读取视频失败的方法之后,我对我的配置下读取失败的原因进行了分析,并给出本csdn下最终的解决方案。

cv2读取视频失败描述

博主使用的环境是服务器下linux-terminal(如果您使用python opencv-python在本地电脑,本文对您的帮助可能不是很大)

相关环境配置

python版本:3.6.9
opencv-python版本: 3.2.0.8(3.3.0.10版本以下均可)

代码部分

video_path = "path/to/my/video"
img_path = "path/to/my/save"
cap = cv2.VideoCapture()
cap.open(video_path)
fps = cap.get(cv2.CAP_PROP_FPS)
width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
print(fps, width, height)
suc = cap.isOpened()
print(suc)
frame_count = 0
while suc:frame_count += 1suc, frame = cap.read()cv2.imwrite(img_path + str(frame_count), frame)cv2.waitKey(1)
cap.release()
print('End!')

terminal输出

0.0 0 0
False
End!

读取失败分析

容易发现,terminal没有输出代码报错的位置,说明代码是没问题的。
此外,代码suc变量print出的结果是False,说明cv2没有按照我们给出视频地址读取对应的视频。
综上,我们有如下无法读取视频的分析:
1、视频路径错误;
2、opencv读取视频问题。
对于1,我们尝试更换其他视频,发现同样无法读取该视频;尝试更换视频格式(由mp4换成avi),发现还是无法读取该视频,我们有理由相信原有视频路径是没问题的。那么就是opencv的问题了。
对于2,根据网上print出当下opencv信息的方法:

import cv2
print(cv2.getBuildInformation())

我们可以得到如下的opencv信息:

  General configuration for OpenCV 3.3.0 =====================================Version control:               3.3.0Platform:Timestamp:                   2017-08-12T18:35:30ZHost:                        Linux 4.4.0-83-generic x86_64CMake:                       3.9.0CMake generator:             Unix MakefilesCMake build tool:            /usr/bin/gmakeConfiguration:               ReleaseCPU/HW features:Baseline:                    SSE SSE2 SSE3requested:                 SSE3Dispatched code generation:  SSE4_1 SSE4_2 FP16 AVX AVX2requested:                 SSE4_1 SSE4_2 AVX FP16 AVX2SSE4_1 (2 files):          + SSSE3 SSE4_1SSE4_2 (1 files):          + SSSE3 SSE4_1 POPCNT SSE4_2FP16 (1 files):            + SSSE3 SSE4_1 POPCNT SSE4_2 FP16 AVXAVX (5 files):             + SSSE3 SSE4_1 POPCNT SSE4_2 AVXAVX2 (7 files):            + SSSE3 SSE4_1 POPCNT SSE4_2 FP16 FMA3 AVX AVX2C/C++:Built as dynamic libs?:      NOC++ Compiler:                /opt/rh/devtoolset-2/root/usr/bin/c++  (ver 4.8.2)C++ flags (Release):         -fPIC   -fsigned-char -W -Wall -Werror=return-type -Werror=non-virtual-dtor -Werror=address -Werror=sequence-point -Wformat -Werror=format-security -Wmissing-declarations -Wundef -Winit-self -Wpointer-arith -Wshadow -Wsign-promo -Wuninitialized -Winit-self -Wno-narrowing -Wno-delete-non-virtual-dtor -Wno-comment -fdiagnostics-show-option -Wno-long-long -pthread -fomit-frame-pointer -ffunction-sections  -msse -msse2 -msse3 -fvisibility=hidden -fvisibility-inlines-hidden -O3 -DNDEBUG  -DNDEBUGC++ flags (Debug):           -fPIC   -fsigned-char -W -Wall -Werror=return-type -Werror=non-virtual-dtor -Werror=address -Werror=sequence-point -Wformat -Werror=format-security -Wmissing-declarations -Wundef -Winit-self -Wpointer-arith -Wshadow -Wsign-promo -Wuninitialized -Winit-self -Wno-narrowing -Wno-delete-non-virtual-dtor -Wno-comment -fdiagnostics-show-option -Wno-long-long -pthread -fomit-frame-pointer -ffunction-sections  -msse -msse2 -msse3 -fvisibility=hidden -fvisibility-inlines-hidden -g  -O0 -DDEBUG -D_DEBUGC Compiler:                  /opt/rh/devtoolset-2/root/usr/bin/ccC flags (Release):           -fPIC   -fsigned-char -W -Wall -Werror=return-type -Werror=non-virtual-dtor -Werror=address -Werror=sequence-point -Wformat -Werror=format-security -Wmissing-declarations -Wmissing-prototypes -Wstrict-prototypes -Wundef -Winit-self -Wpointer-arith -Wshadow -Wuninitialized -Winit-self -Wno-narrowing -Wno-comment -fdiagnostics-show-option -Wno-long-long -pthread -fomit-frame-pointer -ffunction-sections  -msse -msse2 -msse3 -fvisibility=hidden -O3 -DNDEBUG  -DNDEBUGC flags (Debug):             -fPIC   -fsigned-char -W -Wall -Werror=return-type -Werror=non-virtual-dtor -Werror=address -Werror=sequence-point -Wformat -Werror=format-security -Wmissing-declarations -Wmissing-prototypes -Wstrict-prototypes -Wundef -Winit-self -Wpointer-arith -Wshadow -Wuninitialized -Winit-self -Wno-narrowing -Wno-comment -fdiagnostics-show-option -Wno-long-long -pthread -fomit-frame-pointer -ffunction-sections  -msse -msse2 -msse3 -fvisibility=hidden -g  -O0 -DDEBUG -D_DEBUGLinker flags (Release):Linker flags (Debug):ccache:                      NOPrecompiled headers:         YESExtra dependencies:          gthread-2.0 glib-2.0 /lib64/libz.so stdc++ dl m pthread rt3rdparty dependencies:       ittnotify libprotobuf libjpeg libwebp libpng libtiff libjasper IlmImfOpenCV modules:To be built:                 core flann imgproc ml objdetect photo video dnn imgcodecs shape videoio highgui superres features2d calib3d stitching videostab python3Disabled:                    worldDisabled by dependency:      -Unavailable:                 cudaarithm cudabgsegm cudacodec cudafeatures2d cudafilters cudaimgproc cudalegacy cudaobjdetect cudaoptflow cudastereo cudawarping cudev java python2 ts vizGUI:QT:                          NOGTK+:                        NOGThread :                    YES (ver 2.12.3)GtkGlExt:                    NOOpenGL support:              NOVTK support:                 NOMedia I/O:ZLib:                        /lib64/libz.so (ver 1.2.3)JPEG:                        libjpeg (ver 90)WEBP:                        build (ver encoder: 0x020e)PNG:                         build (ver 1.6.24)TIFF:                        build (ver 42 - 4.0.2)JPEG 2000:                   build (ver 1.900.1)OpenEXR:                     build (ver 1.7.1)GDAL:                        NOGDCM:                        NOVideo I/O:DC1394 1.x:                  NODC1394 2.x:                  NOFFMPEG:                      NOavcodec:                   NOavformat:                  NOavutil:                    NOswscale:                   NOavresample:                NOGStreamer:                   NOOpenNI:                      NOOpenNI PrimeSensor Modules:  NOOpenNI2:                     NOPvAPI:                       NOGigEVisionSDK:               NOAravis SDK:                  NOUniCap:                      NOUniCap ucil:                 NOV4L/V4L2:                    NO/NOXIMEA:                       NOXine:                        NOIntel Media SDK:             NOgPhoto2:                     NOParallel framework:            pthreadsTrace:                         YES (with Intel ITT)Other third-party libraries:Use Intel IPP:               NOUse Intel IPP IW:            NOUse Intel IPP Async:         NOUse VA:                      NOUse Intel VA-API/OpenCL:     NOUse Lapack:                  NOUse Eigen:                   NOUse Cuda:                    NOUse OpenCL:                  YESUse OpenVX:                  NOUse custom HAL:              NOOpenCL:                        Include path:                /io/opencv/3rdparty/include/opencl/1.2Use AMDFFT:                  NOUse AMDBLAS:                 NOPython 2:Interpreter:                 NOPython 3:Interpreter:                 python (ver 3.6.0)Libraries:numpy:                       /opt/python/cp36-cp36m/lib/python3.6/site-packages/numpy/core/include (ver 1.11.3)packages path:               /opt/python/cp36-cp36m/lib/python3.6/site-packagesPython (for build):            pythonJava:ant:                         NOJNI:                         NOJava wrappers:               NOJava tests:                  NOMatlab:                        Matlab not found or implicitly disabledTests and samples:Tests:                       NOPerformance tests:           NOC/C++ Examples:              NOInstall path:                  /usr/localcvconfig.h is in:              /io/opencv/build
-----------------------------------------------------------------

容易发现,video下方基本都是NO,最终我们确定是opencv的问题。

失败解决方案

1、网上说没有安装ffmpeg库,我们尝试安装此库,发现并不能解决问题。
2、没有安装opencv-contrib-python-headless,我们尝试安装此库,发现并不能解决问题。
3、opencv库里面dll文件异常,我们尝试修改dll文件,发现并不能解决问题。

最终解决方案

修改opencv版本

我们尝试提高opencv版本,将原来的3.2.0.8版本更新到4.1.1版本及以上,令我们出乎意料的是这个方法将上述遇到的问题解决了,下面将分享我们解决问题的过程和方法。

安装相关配置

我们尝试根据如下指令提高opencv的版本:

pip install opencv-python==4.1.1.26

之后运行代码,发现出现如下报错:

ImportError: libSM.so.6: cannot open shared object file: No such file or directory

依次运行如下命令,即可解决此报错:

apt-get update (选择)
apt-get install sudo (选择)
sudo apt-get install libsm6
sudo apt-get install libxrender1
sudo apt-get install libxext-dev

结果展示

再次查看opencv信息:

 General configuration for OpenCV 4.1.1 =====================================Version control:               4.1.1Platform:Timestamp:                   2019-09-02T14:45:10ZHost:                        Linux 4.15.0-1028-gcp x86_64CMake:                       3.9.0CMake generator:             Unix MakefilesCMake build tool:            /usr/bin/gmakeConfiguration:               ReleaseCPU/HW features:Baseline:                    SSE SSE2 SSE3requested:                 SSE3Dispatched code generation:  SSE4_1 SSE4_2 FP16 AVX AVX2requested:                 SSE4_1 SSE4_2 AVX FP16 AVX2 AVX512_SKXSSE4_1 (13 files):         + SSSE3 SSE4_1SSE4_2 (1 files):          + SSSE3 SSE4_1 POPCNT SSE4_2FP16 (0 files):            + SSSE3 SSE4_1 POPCNT SSE4_2 FP16 AVXAVX (4 files):             + SSSE3 SSE4_1 POPCNT SSE4_2 AVXAVX2 (27 files):           + SSSE3 SSE4_1 POPCNT SSE4_2 FP16 FMA3 AVX AVX2C/C++:Built as dynamic libs?:      NOC++ Compiler:                /usr/lib/ccache/compilers/c++  (ver 4.8.2)C++ flags (Release):         -Wl,-strip-all   -fsigned-char -W -Wall -Werror=return-type -Werror=non-virtual-dtor -Werror=address -Werror=sequence-point -Wformat -Werror=format-security -Wmissing-declarations -Wundef -Winit-self -Wpointer-arith -Wsign-promo -Wuninitialized -Winit-self -Wno-delete-non-virtual-dtor -Wno-comment -Wno-missing-field-initializers -fdiagnostics-show-option -Wno-long-long -pthread -fomit-frame-pointer -ffunction-sections -fdata-sections  -msse -msse2 -msse3 -fvisibility=hidden -fvisibility-inlines-hidden -O3 -DNDEBUG  -DNDEBUGC++ flags (Debug):           -Wl,-strip-all   -fsigned-char -W -Wall -Werror=return-type -Werror=non-virtual-dtor -Werror=address -Werror=sequence-point -Wformat -Werror=format-security -Wmissing-declarations -Wundef -Winit-self -Wpointer-arith -Wsign-promo -Wuninitialized -Winit-self -Wno-delete-non-virtual-dtor -Wno-comment -Wno-missing-field-initializers -fdiagnostics-show-option -Wno-long-long -pthread -fomit-frame-pointer -ffunction-sections -fdata-sections  -msse -msse2 -msse3 -fvisibility=hidden -fvisibility-inlines-hidden -g  -O0 -DDEBUG -D_DEBUGC Compiler:                  /usr/lib/ccache/compilers/ccC flags (Release):           -Wl,-strip-all   -fsigned-char -W -Wall -Werror=return-type -Werror=non-virtual-dtor -Werror=address -Werror=sequence-point -Wformat -Werror=format-security -Wmissing-declarations -Wmissing-prototypes -Wstrict-prototypes -Wundef -Winit-self -Wpointer-arith -Wuninitialized -Winit-self -Wno-comment -Wno-missing-field-initializers -fdiagnostics-show-option -Wno-long-long -pthread -fomit-frame-pointer -ffunction-sections -fdata-sections  -msse -msse2 -msse3 -fvisibility=hidden -O3 -DNDEBUG  -DNDEBUGC flags (Debug):             -Wl,-strip-all   -fsigned-char -W -Wall -Werror=return-type -Werror=non-virtual-dtor -Werror=address -Werror=sequence-point -Wformat -Werror=format-security -Wmissing-declarations -Wmissing-prototypes -Wstrict-prototypes -Wundef -Winit-self -Wpointer-arith -Wuninitialized -Winit-self -Wno-comment -Wno-missing-field-initializers -fdiagnostics-show-option -Wno-long-long -pthread -fomit-frame-pointer -ffunction-sections -fdata-sections  -msse -msse2 -msse3 -fvisibility=hidden -g  -O0 -DDEBUG -D_DEBUGLinker flags (Release):      -L/root/ffmpeg_build/lib  -Wl,--gc-sectionsLinker flags (Debug):        -L/root/ffmpeg_build/lib  -Wl,--gc-sectionsccache:                      YESPrecompiled headers:         NOExtra dependencies:          ade /opt/Qt4.8.7/lib/libQtGui.so /opt/Qt4.8.7/lib/libQtTest.so /opt/Qt4.8.7/lib/libQtCore.so /lib64/libz.so dl m pthread rt3rdparty dependencies:       ittnotify libprotobuf libjpeg-turbo libwebp libpng libtiff libjasper IlmImf quircOpenCV modules:To be built:                 calib3d core dnn features2d flann gapi highgui imgcodecs imgproc ml objdetect photo python3 stitching video videoioDisabled:                    worldDisabled by dependency:      -Unavailable:                 java js python2 tsApplications:                -Documentation:               NONon-free algorithms:         NOGUI:QT:                          YES (ver 4.8.7 EDITION = OpenSource)QT OpenGL support:         NOGTK+:                        NOVTK support:                 NOMedia I/O:ZLib:                        /lib64/libz.so (ver 1.2.3)JPEG:                        libjpeg-turbo (ver 2.0.2-62)WEBP:                        build (ver encoder: 0x020e)PNG:                         build (ver 1.6.37)TIFF:                        build (ver 42 - 4.0.10)JPEG 2000:                   build (ver 1.900.1)OpenEXR:                     build (ver 2.3.0)HDR:                         YESSUNRASTER:                   YESPXM:                         YESPFM:                         YESVideo I/O:DC1394:                      NOFFMPEG:                      YESavcodec:                   YES (58.55.100)avformat:                  YES (58.31.101)avutil:                    YES (56.33.100)swscale:                   YES (5.6.100)avresample:                NOGStreamer:                   NOv4l/v4l2:                    YES (linux/videodev2.h)Parallel framework:            pthreadsTrace:                         YES (with Intel ITT)Other third-party libraries:Lapack:                      NOEigen:                       NOCustom HAL:                  NOProtobuf:                    build (3.5.1)OpenCL:                        YES (no extra features)Include path:                /io/opencv/3rdparty/include/opencl/1.2Link libraries:              Dynamic loadPython 3:Interpreter:                 /opt/python/cp36-cp36m/bin/python (ver 3.6.9)Libraries:                   libpython3.6m.a (ver 3.6.9)numpy:                       /opt/python/cp36-cp36m/lib/python3.6/site-packages/numpy/core/include (ver 1.11.3)install path:                pythonPython (for build):            /opt/python/cp36-cp36m/bin/pythonJava:ant:                         NOJNI:                         NOJava wrappers:               NOJava tests:                  NOInstall to:                    /io/_skbuild/linux-x86_64-3.6/cmake-install
-----------------------------------------------------------------

显然,video下方已经由NO变成YES

再次运行代码,terminal输出结果:

59.94005994005994 1920 1080
True
End!

fps、视频长宽均正常输出,且视频正常打开,问题解决!

希望此分享能帮助遇到同样问题的您!


本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!

相关文章

立即
投稿

微信公众账号

微信扫一扫加关注

返回
顶部