cv_play_video.py

Code with fix (setting a video property) - Julien Rebetez, 2012-03-15 02:45 pm

Download (1 kB)

 
1
"""
2
This is a simple program to play a video using Opencv. This actually demonstrate
3
some problems. For example, run this program on
4
/home/julien/work/cmu_data/data/S1_brownies/videos/videos/out6510211.avi
5

6
And play this file in VLC as well.
7

8
At the beginning, you'll notice that the opencv version skip some frames when
9
the light is turned on (if we actually write all frames to disk, it turns
10
out these frames are the last one fetched by opencv for some reason).
11

12
So OpenCV doesn't seem really reliable to play video...
13
"""
14
import sys
15
import os
16
import cv2
17
18
def main(videofile):
19
  print "Playing %s", videofile
20
  assert os.path.exists(videofile)
21
22
  capture = cv2.VideoCapture(videofile)
23
  # This is a CRUCIAL LINE. Without it, OpenCV will start playing at some random
24
  # position
25
  capture.set(cv2.cv.CV_CAP_PROP_POS_FRAMES, 0)
26
  (status, frame) = capture.read()
27
28
  while frame is not None:
29
    cv2.imshow("Frame", frame)
30
    key = cv2.waitKey()
31
    if key == 27:
32
      break
33
    (status, frame) = capture.read()
34
35
if __name__ == "__main__":
36
  main(sys.argv[1])