How to do it...

The steps for this recipe are:

  1. First, let's create a VideoCapture object and obtain the total number of frames:
import cv2capture = cv2.VideoCapture('../data/drop.avi')frame_count = int(capture.get(cv2.CAP_PROP_FRAME_COUNT))print('Frame count:', frame_count)
  1. Get the total number of frames:
print('Position:', int(capture.get(cv2.CAP_PROP_POS_FRAMES)))_, frame = capture.read()cv2.imshow('frame0', frame)
  1. Note that the capture.read method advances the current video position one frame forward. Get the next frame:
print('Position:', capture.get(cv2.CAP_PROP_POS_FRAMES))_, frame = capture.read()cv2.imshow('frame1', frame)
  1. Let's jump to frame position 100:
capture.set(cv2.CAP_PROP_POS_FRAMES, 100)print('Position:', int(capture.get(cv2.CAP_PROP_POS_FRAMES))) ...

Get OpenCV 3 Computer Vision with Python Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.