Skip to content

Instantly share code, notes, and snippets.

@AnmolTomer
Created October 8, 2019 18:40
Show Gist options
  • Save AnmolTomer/1acdd421041a1260eea6a6aec2c5cfdc to your computer and use it in GitHub Desktop.
Save AnmolTomer/1acdd421041a1260eea6a6aec2c5cfdc to your computer and use it in GitHub Desktop.
#vid_2_frame
import cv2
import os
# <editor-fold defaultstate="collapsed" desc="function: break_into_frames">
def break_into_frames(src_path, dst_path, video_count):
vid = cv2.VideoCapture(src_path)
img_path = dst_path + "\\" + str(video_count) + "_"
count = 0
if not vid.isOpened():
print(f"{src_path}: Error opening video stream or file")
while vid.isOpened():
ret, frame = vid.read()
img_name = img_path + str(count) + ".jpg"
if ret:
cv2.imwrite(img_name, frame)
count += 1
else:
break
vid.release()
# </editor-fold>
if __name__ == '__main__':
source_path = input("Enter the path of the videos: ")
destn_path = input("Enter the path where the frames need to be saved: ")
print("\n")
lst = os.listdir(source_path)
for index,video in enumerate(lst):
if '.mp4' in video[-4:]:
path_of_video = source_path + "\\" + video
break_into_frames(path_of_video, destn_path, index)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment