This Python script is a versatile tool for extracting high-quality screenshots from YouTube videos, local video files, or any other video source you can think of! It's particularly useful for preparing datasets for machine learning projects, such as training Loras or checkpoints, or just for grabbing that perfect frame from your favorite movie.
requirements.txt
Clone this repository:
git clone https://github.com/EnragedAntelope/youtube-screenshot-extractor.git
cd youtube-screenshot-extractor
Install the required dependencies:
pip install -r requirements.txt
Install FFmpeg (required for keyframe extraction and some filters):
(Optional) Install PyCUDA for GPU acceleration:
pip install pycuda>=2022.1
Note: PyCUDA requires CUDA to be installed on your system. For installation instructions, refer to the PyCUDA documentation.
Basic syntax:
python youtube-screenshot-script.py [SOURCE] [OPTIONS]
Where [SOURCE]
can be either a YouTube URL or a path to a local video file.
Note: If you plan to use GPU acceleration (--use-gpu option), make sure you have PyCUDA installed.
Extract frames every 5 seconds from a YouTube video and save them to a custom folder:
python youtube-screenshot-script.py https://www.youtube.com/watch?v=dQw4w9WgXcQ --output my_awesome_screenshots
It's that easy! Now let's dive into all the configurable options.
--method {interval,all,keyframes,scene}
: Frame extraction method (default: interval)--interval INTERVAL
: Interval between frames in seconds (default: 5.0, only used with 'interval' method)--quality QUALITY
: Quality threshold for frame selection (0-100, default: 12.0)--blur BLUR
: Blur threshold for frame selection (default: 10.0)--detect-watermarks
: Enable basic watermark detection--watermark-threshold THRESHOLD
: Watermark detection sensitivity (0-1, default: 0.8)--max-resolution RESOLUTION
: Maximum resolution for YouTube video download (e.g., 720, 1080). Ignored for local files.--output OUTPUT
: Custom output folder name--png
: Save frames as PNG instead of JPG--disable-parallel
: Disable parallel processing of frames--use-gpu
: Use GPU acceleration if available--fast-scene
: Use fast mode for scene detection (less accurate results)--resume
: Resume an interrupted extraction process--thumbnail
: Generate a thumbnail montage of extracted frames--verbose
: Enable detailed logging--dry-run
: Show what would be done without actually processing--config CONFIG
: Load settings from a configuration file--gradfun
: Apply gradfun filter to reduce color banding (less aggressive, preserves more detail)--deblock
: Apply deblocking filter to reduce compression artifacts--deband
: Apply debanding filter to reduce color banding (more aggressive, better for severe banding)Extract keyframes from a local video file:
python youtube-screenshot-script.py path/to/your/video.mp4 --method keyframes
Use scene detection on a YouTube video with custom output folder:
python youtube-screenshot-script.py https://www.youtube.com/watch?v=dQw4w9WgXcQ --method scene --output my_scene_shots
Extract frames every 10 seconds with a lower quality threshold and PNG output:
python youtube-screenshot-script.py https://www.youtube.com/watch?v=dQw4w9WgXcQ --interval 10 --quality 30 --png
Use GPU acceleration and generate a thumbnail montage:
python youtube-screenshot-script.py path/to/your/video.mp4 --use-gpu --thumbnail
Download a YouTube video at a maximum resolution of 720p and extract frames:
python youtube-screenshot-script.py https://www.youtube.com/watch?v=dQw4w9WgXcQ --max-resolution 720
Extract frames with post-processing filters:
python youtube-screenshot-script.py path/to/your/video.mp4 --gradfun --deblock --deband
Processing Time:
Video Resolution:
--max-resolution
option to limit the download quality if needed.--max-resolution
option is ignored.Output: Frames are saved as JPG or PNG images in the specified output folder (or a default folder named after the video title).
Frame Filenames: Follow the format frame_NNNNNN_qXX_bYY[_watermarked].(jpg|png)
, where:
NNNNNN
: Frame numberXX
: Quality score (0-99, higher is better)YY
: Blur score (higher numbers indicate less blur)_watermarked
: Suffix added if a watermark is detectedPost-processing Filters:
--dry-run
option to preview the extraction process without actually saving frames.--resume
option in case the process is interrupted.--max-resolution
option to limit download quality for faster processing and to conserve storage space.
--quality 30 --blur 50
) and adjust as needed.--fast-scene
option for quicker (but less accurate) scene detection results.No frames extracted:
--quality 20 --blur 30
).Keyframe extraction issues:
Scene detection extremely slow or crashing:
--fast-scene
option for quicker but less accurate results.GPU acceleration not working:
pip install pycuda
).--use-gpu
flag.Watermark detection producing false positives:
--watermark-threshold 0.9
for stricter detection).--detect-watermarks
flag.Process dies unexpectedly for large videos:
--resume
option to continue from where it left off.Low quality or blurry output:
Post-processing filters not working:
This project is licensed under the MIT License - see the LICENSE file for details.
Contributions are welcome! Please feel free to submit a Pull Request.