Skip to content

Instantly share code, notes, and snippets.

@steven-mi
Created April 9, 2019 12:16
Show Gist options
  • Save steven-mi/6bc101e198cfc95e3b79752298933c30 to your computer and use it in GitHub Desktop.
Save steven-mi/6bc101e198cfc95e3b79752298933c30 to your computer and use it in GitHub Desktop.
convert a video to images with opencv
import cv2
# open video
video = cv2.VideoCapture('video.mp4')
# get fps and duration
fps = video.get(cv2.CAP_PROP_FPS) # OpenCV2 version 2 used "CV_CAP_PROP_FPS"
frameCount = int(video.get(cv2.CAP_PROP_FRAME_COUNT))
duration = frameCount/fps
# print details
print('fps = ' + str(fps))
print('number of frames = ' + str(frameCount))
print('duration (S) = ' + str(duration))
minutes = int(duration/60)
seconds = duration%60
print('duration (M:S) = ' + str(minutes) + ':' + str(seconds))
# which ... frame will be saved - in our case every 60th frame
SAVE_EACH___FRAME = 60
for count in range(frameCount):
if count % SAVE_EACH___FRAME == 0:
success, image = video.read()
cv2.imwrite("img/frame%d.jpg" % count, image)
print("Saved frame%d.jpg" % count)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment