Ask Your Question
0

Mp4 reading

asked 2013-03-20 03:09:52 -0600

nhqazi gravatar image

updated 2013-03-20 03:36:41 -0600

hi I am new o opencv with python. I have installed OpenCV 2.4.1 and python 2.7 . I also make a short program that reads the avi file successfully. However it fails to read mp4 file. Below is my program . Will appreciate any suggestion.

import cv2
import cv2.cv as cv
c= cv2.VideoCapture("f:\\aviwork\\Example2.Mp4")
print c.grab() // returns false. however returns true if use avi file.

cap = cv.CaptureFromFile("f:\\aviwork\\Example2.Mp4")

nframes=int(cv.GetCaptureProperty(cap,cv.CV_CAP_PROP_FRAME_COUNT))
fps= int(cv.GetCaptureProperty(cap,cv.CV_CAP_PROP_FPS))
print "total frame",cv.GetCaptureProperty(cap,cv.CV_CAP_PROP_FRAME_COUNT)
print "fps" ,fps
print " currpos of videofile",cv.GetCaptureProperty(cap,cv.CV_CAP_PROP_POS_MSEC)
waitpermillisecond=int(1*1000/fps)
print "waitpermillisecond",waitpermillisecond
print cv.GetCaptureProperty(cap,cv.CV_CAP_PROP_FOURCC)

for f in xrange(nframes):
    frameimg=cv.QueryFrame(cap)
    print " currpos of videofile",cv.GetCaptureProperty(cap,cv.CV_CAP_PROP_POS_MSEC)
    print " index of frame",cv.GetCaptureProperty(cap,cv.CV_CAP_PROP_POS_FRAMES)
    cv.ShowImage("hcq",frameimg)
    cv.WaitKey(1)

cv.DestroyAllWindows("hcq")
edit retag flag offensive close merge delete

Comments

1

I had same problem before, have you try install ffmpeg or copy all file in c:\opencv\sources\3rdparty\ffmpeg to c:\python?

Shiloh gravatar imageShiloh ( 2014-07-29 05:07:49 -0600 )edit

1 answer

Sort by ยป oldest newest most voted
0

answered 2013-03-20 03:43:36 -0600

Actually if it returns a false, than it means that your system/openCV does not have a codec available to decode mp4 videos. It doesn't support the format. Going with AVI or other decoding format is the way to go if you want it to work.

But if I am correct, it also depends on the codecs you have installed on your computer. Check if you actually have mp4 codecs on your system.

edit flag offensive delete link more

Comments

Thanks for the reply. I checked the file , i can play it with media player or vlc player installed on my system.do i need to install any other thing inorder to run it from my code.how can i check if my opencv support for codec.

nhqazi gravatar imagenhqazi ( 2013-03-20 03:49:11 -0600 )edit

Basically, when reading through the documentation, it seems that you have to grab and retrieve a frame if you want to do it this way. In the retrieve you can then specify the decoding options. I use in C++ code this line of code to display a frame, which handles all decoding in the background basically. Can you try this?

cap = cv2.VideoCapture("f:\aviwork\Example2.mp4") --> also please remove the capitals here

Mat frame; cap >> frame;

StevenPuttemans gravatar imageStevenPuttemans ( 2013-03-20 04:26:51 -0600 )edit

thanks. however i checked Mat type is not supported in python 2.7 i think with the above line u want to redirect cap to the frame. may i ask what actually u want to check.

nhqazi gravatar imagenhqazi ( 2013-03-20 04:48:10 -0600 )edit

Basically, I want to see if you let openCV choose his decoder, if it will read the video. On the other hand, care to share the mp4 file? Place a link and I will see if I have same problem over here.

StevenPuttemans gravatar imageStevenPuttemans ( 2013-03-20 04:53:06 -0600 )edit

can u pass me ur email so that i can pass u the video as i do nt find any upload link here. secondly can u please also write more how to incorporate the decoding option while reading the frame. i will check how it is done in python.

nhqazi gravatar imagenhqazi ( 2013-03-20 05:01:03 -0600 )edit

Upload the file using a service like : http://www.2shared.com/

For the decoding, I know when writing video in OpenCV u need to assing a fourcc code to identify the codec. I should check if you can set that one when reading.

Basically try these steps

  • Create a VideoCapture element: VideoCapture cap;
  • Set the property for the capturing codec : cap.set(CV_CAP_PROP_FOURCC, CV_FOURCC('D','I','V','4'))
  • Then open the file : cap.open(path);

This way you define the DivX mpeg4 codec should be used to read the mp4 file.

StevenPuttemans gravatar imageStevenPuttemans ( 2013-03-20 05:20:57 -0600 )edit

thank you i got it. i did nt set this code i have just read this property using the get method. however please excuse my limited knowledge in this field. what code should i give when i want the pencv should read mp4

nhqazi gravatar imagenhqazi ( 2013-03-20 05:36:12 -0600 )edit

MP4 shows that you are using MPEG-4 coding for your video. There are several MPEG-4 codecs available, including the divx version, which can be coded like I said. However other possibilities are

  • CV_FOURCC('D','I','V','4')
  • CV_FOURCC('3','I','V','2')
  • CV_FOURCC('D','A','V','C')

Look for more information on : http://www.fourcc.org/codecs.php

StevenPuttemans gravatar imageStevenPuttemans ( 2013-03-20 05:43:42 -0600 )edit

Following ur suggestion i run this code without any success as it returns false import cv2 import cv2.cv as cv cap=cv2.VideoCapture() cap.set(cv.CV_CAP_PROP_FOURCC, cv.CV_FOURCC('M','P','4','2')) c= cv2.VideoCapture("f:\aviwork\Example2.Mp4") print c.grab() , i did nt use open method assuming its C code.

nhqazi gravatar imagenhqazi ( 2013-03-20 07:57:02 -0600 )edit

actually i also used open method and checked it it returns false, here is the code: import cv2 import cv2.cv as cv cap=cv2.VideoCapture() cap.set(cv.CV_CAP_PROP_FOURCC, cv.CV_FOURCC('D','I','V','4')) cwi=cap.open("f:\aviwork\Example2.Mp4") print "cwi",cwi

nhqazi gravatar imagenhqazi ( 2013-03-20 08:11:07 -0600 )edit

Question Tools

2 followers

Stats

Asked: 2013-03-20 03:09:52 -0600

Seen: 35,663 times

Last updated: Mar 20 '13