venpo045 venpo045 - 1 year ago 241
Python Question

Extracting image from video at a given time using OpenCV

My task is to make a utility that can take a video and time in seconds.

The utility should write out jpeg images from the video with the given input.

E.g. let the video name be

and time be supplied to the tool as
seconds. The utility should write out image from video @ 20th second.

# Import the necessary packages
import argparse
import cv2

vidcap = cv2.VideoCapture('Wildlife.mp4')
success,image =
count = 0;
while success:
success,image =
cv2.imwrite("frame%d.jpg" % count, image) # save frame as JPEG file
if cv2.waitKey(10) == 27: # exit if Escape is hit
count += 1

The above code gives all frames of the entire video, my concern is how can I pass time and get the frame at the specified time?

Answer Source

why don't you just do, what @micka proposed ?

import cv2

vidcap = cv2.VideoCapture('d:/video/keep/Le Sang Des Betes.mp4')
vidcap.set(cv2.CAP_PROP_POS_MSEC,20000)      # just cue to 20 sec. position
success,image =
if success:
    cv2.imwrite("frame20sec.jpg", image)     # save frame as JPEG file