Skip to content

Video-Frame-Extractor/Video-Frame-Extractor-Tool

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 

Repository files navigation

Extract Video Frames Using FFmpeg and Python

This guide covers how to use the video_to_frames.py script to extract frames from video files using FFmpeg. This command-line approach is ideal for batch processing, automation, or integration into existing workflows.

Prerequisites

Install FFmpeg

Before using the script, you need to have FFmpeg installed on your system.

macOS (using Homebrew):

brew install ffmpeg

Ubuntu/Debian:

sudo apt update
sudo apt install ffmpeg

Windows:

  1. Download FFmpeg from https://ffmpeg.org/download.html
  2. Extract the archive and add the bin folder to your system PATH

Verify Installation:

ffmpeg -version

Python Requirements

The script requires Python 3.6 or higher. No additional Python packages are needed as it only uses built-in modules.

python3 --version

Quick Start

Basic Usage

Extract frames at 1 frame per second (default):

python3 video_to_frames.py video.mp4 -o output_frames/

Show Video Information

View video details without extracting frames:

python3 video_to_frames.py video.mp4 --info

Output:

Video Information:
  Resolution: 1920x1080
  Frame Rate: 30.00 fps
  Duration: 120.50 seconds
  Total Frames: ~3615

Command Line Options

Option Description Default
video Path to input video file (required)
-o, --output Output directory for frames frames
-f, --fps Frames per second to extract 1
--format Output format: jpg, png, webp jpg
-q, --quality Image quality (1-100) 90
--start Start time in seconds None
--end End time in seconds None
--prefix Filename prefix for output files frame
--scale Output resolution (e.g., -1:720) None
--info Show video info and exit False

Usage Examples

Extract at Different Frame Rates

1 frame per second (good for long videos):

python3 video_to_frames.py video.mp4 -o frames/ -f 1

5 frames per second:

python3 video_to_frames.py video.mp4 -o frames/ -f 5

Extract at original frame rate (30 fps example):

python3 video_to_frames.py video.mp4 -o frames/ -f 30

Choose Output Format

JPEG (smaller files, lossy compression):

python3 video_to_frames.py video.mp4 -o frames/ --format jpg

PNG (lossless, larger files):

python3 video_to_frames.py video.mp4 -o frames/ --format png

WebP (modern format, good compression):

python3 video_to_frames.py video.mp4 -o frames/ --format webp

Adjust Image Quality

High quality (larger files):

python3 video_to_frames.py video.mp4 -o frames/ --quality 95

Lower quality (smaller files):

python3 video_to_frames.py video.mp4 -o frames/ --quality 70

Extract Specific Time Range

Extract frames from 10 seconds to 30 seconds:

python3 video_to_frames.py video.mp4 -o frames/ --start 10 --end 30

Extract first 60 seconds only:

python3 video_to_frames.py video.mp4 -o frames/ --end 60

Skip first 5 seconds:

python3 video_to_frames.py video.mp4 -o frames/ --start 5

Custom Filename Prefix

python3 video_to_frames.py video.mp4 -o frames/ --prefix screenshot
# Output: screenshot_0001.jpg, screenshot_0002.jpg, ...

Resize Output Frames

Resize to 720p (maintain aspect ratio):

python3 video_to_frames.py video.mp4 -o frames/ --scale -1:720

Resize to specific dimensions:

python3 video_to_frames.py video.mp4 -o frames/ --scale 1280:720

Combined Example

Extract PNG frames from a specific time range at 2 fps with high quality:

python3 video_to_frames.py movie.mp4 \
  -o movie_frames/ \
  -f 2 \
  --format png \
  --quality 95 \
  --start 60 \
  --end 120 \
  --prefix scene

Output Structure

After extraction, your output directory will contain numbered image files:

output_frames/
  frame_0001.jpg
  frame_0002.jpg
  frame_0003.jpg
  ...

The 4-digit numbering (%04d) supports up to 9,999 frames. For longer videos with higher frame rates, the script will continue numbering beyond this.

Tips and Best Practices

Choosing Frame Rate

  • 1 fps: Good for long videos, creating thumbnails, or general overview
  • 2-5 fps: Balanced extraction for most use cases
  • 10+ fps: Detailed analysis, animation, or short clips
  • Original fps: When you need every frame (large output)

Choosing Format

Format Best For File Size
JPEG Web use, sharing, storage efficiency Small
PNG Editing, transparency, lossless quality Large
WebP Modern web, good balance Medium

Optimize for Speed

For large videos, using a time range can significantly speed up extraction:

# Instead of extracting entire video
python3 video_to_frames.py long_video.mp4 -o frames/

# Extract only the portion you need
python3 video_to_frames.py long_video.mp4 -o frames/ --start 300 --end 600

Batch Processing

Process multiple videos with a shell loop:

for video in *.mp4; do
  output_dir="${video%.mp4}_frames"
  python3 video_to_frames.py "$video" -o "$output_dir" -f 1
done

Troubleshooting

"FFmpeg is not installed"

Ensure FFmpeg is in your system PATH. After installation, restart your terminal or run:

# macOS/Linux
source ~/.bashrc  # or ~/.zshrc

# Windows - restart Command Prompt or PowerShell

"Video file not found"

Use the full path to the video file:

python3 video_to_frames.py /full/path/to/video.mp4 -o frames/

Low Quality Output

Increase the quality parameter:

python3 video_to_frames.py video.mp4 -o frames/ --quality 95

Or use PNG format for lossless output:

python3 video_to_frames.py video.mp4 -o frames/ --format png

Out of Disk Space

  • Use a lower frame rate (-f 0.5 for 1 frame every 2 seconds)
  • Extract a specific time range (--start and --end)
  • Use JPEG format with lower quality

Comparison: CLI vs Browser Tool

Feature video_to_frames.py VideoToJPG.com
Installation Requires FFmpeg None (browser-based)
Batch Processing Yes No
Automation Yes (scriptable) No
Sharpness Detection No Yes
Visual Frame Selection No Yes
Offline Use Yes Yes (after page loads)
Best For Automation, batch jobs Interactive selection

Use the Python script when you need automation or batch processing. Use VideoToJPG.com when you want visual frame selection with sharpness detection.