convert video to set of jpegs in python

To convert a video into a set of JPEG images in Python, we can use the OpenCV library. Here is a sample code that does this:

main.py
import cv2

# Open the video file
video_file = 'path/to/video/file.mp4'
video = cv2.VideoCapture(video_file)

# Define the frame index
frame_index = 0

while True:
    # Read a frame from the video
    ret, frame = video.read()

    # Break the loop if the video is over
    if not ret:
        break

    # Save the frame as a JPEG image
    frame_file = f'path/to/output/folder/frame_{frame_index:05d}.jpg'
    cv2.imwrite(frame_file, frame)

    # Increment the frame index
    frame_index += 1

# Release the video file handle
video.release()
547 chars
27 lines

In this code, we first open the video file using the cv2.VideoCapture method. We then define a frame index variable and start a loop to read each frame from the video. For each frame, we save it as a JPEG image using the cv2.imwrite method. Finally, we release the video file handle using the video.release method.

gistlibby LogSnag