简体   繁体   English

无法在Windows 7机器中使用OpenCV 2.4.3,Python 2.7打开“.mp4”视频文件

[英]Cannot open “.mp4” video files using OpenCV 2.4.3, Python 2.7 in Windows 7 machine

I am currently working on a project that involves reading mp4 video files. 我目前正在开展一个涉及阅读mp4视频文件的项目。 The problem I encountered is that it using Python 2.7 (32 bit), OpenCV 2.4.3 (cv2.pyd) in a Windows 7 machine. 我遇到的问题是它在Windows 7机器中使用Python 2.7(32位),OpenCV 2.4.3(cv2.pyd)。

The code snippet is as follows: 代码段如下:

try:
        video = cv2.VideoCapture("video.mp4")
except:
        print "Could not open video file"
        raise
print video.grab()

" video.grab() " always returns false: meaning it doesn't read the file " video.mp4 " But when we try this: video.grab() ”总是返回false:意味着它不会读取文件“ video.mp4 ”但是当我们尝试这个时:

try:
        video = cv2.VideoCapture("video.avi")
except:
        print "Could not open video file"
        raise
print video.grab()

" video.grab() " returns true: meaning it is able to read " .avi " files. video.grab() ”返回true:表示它能够读取“ .avi ”文件。

Another is we have tried this same snippet on Linux and Mac and it seems to work fine, meaning it is able to read both mp4 files and avi files. 另一个是我们在Linux和Mac上试过这个相同的片段,它看起来工作正常,这意味着它能够读取mp4文件和avi文件。

This problem is similar to this problem and this problem . 这个问题类似于这个问题这个问题 Both still don't have a clear and workable answer. 两者仍然没有明确和可行的答案。

I would appreciate any help or workaround aside from just using Linux or Mac for programming this as I need this to work on all three systems. 除了使用Linux或Mac进行编程之外,我将不胜感激任何帮助或解决方法,因为我需要在所有三个系统上工作。

I have had the same issue before, solved by this step: 之前我遇到过同样的问题,通过这一步解决了:

Check your OpenCV python version 检查你的OpenCV python版本

>>> from cv2 import __version__
>>> __version__
'2.4.0'

Then Copy your opencv_ffmpeg.dll to C:\\Python27\\ and rename it to relevant your OpenCV Python Version. 然后将opencv_ffmpeg.dll复制到C:\\Python27\\并将其重命名为与OpenCV Python版本相关的。 In my case I had to rename it to opencv_ffmpeg240.dll . 在我的情况下,我不得不将其重命名为opencv_ffmpeg240.dll

Update: On Windows, you can find the opencv_ffmpeg DLL inside of the build folder of your OpenCV installation. 更新:在Windows上,您可以在OpenCV安装的build文件夹中找到opencv_ffmpeg DLL。 For example: C:\\dev\\opencv\\build\\x86\\vc12\\bin 例如: C:\\dev\\opencv\\build\\x86\\vc12\\bin

Then, just copy and paste the opencv_ffmpeg<version>.dll file into the root folder of your Python installation. 然后,只需将opencv_ffmpeg<version>.dll文件复制并粘贴到Python安装的根文件夹中即可。

I ran into this issue using OpenCV version 2.4.11 and Python 2.7 under a Windows 7 operating system. 我在Windows 7操作系统下使用OpenCV版本2.4.11和Python 2.7遇到了这个问题。 I wasn't able to open and manipulate mp4 files, but was able to open avi files. 我无法打开和操作mp4文件,但能够打开avi文件。

The solution in my case was to copy the opencv_ffmpeg2411.dll file from the build folder of my OpenCV installation, and paste it into the root folder of my Python installation. 在我的情况下,解决方案是从我的OpenCV安装的build文件夹中复制opencv_ffmpeg2411.dll文件,并将其粘贴到我的Python安装的根文件夹中。 So, in my case, the DLL file is in "C:\\dev\\opencv\\build\\x86\\vc12\\bin", and I copied it to "C:\\Program Files(x86)\\Python2.7". 因此,在我的情况下,DLL文件位于“C:\\ dev \\ opencv \\ build \\ x86 \\ vc12 \\ bin”中,我将其复制到“C:\\ Program Files(x86)\\ Python2.7”。

Your mp4 may be having codecs for which your system does not have support (or opencv does not have support) while your avi codecs may be supported. 您的mp4可能具有您的系统不支持的编解码器(或opencv没有支持),而您的avi编解码器可能受支持。 Also if opencv is using libav for decoding you should install that. 另外如果opencv使用libav进行解码,你应该安装它。

I had the same problem with my .mpg video file. 我的.mpg视频文件遇到了同样的问题。 Couldn't open it in openCV . 无法在openCV打开它。

I copied the openCv_ffmpeg330.dll from the C:\\OpenCV\\build\\bin folder into the c:\\python27 folder. 我将openCv_ffmpeg330.dllC:\\OpenCV\\build\\bin folder into the c:\\python27文件夹中。

That worked!. 那很有效!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM