Home > Software engineering >  How to extract all the frames from a video?
How to extract all the frames from a video?

Time:04-10

Hey so I wanted to know how I can extract all frames from a vide using MoviePy Currently I am doing it in open-cv which is really really slow. My current code :-

vidObj = cv2.VideoCapture("./video.mp4")
count = 0
flag = 1
while flag:
    flag, image = vidObj.read()
    try:
        cv2.imwrite(f"./images/frame{count}.jpg", image)
    except:
        break
    count  = 1

Can someone tell me how to achieve the same in MoviePy please?

CodePudding user response:

You can use the write_images_sequence() method.

d represents a 5 digit frame index.

from moviepy.editor import VideoFileClip

video = VideoFileClip('video.mp4')
video.write_images_sequence('framed.png', logger='bar')

CodePudding user response:

This code help to extract frames from videos . Hope it will work !

import cv2

# Function to extract frames
def FrameCapture(Video_Path, Image_Path):
    # Path to video file
    vidObj = cv2.VideoCapture(Video_Path)

    # Used as counter variable
    count = 0

    # checks whether frames were extracted
    success = 1

    while success:
        # vidObj object calls read
        # function extract frames
        success, image = vidObj.read()
        # Saves the frames with frame-count
        count  = 1
        if count % 1== 0:
            Final_path = (Image_Path "/" str(count) ".jpg")
            print(Final_path)
            cv2.imwrite(Final_path, image)


# Driver Code
if __name__ == '__main__':
    # Calling the function
    Video_Path = r"videopath.mp4"
    Image_Storage_Path = r"Image_folder_path"
    FrameCapture(Video_Path, Image_Storage_Path)
  • Related