5 # import the necessary things for OpenCV
7 from opencv import highgui
9 # the codec existing in cvcapp.cpp,
10 # need to have a better way to specify them in the future
11 # WARNING: I have see only MPEG1VIDEO working on my computer
14 MSMPEG4V3 = 0x33564944
16 MSMPEG4V2 = 0x3234504D
18 MPEG1VIDEO = 0x314D4950
23 #############################################################################
24 # so, here is the main part of the program
26 if __name__ == '__main__':
29 print "OpenCV Python capture video"
31 # first, create the necessary window
32 highgui.cvNamedWindow ('Camera', highgui.CV_WINDOW_AUTOSIZE)
34 # move the new window to a better place
35 highgui.cvMoveWindow ('Camera', 10, 10)
38 # try to get the device number from the command line
39 device = int (sys.argv [1])
41 # got it ! so remove it from the arguments
43 except (IndexError, ValueError):
44 # no device number on the command line, assume we want the 1st device
47 if len (sys.argv) == 1:
48 # no argument on the command line, try to use the camera
49 capture = highgui.cvCreateCameraCapture (device)
51 # we have an argument on the command line,
52 # we can assume this is a file name, so open it
53 capture = highgui.cvCreateFileCapture (sys.argv [1])
55 # check that capture device is OK
57 print "Error opening capture device"
60 # capture the 1st frame to get some propertie on it
61 frame = highgui.cvQueryFrame (capture)
63 # get size of the frame
64 frame_size = cv.cvGetSize (frame)
66 # get the frame rate of the capture device
67 fps = highgui.cvGetCaptureProperty (capture, highgui.CV_CAP_PROP_FPS)
69 # no fps getted, so set it to 30 by default
73 writer = highgui.cvCreateVideoWriter ("captured.mpg", MPEG1VIDEO,
74 fps, frame_size, True)
76 # check the writer is OK
78 print "Error opening writer"
84 # 1. capture the current image
85 frame = highgui.cvQueryFrame (capture)
87 # no image captured... end the processing
90 # write the frame to the output file
91 highgui.cvWriteFrame (writer, frame)
93 # display the frames to have a visual output
94 highgui.cvShowImage ('Camera', frame)
97 k = highgui.cvWaitKey (5)
100 # user has press the ESC key, so exit
103 # end working with the writer
104 # not working at this time... Need to implement some typemaps...
105 # but exiting without calling it is OK in this simple application
106 #highgui.cvReleaseVideoWriter (writer)