python 使用cv2读取mp4视频失败的解决方法
Posted 不会吃草的猪
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python 使用cv2读取mp4视频失败的解决方法相关的知识,希望对你有一定的参考价值。
python 使用cv2读取mp4视频失败的解决方法
如果试遍全网方法仍用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 += 1
suc, 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.0
Platform:
Timestamp: 2017-08-12T18:35:30Z
Host: Linux 4.4.0-83-generic x86_64
CMake: 3.9.0
CMake generator: Unix Makefiles
CMake build tool: /usr/bin/gmake
Configuration: Release
CPU/HW features:
Baseline: SSE SSE2 SSE3
requested: SSE3
Dispatched code generation: SSE4_1 SSE4_2 FP16 AVX AVX2
requested: SSE4_1 SSE4_2 AVX FP16 AVX2
SSE4_1 (2 files): + SSSE3 SSE4_1
SSE4_2 (1 files): + SSSE3 SSE4_1 POPCNT SSE4_2
FP16 (1 files): + SSSE3 SSE4_1 POPCNT SSE4_2 FP16 AVX
AVX (5 files): + SSSE3 SSE4_1 POPCNT SSE4_2 AVX
AVX2 (7 files): + SSSE3 SSE4_1 POPCNT SSE4_2 FP16 FMA3 AVX AVX2
C/C++:
Built as dynamic libs?: NO
C++ 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 -DNDEBUG
C++ 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_DEBUG
C Compiler: /opt/rh/devtoolset-2/root/usr/bin/cc
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 -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 -DNDEBUG
C 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_DEBUG
Linker flags (Release):
Linker flags (Debug):
ccache: NO
Precompiled headers: YES
Extra dependencies: gthread-2.0 glib-2.0 /lib64/libz.so stdc++ dl m pthread rt
3rdparty dependencies: ittnotify libprotobuf libjpeg libwebp libpng libtiff libjasper IlmImf
OpenCV modules:
To be built: core flann imgproc ml objdetect photo video dnn imgcodecs shape videoio highgui superres features2d calib3d stitching videostab python3
Disabled: world
Disabled by dependency: -
Unavailable: cudaarithm cudabgsegm cudacodec cudafeatures2d cudafilters cudaimgproc cudalegacy cudaobjdetect cudaoptflow cudastereo cudawarping cudev java python2 ts viz
GUI:
QT: NO
GTK+: NO
GThread : YES (ver 2.12.3)
GtkGlExt: NO
OpenGL support: NO
VTK support: NO
Media 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: NO
GDCM: NO
Video I/O:
DC1394 1.x: NO
DC1394 2.x: NO
FFMPEG: NO
avcodec: NO
avformat: NO
avutil: NO
swscale: NO
avresample: NO
GStreamer: NO
OpenNI: NO
OpenNI PrimeSensor Modules: NO
OpenNI2: NO
PvAPI: NO
GigEVisionSDK: NO
Aravis SDK: NO
UniCap: NO
UniCap ucil: NO
V4L/V4L2: NO/NO
XIMEA: NO
Xine: NO
Intel Media SDK: NO
gPhoto2: NO
Parallel framework: pthreads
Trace: YES (with Intel ITT)
Other third-party libraries:
Use Intel IPP: NO
Use Intel IPP IW: NO
Use Intel IPP Async: NO
Use VA: NO
Use Intel VA-API/OpenCL: NO
Use Lapack: NO
Use Eigen: NO
Use Cuda: NO
Use OpenCL: YES
Use OpenVX: NO
Use custom HAL: NO
OpenCL: <Dynamic loading of OpenCL library>
Include path: /io/opencv/3rdparty/include/opencl/1.2
Use AMDFFT: NO
Use AMDBLAS: NO
Python 2:
Interpreter: NO
Python 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-packages
Python (for build): python
Java:
ant: NO
JNI: NO
Java wrappers: NO
Java tests: NO
Matlab: Matlab not found or implicitly disabled
Tests and samples:
Tests: NO
Performance tests: NO
C/C++ Examples: NO
Install path: /usr/local
cvconfig.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.1
Platform:
Timestamp: 2019-09-02T14:45:10Z
Host: Linux 4.15.0-1028-gcp x86_64
CMake: 3.9.0
CMake generator: Unix Makefiles
CMake build tool: /usr/bin/gmake
Configuration: Release
CPU/HW features:
Baseline: SSE SSE2 SSE3
requested: SSE3
Dispatched code generation: SSE4_1 SSE4_2 FP16 AVX AVX2
requested: SSE4_1 SSE4_2 AVX FP16 AVX2 AVX512_SKX
SSE4_1 (13 files): + SSSE3 SSE4_1
SSE4_2 (1 files): + SSSE3 SSE4_1 POPCNT SSE4_2
FP16 (0 files): + SSSE3 SSE4_1 POPCNT SSE4_2 FP16 AVX
AVX (4 files): + SSSE3 SSE4_1 POPCNT SSE4_2 AVX
AVX2 (27 files): + SSSE3 SSE4_1 POPCNT SSE4_2 FP16 FMA3 AVX AVX2
C/C++:
Built as dynamic libs?: NO
C++ 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 -DNDEBUG
C++ 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_DEBUG
C Compiler: /usr/lib/ccache/compilers/cc
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 -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 -DNDEBUG
C 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_DEBUG
Linker flags (Release): -L/root/ffmpeg_build/lib -Wl,--gc-sections
Linker flags (Debug): -L/root/ffmpeg_build/lib -Wl,--gc-sections
ccache: YES
Precompiled headers: NO
Extra 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 rt
3rdparty dependencies: ittnotify libprotobuf libjpeg-turbo libwebp libpng libtiff libjasper IlmImf quirc
OpenCV modules:
To be built: calib3d core dnn features2d flann gapi highgui imgcodecs imgproc ml objdetect photo python3 stitching video videoio
Disabled: world
Disabled by dependency: -
Unavailable: java js python2 ts
Applications: -
Documentation: NO
Non-free algorithms: NO
GUI:
QT: YES (ver 4.8.7 EDITION = OpenSource)
QT OpenGL support: NO
GTK+: NO
VTK support: NO
Media 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: YES
SUNRASTER: YES
PXM: YES
PFM: YES
Video I/O:
DC1394: NO
FFMPEG: YES
avcodec: YES (58.55.100)
avformat: YES (58.31.101)
avutil: YES (56.33.100)
swscale: YES (5.6.100)
avresample: NO
GStreamer: NO
v4l/v4l2: YES (linux/videodev2.h)
Parallel framework: pthreads
Trace: YES (with Intel ITT)
Other third-party libraries:
Lapack: NO
Eigen: NO
Custom HAL: NO
Protobuf: build (3.5.1)
OpenCL: YES (no extra features)
Include path: /io/opencv/3rdparty/include/opencl/1.2
Link libraries: Dynamic load
Python 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: python
Python (for build): /opt/python/cp36-cp36m/bin/python
Java:
ant: NO
JNI: NO
Java wrappers: NO
Java tests: NO
Install to: /io/_skbuild/linux-x86_64-3.6/cmake-install
-----------------------------------------------------------------
显然,video下方已经由NO变成YES。
再次运行代码,terminal输出结果:
59.94005994005994 1920 1080
True
End!
fps、视频长宽均正常输出,且视频正常打开,问题解决!
希望此分享能帮助遇到同样问题的您!
Android平台直接从URL播放mp4视频失败,有啥解决办法吗?
【中文标题】Android平台直接从URL播放mp4视频失败,有啥解决办法吗?【英文标题】:Failed to play mp4 video directly from URL in Android platform, is there any solution?Android平台直接从URL播放mp4视频失败,有什么解决办法吗? 【发布时间】:2012-08-26 14:02:09 【问题描述】:我正在尝试在 videoView 上播放来自 URL 的视频。视频扩展是mp4,问题是出现这样的错误:
logcat 是这样的:
![在此处输入图片描述][2]
实际上,我在 ***.com 上发现了很多问题,但没有什么是方便的。问题在于 mp4 格式,我尝试了 3gp 并且效果很好。我不想下载视频然后播放,也不想将其转换为 3gp。
感谢您的帮助。
EDIT:: 这里是代码sn-p
String LINK = "https://********/cbcbcbcbcbcbcbcbcbcbcbcbcbcbcbcb.mp4";
VideoView videoView = (VideoView) findViewById(R.id.videoView1);
MediaController mc = new MediaController(this);
mc.setAnchorView(videoView);
mc.setMediaPlayer(videoView);
Uri video = Uri.parse(LINK);
videoView.setMediaController(mc);
videoView.setVideoURI(video);
videoView.start();
编辑:
我不知道如何应用这个来自 android 网站的建议:
For 3GPP and MPEG-4 containers, the moov atom must precede any mdat atoms, but must succeed the ftyp atom.
For 3GPP, MPEG-4, and WebM containers, audio and video samples corresponding to the same time offset may be no more than 500 KB apart. To minimize this audio/video drift, consider interleaving audio and video in smaller chunk sizes.
【问题讨论】:
放一些代码看看是什么原因造成的 感谢@Filipe Batista 我已添加代码 它适用于另一个带有 http 的 mp4 视频...我上面的视频是 HTTPS 并且需要 SSL 证书,所以还有另一个问题 【参考方案1】:就像@Giuseppe 所说,HTTPS
可能是问题所在,如果您使用的是 3.1 之前的 Android 版本。
如果在这里看到它:
http://developer.android.com/guide/appendix/media-formats.html
它说:
注意:在 Android 3.1 之前不支持 HTTPS。
编辑: 确保您在清单文件中拥有以下权限:
<uses-permission android:name="android.permission.INTERNET" />
还要测试在真实设备上播放的视频,因为有时模拟器会出现一些问题。
【讨论】:
查看我编辑的答案。我还在真实设备(Android 2.3.6)上测试了该代码,并且可以正常工作。 你是如何解决姜饼中的 https 的,很高兴知道,谢谢@Amt87以上是关于python 使用cv2读取mp4视频失败的解决方法的主要内容,如果未能解决你的问题,请参考以下文章