51

I'm using Python bindings to OpenCV 2.4 installed with following instructions.

My problem is similar to this one, but I need Windows machine solution.

Problem: when I try to use

cap = cv2.VideoCapture(0)
print cap.grab()

it works fine, but the following code

cap = cv2.VideoCapture(filename)
print cap.grab()

always return False.

Path to the file is correct, absolute and has no spaces.

I tried on two machines with Windows XP and Windows 7 with similar results. On Linux (Ubuntu) it works fine for me.

Community
  • 1
  • 1
Bihaqo
  • 670
  • 1
  • 6
  • 10
  • Could you post value of the filename? Try using `filename = os.path.join( filename )`. more info here: http://stackoverflow.com/questions/1633643/unix-paths-officially-work-in-python-for-any-platform – Larry Battle Jul 28 '12 at 08:03
  • It was `"C:\\m.avi"`, but I just tried `"C:\m.avi"` and `"C:/m.avi"`. It still not working (with and without `os.path.join`). – Bihaqo Jul 28 '12 at 08:29
  • 1
    Check if opencv_ffmpeg.dll is in the same directory that other OpenCV dlls used by python. Also, try another avi file. Sometimes OpenCV can't decode files that are correctly played by other programs (ex. Windows Media Player) – Piotr Jul 28 '12 at 12:52
  • I didn't find any files with "opencv" in a name on my computer (except opencv folder and it's contents of course). So I just tried to copy `opencv_ffmpeg.dll` to `C:\Python27\Lib\site-packages\` (like `cv2.pyd`) but it doesn't work. – Bihaqo Jul 30 '12 at 11:10
  • Answers to this question : http://stackoverflow.com/questions/12279833/videocapture-opencv-2-4-2-error-in-windows also seemed relevant and helpful. – Martin Haeberli Mar 05 '14 at 18:24

9 Answers9

75

Add C:\OpenCV\3rdparty\ffmpeg\ to the Windows PATH environment variable or copy opencv_ffmpeg.dll from that directory to C:\Python27\ or to a directory that is in the PATH. Alternatively, use the OpenCV binaries from http://www.lfd.uci.edu/~gohlke/pythonlibs/#opencv.

Renaming the opencv_ffmpeg.dll file may also be necessary.

For OpenCV version X.Y.Z
opencv_ffmpeg.dll ==> opencv_ffmpegXYZ.dll

For 64-bit OpenCV version X.Y.Z
opencv_ffmpeg.dll ==> opencv_ffmpegXYZ_64.dll

Harsh
  • 166
  • 8
cgohlke
  • 9,142
  • 2
  • 33
  • 36
  • 4
    Thank you! It works, but only if you copy _all_ files from `C:\OpenCV\3rdparty\ffmpeg\`, not just `opencv_ffmpeg.dll`. – Bihaqo Jul 30 '12 at 11:13
  • 2
    See all answers below. It seems that one needs to rename the `opencv_ffmpeg.dll` file to `opencv_ffmpegXYZ.dll` for OpenCV version X.YZ and to `opencv_ffmpegXYZ_64.dll` if one uses the 64 bit version. – emem Nov 03 '14 at 10:29
  • 2
    With OpenCV 2.4.10, adding C:\opencv\sources\3rdparty\ffmpeg to Windows path and renaming opencv_ffmpeg.dll and opencv_ffmpeg_64.dll to opencv_ffmpeg2410.dll and opencv_ffmpeg2410_64.dll it works ! Thanks :) I am now able to open MJPEG video files – vdechef Feb 18 '15 at 07:59
  • Thanks - this works! It's frustrating that this "silently fails" without that DLL copied in. I'd rather instead of a subtle false, a nice big exception "error, Danger Will Robinson, missing dll <...>" and then die. – Danny Staple Dec 13 '15 at 21:26
  • I would recommend the second option (using the OpenCV binaries from http://www.lfd.uci.edu/~gohlke/pythonlibs/#opencv). It's cleaner and pretty straightforward. Thanks for the help. – Victor Oliveira Antonino Dec 13 '16 at 20:29
  • does anyone know how to fix the same on ubuntu operating system. –  Oct 12 '17 at 15:57
29

Copying opencv_ffmpeg.dll wasn't enough for me - I had to rename it to opencv_ffmpeg246.dll, when I used OpenCV 2.4.6 with Python 2.7.5

emem
  • 5,588
  • 1
  • 24
  • 30
10

I first tried copying, then changing the Path environment of windows.. it didn't work until i changed the name to opencv_ffmpeg245.dll for my openCV version 2.4.5 and python 2.7. (notice **245.dll is from opencv version 2.4.5)

SanD
  • 473
  • 7
  • 11
8

For 64 bit versions, rename opencv_ffmpeg_64.dll to opencv_ffmpeg249_64.dll Change 249 to your opencv version.

user3738775
  • 81
  • 1
  • 2
6

For OpenCV 3.0.0 on 64 bit environment copy opencv_ffmpeg_64.dll and rename it to opencv_ffmpeg300_64.dll.

Also add this path to your PATH environment variable to get it work with OpenCV under Java.

masewo
  • 801
  • 9
  • 11
5

Update: This is still true for OpenCV 2.4.9, copy all the files, and rename the dll opencv_ffmpeg249.dll

bw4sz
  • 2,237
  • 2
  • 29
  • 53
  • I can't get it to work for OpenCV 3.0.0-tp2 Windows, any hints for a desperate coder? :) – andli Jun 19 '14 at 19:40
4

I had the same issue with python 2.7 and opencv 2.4.10. I copied the opencv_ffmpeg2410_64.dll file from C:\Applications\opencv\build\x64\vc10\bin into the python path (mine is at C:\Users\mohammad\Anaconda) and it was solved!

3

At first I use windows8.1 32bit and opencv 2.4.10.
I faced same problem to use file test1.wmv which was made by Logicool C270.
I tried to use other file test2.mov which was made by Digital Camera ( Canon SX160 ). So I can play test2.mov to append path C:\opencv\build\x86\vc12\bin.
But I cannot Play test1.wmv.
So I try to change format test1.wmv to test1a.mp4 by using VLC ( to Video for youtube HD) .
Then I can play test1a.mp4.
I tried test1.wmv on Ubuntu 14.04 LTS , then I can play it easy.
It may occur some difference on default ffmpeg binary between windows8.1 and Ubuntu 14.04 LTS.

Lrrr
  • 4,755
  • 5
  • 41
  • 63
user
  • 31
  • 1
0

For anaconda environment copy opencv_ffmpeg249_64.dll from C:\Python27\ rename its version according to your opencv correct version and past it to C:\Users\KHALID\Anaconda2\Library\bin

Khalid Bin Huda
  • 1,583
  • 17
  • 16