Watch Video in Python with OpenCV


OpenCV is a widely-used resource for video processing as well as image processing.  After compiling and installing OpenCV for the first time, I wanted to figure out how to read from an existing video file on my hard drive and display it on my screen.  As usual in video processing, the way to do this is to read each frame of the video one-at-a-time, and display each as an image in sequence.  To understand the basics of showing images, check out my tutorial: Viewing Images in Python with OpenCV.

Here I'll assume you have a short (<1 minute) video file available on your hard drive.  If not, I recommend downloading the trailer for the awesome open-source animated movie Sintel.  The MPEG format 480p version is more than enough to demonstrate the basic capabilities of OpenCV's video processing.

Note that OpenCV supports reading and writing several popular formats (such as AVI and MPEG). It requires the FFMPEG open-source video processing library to make this magic happen, so make sure you've installed this as well (Google around for basic instructions).


This code reads in a given video from file, determines the total number of frames as well as the frame rate, and uses these parameters to display each extracted frame on the screen for the appropriate length of time.

To use: Copy/paste this code into a text file.  Call it something like "" and execute as a script (no args necessary).  Make sure to adjust the filepath to point to a valid video file on your local filesystem.

import cv

vidFile = cv.CaptureFromFile( '/home/mhughes/sintel_trailer-480p.mp4' )

nFrames = int(  cv.GetCaptureProperty( vidFile, cv.CV_CAP_PROP_FRAME_COUNT ) )
fps = cv.GetCaptureProperty( vidFile, cv.CV_CAP_PROP_FPS )
waitPerFrameInMillisec = int( 1/fps * 1000/1 )

print 'Num. Frames = ', nFrames
print 'Frame Rate = ', fps, ' frames per sec'

for f in xrange( nFrames ):
  frameImg = cv.QueryFrame( vidFile )
  cv.ShowImage( "My Video Window",  frameImg )
  cv.WaitKey( waitPerFrameInMillisec  )

# When playing is done, delete the window
#  NOTE: this step is not strictly necessary, 
#         when the script terminates it will close all windows it owns anyways
cv.DestroyWindow( "My Video Window" )


You should get a nice video playing in its own window on your Desktop, if everything runs smoothly.


Depending on how you installed FFMPEG, you may see a Warning message that looks something like this.

[swscaler @ 0x96c41f0] No accelerated colorspace conversion found.

What seems to be happening here is that FFMPEG was not configured with advanced features that allow fancy acceleration of colorspaces.  This doesn't seem to impact basic performance, but could mean that your video quality could be improved by reinstalling FFMPEG with more advanced options.  Google searching should lead you to a number of sites that recommend fixes to this exact issue, but I haven't verified any so I won't endorse one here yet.

Mostly I found this annoying, since it displays every time I show a different frame in a video.  You can always redirect stderr if this bothers you.