0

l try to read the following video, downloaded from http://www.sample-videos.com/

which is http://www.sample-videos.com/video/mp4/720/big_buck_bunny_720p_5mb.mp4

Here is my code :

import cv2

cap = cv2.VideoCapture('big_buck_bunny_720p_5mb.mp4')

if(cap.isOpened()== False):
  print("Error opening video stream or file")


count = 0

while (cap.isOpened()):
    # capture frame by frame :
    ret, frame = cap.read()
    if ret==True:
        # Display the resulting frame
        cv2.imshow('Frame', frame)
        cv2.imwrite("frame%d.jpg" % count, frame)
        count +=1
        print(count)

However l get Error opening video stream or file at cap = cv2.VideoCapture('big_buck_bunny_720p_5mb.mp4') and ret equals False always

My OpenCV version is 3.1.0

Joseph
  • 73
  • 2
  • 9
  • could be lots of things. can you open/stream any file? Where is the file located? Are you pointing to that location, or your default directory in python? – Mohammad Athar Oct 24 '17 at 14:36
  • Yes it's my default directory in python – Joseph Oct 24 '17 at 14:49
  • It has to do with the dll's related to ffmpeg. Please see https://stackoverflow.com/questions/13834399/cannot-open-mp4-video-files-using-opencv-2-4-3-python-2-7-in-windows-7-machi – v.coder Oct 24 '17 at 14:53
  • So the problem is related to the exetension – Joseph Oct 24 '17 at 16:44
  • "My python version is 3.1.0" -- the tag says it's 3.5. Did you mean your OpenCV version is 3.1.0? – Dan Mašek Oct 24 '17 at 16:52
  • Sorry, yes it's my OpenCV VERSION – Joseph Oct 25 '17 at 10:04
  • Make sure you have the right decoder to decode this video in your system and that it's properly registered (i.e. on Windows, its dll folder can be for example added to PATH variable). Mp4 is a container, not format and it can store content written using several different encoders. – Max Walczak Oct 25 '17 at 11:53
  • Its worth checking the simple stuff also - are you 110% sure the path is correct? – Mick Oct 25 '17 at 18:03

2 Answers2

0

There may be the following issue with your machine:

  1. configure the video path
  2. check the permission to access the file
  3. install an additional codec
Skandix
  • 1,916
  • 6
  • 27
  • 36
Gayan Chinthaka
  • 521
  • 6
  • 5
0

You might have installed opencv but there are some prerequisites needs to be installed while reading a .mp4 video file using open cv.

You can verify that by simply reading an .avi format file and .mp4 file

[it could read .avi file but not .mp4 file]

To read a mp4 .file

  1. Install ffmpeg package compiled with H.264 codec:

    H.264/MPEG-4 Part 10 or AVC (Advanced Video Coding) is a standard for video compression, and is currently one of the most commonly used formats for the recording, compression, and distribution of high definition video.
    
    Ref : https://www.debiantutorials.com/how-to-install-ffmpeg-with-h-264mpeg-4-avc/
    

Few suggestions to make sure all prerequisites are available

    1. check ffmpeg package compiled with H.264 is already installed in the machine using the command below. 
    ffmpeg -version 
    2. Installation of open-cv in anaconda will reduce the stress to install ffmpeg package compiled with H.264 
    3. Make sure that the user created in the machine has got enough privilege to read and write in specific application related directories
        a. Check the read and write permission using the command below
            ls -ld <folder-path>
               or
            namei -mo <folder-path>
        b. Alter the access writes based on the user privilege required (sudo access needed else we need to engage admin to alter the permission)
            eg : sudo chmod -R 740 <folder-path>** [ Recursive rwx for user ,r for group ]