A simple Python utility for extracting image datasets from video collections. Instead of manually scrubbing through videos and taking screenshots, this script automatically selects a number of random frames from every video in a specified folder and saves them as individual JPG images.
Useful for building training datasets for style LoRAs, character concepts, environments, architecture, cinematic aesthetics, and general visual references.
Each video is processed independently, with randomly selected frames distributed across its entire duration. The extracted images are saved using the original video filename as a prefix, making it easy to identify which video each image came from.
Features
Batch processing of an entire folder of videos.
Random frame selection from each video.
Adjustable number of extracted frames per video.
Supports MP4, MOV, MKV, AVI, WEBM, and M4V.
Automatically creates the output folder if it doesn't exist.
Saves extracted frames as JPG images.
No GUI or complicated workflow required.
Installation and setup guide
1. Install Python
You need Python installed on your system. If you already use ComfyUI or other Python-based tools, you probably have Python installed, but you can also use your existing Python installation.
2. Install the required dependency
The script uses OpenCV to read videos and extract frames.
Open CMD or PowerShell and run:
pip install opencv-pythonThat's the only external Python package required.
3. Configure the script
Open extract.py in Notepad or any code editor. At the top, locate the SETTINGS section:
VIDEO_FOLDER = r" Your folder containing the videos"
OUTPUT_FOLDER = r" Your Output folder "
FRAMES_PER_VIDEO = 20Change these values to your preferred folders and extraction settings.
VIDEO_FOLDER
The directory containing your source videos.
OUTPUT_FOLDER
Where the extracted frames will be saved. The script automatically creates this folder if it doesn't already exist.
4. Run the script
Save your changes and run Run Extract.bat
How frame selection works
The script randomly selects frames from the entire duration of each video, without selecting the same frame twice within that video.
Selected frames are then processed in chronological order and saved as JPG files.
A small clarification:
This is random frame sampling, not fixed-interval extraction. It doesn't extract every 10th or 20th frame. Random sampling is useful when you want a varied representation of a video's visual content without having to process every frame.
