Skip to content

singulainthony/frameExtractor

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 

Repository files navigation

Video Frame Extractor and Zipper

This Python script extracts all frames from an MP4 video using ffmpeg and then creates a zip file containing all the extracted PNG frames.

Requirements

  • Python 3.6+
  • ffmpeg installed and available in your system PATH
  • Built-in Python modules: zipfile, os, subprocess, argparse, tempfile, shutil

Installation

  1. Install ffmpeg (if not already installed):

    • macOS: brew install ffmpeg
    • Ubuntu/Debian: sudo apt install ffmpeg
    • Windows: Download from ffmpeg.org
  2. Download the script:

    # Make the script executable
    chmod +x extract_frames.py

Usage

Basic Usage

Run the script and it will prompt you for the video path:

python extract_frames.py

The 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.zip containing all frames
  • Clean up the temporary files

Advanced Usage

Custom Output Filename

python extract_frames.py -o my_frames.zip
# Then enter: /path/to/video.mp4

Extract at Specific Frame Rate

# 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.mp4

Keep Temporary Frame Files

python extract_frames.py --keep-frames
# Then enter: /path/to/video.mp4

Specify Temporary Directory

python extract_frames.py -d /path/to/temp/dir
# Then enter: /path/to/video.mp4

Command Line Options

  • -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.

Examples

Extract all frames from a video (interactive)

python extract_frames.py
# Then enter: /path/to/sample_video.mp4

Extract frames at 5 FPS and save as custom zip

python extract_frames.py -r 5 -o sample_frames_5fps.zip
# Then enter: /path/to/sample_video.mp4

Extract frames and keep them for inspection

python extract_frames.py --keep-frames
# Then enter: /path/to/sample_video.mp4

How It Works

  1. Frame Extraction: Uses ffmpeg with the select=eq(pict_type,I) filter to extract I-frames (keyframes) from the video
  2. Temporary Storage: Saves all frames as PNG files in a temporary directory
  3. Zipping: Creates a zip file containing all extracted frames
  4. Cleanup: Removes temporary files (unless --keep-frames is specified)

Notes

  • 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

Troubleshooting

"ffmpeg not found" Error

Make sure ffmpeg is installed and available in your system PATH.

"Video file not found" Error

Check that the video file path is correct and the file exists.

Permission Errors

Make sure you have write permissions in the current directory for creating the zip file.

Large Video Files

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.

License

This script is provided as-is for educational and personal use.

About

Use python to extract all the frames of a video

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages