Spaces:
Running
Running
File size: 954 Bytes
f1bbd74 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
import cv2
import os
import glob
def extract_frames_from_videos(input_folder, output_folder):
os.makedirs(output_folder, exist_ok=True)
video_files = glob.glob(os.path.join(input_folder, "*.mp4")) # Modify if using other formats
image_count = 0 # To maintain sequential numbering across videos
for video_file in video_files:
cap = cv2.VideoCapture(video_file)
success, frame = cap.read()
while success:
image_path = os.path.join(output_folder, f"{image_count}.jpg")
cv2.imwrite(image_path, frame)
image_count += 1
success, frame = cap.read()
cap.release()
print(f"Extraction complete. {image_count} images saved in {output_folder}")
# Example usage
input_folder = "videos" # Change this to the path where your videos are stored
output_folder = "output_images"
extract_frames_from_videos(input_folder, output_folder)
|