# CV2 is not read video file

**URL:** <https://forum.opencv.org/t/cv2-is-not-read-video-file/3550>\
**Category:** Uncategorized\
**Tags:** videoio\
**Created:** [May 27, 2021, 6:43am UTC](https://forum.opencv.org/t/cv2-is-not-read-video-file/3550 "2021-05-27T06:43:43Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![MohitSharma](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/mohitsharma/32/2228_2.png) [@MohitSharma](https://forum.opencv.org/u/MohitSharma)\
**Post date:** [May 27, 2021, 6:43am UTC](https://forum.opencv.org/t/cv2-is-not-read-video-file/3550/1 "2021-05-27T06:43:43Z")

</div>

```auto
# #code for sampling image from the video @ 5fps
# #to be used only once for sampling video
import cv2
import math # for mathematical operations
import matplotlib.pyplot as plt # for plotting the images

count = 0
videoFile = '../Test Tom and Jerry.mp4'
cap = cv2.VideoCapture(videoFile) # capturing the video from the given path
frameRate = cap.get(5) #frame rate
print(frameRate)
x=1

while(cap.isOpened()):
  print(True)
  frameId = cap.get(1) #current frame number
  ret, frame = cap.read()
  if (ret != True):
      break
  if (frameId % math.floor(frameRate) == 0):
      filename ="\Train\test%d.jpg" % count;count+=1
      cv2.imwrite(filename, frame)
      print("*", end="")
    
cap.release()
cv2.destroyAllWindows()
print ("Done!")

```

cap.isOpened() is always false for some reason.

---

<div class="post-metadata">

**Author:** ![crackwitz](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/crackwitz/32/14_2.png) [@crackwitz](https://forum.opencv.org/u/crackwitz)\
**Post date:** [May 28, 2021, 12:36am UTC](https://forum.opencv.org/t/cv2-is-not-read-video-file/3550/2 "2021-05-28T00:36:22Z")

</div>

please run and post the output of:

`print(cv2.getBuildInformation())`

---

<div class="post-metadata">

**Author:** ![furas](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/furas/32/706_2.png) [@furas](https://forum.opencv.org/u/furas)\
**Post date:** [June 5, 2021, 11:44am UTC](https://forum.opencv.org/t/cv2-is-not-read-video-file/3550/3 "2021-06-05T11:44:55Z")

</div>

First you could try with full path

```auto
videoFile = "/full/path/to/Test Tom and Jerry.mp4"

```

because code may run in different folder then you expect and then relative path may not find file. And `cv2` doesn’t raise error when it can’t read file but it returns `False` in `cap.isOpened()` and in `ret, frame = cap.read()`

You can check `"Current Working Directory"` with

```auto
import os

print( os.getcwd() )

```

* * *

You could also check if you have problem with other files and with different compressions. This way you can recognize if this is not problem with codec.

* * *

If you run code in script (not in Jupyter, IPython or other console) then you can get full path to folder with your script and use it to generate full path to other files

```auto
import os

BASE = os.path.dirname(os.path.abspath( __file__ ))

videoFile = os.path.join(BASE, "../Test Tom and Jerry.mp4")

```

* * *

**By the way:**

`"\t"` has special meaning in string - it means key `<tab>` - so path `"\Test\test%d.jpg"` means `"\Test<tab>est%d.jpg"`. Use `/` instead of `\` like `/Test/test%d.jpg` or double `\\` like `"\\Train\\test%d.jpg"`or use prefix `r` for `"raw"` string like `r"\Train\test%d.jpg"`
