1

I'm trying to grab the images from a video file but I can't succeed to open it and I don't know why.

Below is a code sample that print False where I'm expecting to get a True. I don't get why I can't open this simple video file, any lead would be very much appreciated!

I tried with a relative path first then moved to an absolute path to see if anything changed and it's still the same...

video = cv2.VideoCapture()
path = "C:\\Users\\Leo\\Dropbox\\Projet VISORD\\TP3\\video.mpg"
print video.open(path)
Leo
  • 1,129
  • 4
  • 23
  • 38
  • @cgohlke I read it and copied all the files from `3rdparty` to my `python27` folder but it didn't change anything. – Leo Mar 16 '13 at 22:50

3 Answers3

2

The following code works for me:

import cv2

Load the video file:

capture = cv2.VideoCapture('videos/my_video.avi')

Frame is the image you want, flag is success/failure:

flag, frame = capture.read()

Loop through the video's frames:

while True:
    flag, frame = capture.read()
    if flag == 0:
        break
    cv2.imshow("Video", frame)
    key_pressed = cv2.waitKey(10)    #Escape to exit
    if key_pressed == 27:
        break

However, MPEG is a compressed format, which means that you need the correct codecs to be installed and might have to do some more work to handle the conversion. You can read about the supported different types of video formats at the OpenCV VideoCodec documentation.

(However, if you just want a simple working example, try using a .AVI file and see if it works for you.)

hjweide
  • 11,893
  • 9
  • 45
  • 49
  • you're wrong about mpeg. libffmpeg is used by opencv to decompress video. but that, again is dependant on the actual codecs being installed on the machine – berak Mar 18 '13 at 13:57
2

Had a similar problem. Try changing

path = "C:\\Users\\Leo\\Dropbox\\Projet VISORD\\TP3\\video.mpg" 

to

path = "C:/Users/Leo/Dropbox/Projet VISORD/TP3/video.mpg"

and see if it works.

Alexey
  • 5,898
  • 9
  • 44
  • 81
2

The codecs that cv2 supports out of the box are limited. A few of the formats can be found at the link below. I haven't tried them all yet.

http://opencv.willowgarage.com/wiki/documentation/cpp/highgui/VideoWriter

I've had some luck with mp42 codec. Had to convert my camera's mp4 (h264) format to an avi in the correct format.

Using a tool ffmpeg at the moment.

ffmpeg -i input.mp4 -codec:v msmpeg4v2 output.avi

This still leaves something to be desired as it loses resolution, so I am working toward a better solution myself. I only just started at this myself.

Larry
  • 36
  • 1