Ask Your Question

cv2.VideoCapture: Cannot read from file

asked 2012-08-31 09:54:08 -0500

slychief gravatar image


I try to process videos using the Python interface. I downloaded OpenCV 2.4.2 and copied the file to the Python27\Lib\site-packages folder on Windows 7.

Using the following code, I'm not able to read video files. capture.isOpened() always returns false. Reading images from Webcam using cv2.VideoCapture(0) works fine, but not from files.

Did I miss to copy some importtant files to the Python directory?

import cv2

if __name__ == '__main__':

    capture = cv2.VideoCapture("test.mpg")

    if capture.isOpened():
            print("Device Opened\n")
            print("Failed to open Device\n")

    (ret, imageMat) =

Cheers, Sly

edit retag flag offensive close merge delete


How you run your app? Try absolute path for test.

Victor1234 gravatar imageVictor1234 ( 2012-09-02 07:10:17 -0500 )edit

is your test.mpg in the same folder of your code?

Mahdi gravatar imageMahdi ( 2012-11-26 14:29:09 -0500 )edit

I'm having the same issue: VideoCapture works well with webcam, but no success with files - absolute path and otherwise.

I tried the solutions from here:, but it still doesn't work. I added the folder to the PATH, and copied the dlls around.

errollw gravatar imageerrollw ( 2013-01-09 18:13:15 -0500 )edit

Did you ever find a solution to this?

loughnane gravatar imageloughnane ( 2013-08-13 14:45:53 -0500 )edit

There might be a problem with the ffmpeg dll that is being used. Which build are you following? x86 or x86_64?

prakharmohan gravatar imageprakharmohan ( 2014-02-18 03:54:48 -0500 )edit

DId anyone get the solution of this problem? I am facing the same problem for so many days and still couldn't fix it

AnjaliChanglani gravatar imageAnjaliChanglani ( 2015-02-18 07:47:38 -0500 )edit

Did anyone solved it? I am having the same problem. After added path and copied dll still not working. I am on win 8; 64x.

Vashaman gravatar imageVashaman ( 2017-02-23 07:58:35 -0500 )edit

This post is too old you should ask a new question

LBerger gravatar imageLBerger ( 2017-02-23 08:08:10 -0500 )edit

2 answers

Sort by ยป oldest newest most voted

answered 2015-07-17 12:16:33 -0500

wunjo gravatar image

Try including the full file path in cv2.VideoCapture and be sure to change the "\" to "/". OpenCV doesn't like the forward slashes for some reason. Example: cv2.VideoCapture('C:/Project/Videos/test.mpg')Also, see my answer here for more clarification on proper codecs to use: ( Hope this helps!

edit flag offensive delete link more

answered 2018-10-24 01:41:55 -0500

I was getting the same error while using opencv in anaconda3 virtual environment. I checked the buildinformation (cv2.getBuildInformation()) for current opencv version and ffmpeg wasn't marked "yes". To resolve this

  1. I uninstalled opencv from my conda environment ( conda uninstall opencv)

  2. Installed latest ffmpeg using conda-forge channel

  3. Then installed opencv again using conda-forge channel ( conda install -c conda-forge opencv) . Don't install using menpo channel

Doing this resolved the issue for me.

edit flag offensive delete link more
Login/Signup to Answer

Question Tools



Asked: 2012-08-31 09:54:08 -0500

Seen: 24,511 times

Last updated: Jul 17 '15