3

I am trying open *.mp4 file with opencv. I am trying with following code but I am unable to do this. How can solve this issue?

import cv2
cap = cv2.VideoCapture("test.mp4")
# cap = cv2.VideoCapture("test.avi") # it's also not working
cap.isOpened() # Output: False
  • N.B:

    • OS : ubuntu 16.04
    • OpenCv version: 3.3.0
Md. Rezwanul Haque
  • 2,882
  • 7
  • 28
  • 45
  • take a look [here](http://answers.opencv.org/question/9692/mp4-reading/) most likely you are missing a codec for mp4 –  Mar 15 '18 at 12:21
  • Please check everything listed in the linked answer. If it doesn't work, let me know and I'll reopen the question – Miki Mar 15 '18 at 12:33
  • @Miki , I tried to solve this `linked answer` but it doesn't work.. – Md. Rezwanul Haque Mar 15 '18 at 12:52
  • Please explain why the [linked answer](https://stackoverflow.com/questions/47112642/permanent-fix-for-opencv-videocapture) doesn't work – Miki Mar 15 '18 at 12:54
  • After executing this `ffmpeg -codecs | grep -i avc` I got this ` DEV.LS h264 H.264 / AVC / MPEG-4 AVC / MPEG-4 part 10 (decoders: h264 h264_crystalhd h264_vdpau ) (encoders: libx264 libx264rgb ) D.A.L. avc On2 Audio for Video Codec (decoders: on2avc ) ` – Md. Rezwanul Haque Mar 15 '18 at 12:56
  • python -c "import cv2; print(cv2.getBuildInformation())" | grep -i ffmpeg FFMPEG: NO – Md. Rezwanul Haque Mar 15 '18 at 12:57
  • _"If the output is No then follow an online guide to build OpenCV from source with ffmpeg support."_ – Miki Mar 15 '18 at 13:00
  • @Miki Where I will get `online guide to build OpenCV` ? – Md. Rezwanul Haque Mar 15 '18 at 13:02

4 Answers4

2

I don't know if this post up-to-date. However, I had the same problem on Ubuntu 18.4 and had installed the opencv via conda:

conda install --channel https://conda.anaconda.org/menpo opencv3

And it turned out that menpo is not supported anymore. After installing from conda-forge, the problem disappeared:

conda install -c conda-forge opencv

You can surely check if the mpeg support is there:

python -c "import cv2; print(cv2.getBuildInformation())" | grep -i ffmpeg

it should return "YES"

2

install this package, it enable ffmpeg in opencv:

pip install opencv-contrib-python 
1

Install this package

pip install VideoCapture

import cv2
cap = cv2.VideoCapture("test.mp4")
# cap = cv2.VideoCapture("test.avi")
cap.isOpened() 

cap.release()
-1

try updating opencv. also, this might help: Cannot open ".mp4" video files using OpenCV 2.4.3, Python 2.7 in Windows 7 machine