This Python script extracts all frames from an MP4 video using ffmpeg and then creates a zip file containing all the extracted PNG frames.
- Python 3.6+
- ffmpeg installed and available in your system PATH
- Built-in Python modules:
zipfile,os,subprocess,argparse,tempfile,shutil
-
Install ffmpeg (if not already installed):
- macOS:
brew install ffmpeg - Ubuntu/Debian:
sudo apt install ffmpeg - Windows: Download from ffmpeg.org
- macOS:
-
Download the script:
# Make the script executable chmod +x extract_frames.py
Run the script and it will prompt you for the video path:
python extract_frames.pyThe script will:
- Ask you to enter the path to your MP4 video file
- Validate the file exists and has the correct extension
- Extract all frames from the video
- Save them as PNG files in a temporary directory
- Create a zip file named
frames.zipcontaining all frames - Clean up the temporary files
python extract_frames.py -o my_frames.zip
# Then enter: /path/to/video.mp4# Extract 1 frame per second
python extract_frames.py -r 1
# Then enter: /path/to/video.mp4
# Extract 10 frames per second
python extract_frames.py -r 10
# Then enter: /path/to/video.mp4python extract_frames.py --keep-frames
# Then enter: /path/to/video.mp4python extract_frames.py -d /path/to/temp/dir
# Then enter: /path/to/video.mp4-o, --output: Output zip file path (default:frames.zip)-d, --temp-dir: Temporary directory for frames (default: auto-generated)-r, --frame-rate: Frame rate for extraction (e.g., 1 for 1 fps)--keep-frames: Keep temporary frame files after zipping
Note: The video file path is now entered interactively when the script runs, rather than as a command line argument.
python extract_frames.py
# Then enter: /path/to/sample_video.mp4python extract_frames.py -r 5 -o sample_frames_5fps.zip
# Then enter: /path/to/sample_video.mp4python extract_frames.py --keep-frames
# Then enter: /path/to/sample_video.mp4- Frame Extraction: Uses ffmpeg with the
select=eq(pict_type,I)filter to extract I-frames (keyframes) from the video - Temporary Storage: Saves all frames as PNG files in a temporary directory
- Zipping: Creates a zip file containing all extracted frames
- Cleanup: Removes temporary files (unless
--keep-framesis specified)
- The script extracts I-frames (keyframes) by default, which are typically fewer than total frames but represent complete, independent images
- If you need every single frame, you can modify the ffmpeg command in the script
- PNG format is used for lossless quality
- The script automatically handles temporary directory creation and cleanup
- Progress information is displayed during extraction and zipping
Make sure ffmpeg is installed and available in your system PATH.
Check that the video file path is correct and the file exists.
Make sure you have write permissions in the current directory for creating the zip file.
For very long videos, the extraction process may take some time and generate many frames. Consider using the -r option to limit frame rate if you don't need every frame.
This script is provided as-is for educational and personal use.