Problems converting video to pictures using python 2.7


Tried a lot of options and am running out of ideas. I was hoping someone here could help. I am trying to write some code in python that will extract frames (say every tenth frame) from a video (.avi or .wmv) and create a picture (.jpg preferably - but other formats will do). I have had no success and was wondering if someone could assist me in solving my problem by providing an alternative to what I have tried and failed.

I have tried PyMedia (the example in their tutorial does not work - the program bombs out when it looks for the video codecs):

#dm= muxer.Demuxer( inFile.split( '.' )[ -1 ] )  This line does not work

  dm= muxer.Demuxer( 'avi' )  #This modified line does seem to work however

  i= 1

  inFile = "VideoTest.avi"

  f= open( inFile, 'rb' )

  s= 400000 )

  r= dm.parse( s )

  v= filter( lambda x: x[ 'type' ]== muxer.CODEC_TYPE_VIDEO, dm.streams )

  v_id= v[ 0 ][ 'index' ]

  print 'Assume video stream at %d index: ' % v_id

  c= vcodec.Decoder( dm.streams[ v_id ] )     #this is the point where it crashes.

I have tried OpenCV v2.2 for Python but that doesn't work either (I can get most of OpenCV to work - except the one function, CaptureFromFile, that I need does not work). I believe the reason this function doesn't work is because on Windows it requires highGui to operate and for some reason, python and opencv cannot find highgui eventhough it is in the correct directory. I also understand OpenCV has issues with finding and applying correct video codecs so I am not sure which is the cause of my problem.

I have looked at pyFFMPEG but the last build for that was version 2.6 and I am running python 2.7.

I am running this on Windows Vista and Windows 7 machines, have Python 2.7 and OpenCV 2.2 loaded on C:\ and all other python packages (pygames, pymedia, numpy, scipy) installed in C:\python27\Libs\site-packages..." I downloaded and installed from executables, all packages were built for python 2.7. My Path variable includes Python27 and OpenCV and I have a PYTHONPATH variable.

Thanks for any ideas or recommendations.

By : scharbon


I tried the following to extract each frames as a separate image:

import cv2

file_path = "some\path\to\the\file.avi"
video_object = cv2.VideoCapture(path)

success = True
while success:
    success,frame =
    if success:
        cv2.imwrite(path[:-4] "_" str(i) ".jpg",frame)

This script saves each frame as an image in the same folder as the source file. It may not be the most efficient way to do this, but it works for me! I know my string parsing is not the most recommended, but it works. returns two object. The first is a bool indicating whether the reading operation was a success or not, and the second is the image.

There may be limitations with respect to the video codec, of which I am not aware. I'm using Python 2.7 with the most recent version of openCV and Numpy as of 11/7/2013.

This video can help you solving your question :)
By: admin