Skip to content

Instantly share code, notes, and snippets.

@TutorialDoctor
Created November 27, 2020 14:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save TutorialDoctor/5b13514201d1f120b11c3c85c8383646 to your computer and use it in GitHub Desktop.
Save TutorialDoctor/5b13514201d1f120b11c3c85c8383646 to your computer and use it in GitHub Desktop.
Convert MP4 video to a PNG sequence
# This code is not mine:
# https://www.geeksforgeeks.org/extract-images-from-video-in-python/
# pip3 install opencv-python
# Importing all necessary libraries
import cv2
import os
# Read the video from specified path
cam = cv2.VideoCapture(os.path.abspath("local_video.MP4"))
try:
# creating a folder named data
if not os.path.exists('data'):
os.makedirs('data')
# if not created then raise error
except OSError:
print ('Error: Creating directory of data')
# frame
currentframe = 0
while(True):
# reading from frame
ret,frame = cam.read()
if ret:
# if video is still left continue creating images
name = './data/frame' + str(currentframe) + '.jpg'
print ('Creating...' + name)
# writing the extracted images
cv2.imwrite(name, frame)
# increasing counter so that it will
# show how many frames are created
currentframe += 1
else:
break
# Release all space and windows once done
cam.release()
cv2.destroyAllWindows()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment