A tool for analyzing and optimizing GIF files, with a focus on efficient batch processing optimized for Mac M2 chips.
- Analyze GIF files for quality metrics (SSIM, PSNR, VMAF)
- Compress GIFs using multiple compression engines
- Generate detailed reports on compression efficiency
- Smart batch processing with adaptive sizing
- Optimized for Mac M2 chips
The latest version includes an intelligent adaptive batch sizing system that dynamically adjusts the number of GIFs processed in parallel based on:
-
GIF Complexity Analysis: Automatically detects GIF complexity by examining:
- Frame differences
- File size
- Resolution and frame count
- Color complexity
-
Memory Management: Monitors system resources to prevent out-of-memory errors:
- Dynamically adjusts batch sizes based on available RAM
- Prevents VMAF calculation failures due to memory limitations
- Ensures optimal resource utilization
-
Optimization by Complexity Grouping:
- Processes simple GIFs in larger batches (maximum efficiency)
- Processes complex GIFs in smaller batches (ensures reliability)
- Balances throughput with stability
When processing a large number of GIFs (e.g., 9,000 files), the system:
-
First samples each GIF to estimate its complexity (quick examination)
-
Categorizes GIFs into complexity groups (low, medium, high)
-
Calculates optimal batch sizes for each group based on:
- Available system memory
- Base batch size (configurable)
- Estimated memory requirements per GIF
-
Creates optimized processing batches to maximize throughput while preventing errors
-
Monitors performance during processing to make adjustments
The adaptive system is particularly valuable for VMAF calculations, which are memory-intensive:
- Prevents out-of-memory errors during VMAF processing
- Dynamically reduces batch size for complex GIFs
- Adjusts to system load for consistent performance
- Maintains high throughput by processing simple GIFs in larger batches
Run the optimized analysis script:
./run_analysis_mac_optimized.py --batch-size 5The --batch-size parameter sets the base batch size, which will be automatically adjusted based on GIF complexity.
./run_analysis_mac_optimized.py --helpFor VMAF-specific processing:
./run_analysis_mac_optimized.py --selective --fields vmaf_all_frames,vmaf_first_frame,vmaf_min,vmaf_max,vmaf_harmonic_mean- For general processing: Use batch size 5-6 (default is 6)
- For memory-constrained systems: Use batch size 3-4
- For VMAF-heavy processing: Use batch size 4-5
- For maximum throughput on high-end systems: Use batch size 8
The adaptive system will automatically scale down batch sizes for complex GIFs, so these are just starting points.
The tool supports multiple compression engines:
- Gifsicle: Industry standard GIF optimization tool
- Uses optimization levels 1-3 and lossy values 0-300
- Encoded as: (optimization_level * 1000) + lossy_value
- See GIFSICLE_ENCODING.md for detailed information
- Animately: Advanced GIF compression engine with superior compression ratios
- Uses 11 compression levels (0-100)
- Usually provides 10-16% better compression than gifsicle at equivalent levels
Based on our testing, the Animately engine typically outperforms Gifsicle:
| Engine | Level | Compression Ratio | Notes |
|---|---|---|---|
| Gifsicle | 0 | 1.01 | Minimal compression, lossless |
| Gifsicle | 50 | 0.94 | Good compression, some quality loss |
| Animately | 0 | 0.93 | Better than gifsicle level 50 |
| Animately | 50 | 0.84 | Best compression, acceptable quality |
- macOS on Apple Silicon (optimized for M2)
- Python 3.8+
- Required Python packages:
- numpy
- pandas
- Pillow
- psutil (for memory monitoring)
- tqdm (for progress tracking)
This tool analyzes GIF files to calculate quality metrics and optimize file size while maintaining visual quality. It implements Netflix's Video Multimethod Assessment Fusion (VMF) algorithm to assess the perceptual quality of compressed GIFs.
- Quality Assessment: Calculate SSIM, PSNR, and VMF scores for GIFs at different compression levels
- Compression Optimization: Test multiple compression levels to find optimal quality/size ratio
- Parallel Processing: Process multiple files simultaneously for faster analysis
- Detailed Reports: Generate comprehensive reports with quality metrics and file size data
- Adaptive Batch Sizing: Smart batch processing with adaptive sizing
- Python 3.8 or higher
- Pip package manager
-
Clone the repository:
git clone https://github.com/yourusername/gif-analysis.git cd gif-analysis -
Install the package and dependencies:
python install.pyor
pip install -e .
The tool can be run from the command line:
python main.py [OPTIONS]
--input-dir,-i: Directory containing GIF files to analyze--output-dir,-o: Directory where compressed GIFs will be saved--levels,-l: Comma-separated list of compression levels to test (0-300)--engine,-e: Engine to use: 'animately', 'gifsicle', or 'all' (default)--parallel,-p: Enable parallel processing--no-parallel: Disable parallel processing--verbose,-v: Enable verbose logging--max-files,-m: Maximum number of GIF files to process (for testing)--safe-levels: Use only safe compatible levels (0-100) for all engines
For convenience, three shorthand scripts are provided to run the tool with predefined settings:
./run_analysis_mac_optimized.py [ADDITIONAL_OPTIONS]
a way to run the script with selective optimizations:
./run_analysis_mac_optimized.py --selective
a way to run the script with overwrite:
./run_analysis_mac_optimized.py --overwrite
a way to run the script with batch size:
./run_analysis_mac_optimized.py --batch-size 5
a way to run the script with batch size and selective:
./run_analysis_mac_optimized.py --batch-size 3 --selective
This script provides the most optimized experience, especially for Mac M2 systems, with these specific optimizations:
- Automatically configures CPU thread usage based on available cores (leaving 2 cores free for system processes)
- Sets optimal batch size for M2 Mac memory configurations
- Enables hardware acceleration for video processing using Apple's VideoToolbox
- Configures NumPy and BLAS libraries for optimal performance
- Provides enhanced logging and graceful interruption handling (Ctrl+C to pause/resume)
- Supports all the same options as other scripts
./analyze.sh [ADDITIONAL_OPTIONS]
./run_analysis.py [ADDITIONAL_OPTIONS]
All scripts:
- Use the default input directory (
gif_analysis/data) - Use the default output directory (
gif_analysis/output/compressed_gifs) - Enable verbose logging
- By default, use all available compression engines with their optimal levels:
- Gifsicle: 33 encoded levels (1000-3300)
- Animately: 11 levels (0-100)
- Pass any additional options to the main script
--gifsicle-encoded: Use only gifsicle with its 33 encoded compression levels--animately-only: Use only the animately engine with its 11 compression levels--safe-levels: Use all engines but with safe compatible levels (0-100) for both
Example:
# Run analysis with maximum of 5 files
./analyze.sh --max-files 5
# Use only the animately engine
./run_analysis.py --animately-only
# Use only gifsicle with encoded compression levels
./analyze.sh --gifsicle-encoded
# Use both engines with safe compatible levels
./run_analysis.py --safe-levels
Basic usage:
python main.py -i ./gif_analysis/data -o ./gif_analysis/output/compressed_gifs
Specific compression levels:
python main.py -i ./gif_analysis/data -o ./gif_analysis/output/compressed_gifs -l 30,60,90,120
Disable parallel processing:
python main.py -i ./gif_analysis/data -o ./gif_analysis/output/compressed_gifs --no-parallel
Limit processing to a few files for testing:
python main.py -i ./gif_analysis/data -o ./gif_analysis/output/compressed_gifs -l 0,50 --max-files 5
The tool uses a YAML configuration file located at gif_analysis/config.yaml. You can modify this file to customize:
- Input and output directories
- Default compression levels
- Report paths
- Other parameters
gif-analysis/ # Repository root
│
├── gif_analysis/ # Python package directory
│ ├── __init__.py # Package initialization
│ ├── config.yaml # Configuration file
│ ├── core/ # Core functionality
│ │ ├── __init__.py # Core subpackage initialization
│ │ ├── analyzer.py # Main analysis module
│ │ ├── compression_manager.py # Handles compression engines
│ │ └── utils.py # Utility functions
│ ├── data/ # Input data directory
│ │ ├── unused/ # Storage for unused GIF files
│ │ └── tagged_examples/ # Categorized example GIFs
│ └── output/ # Output directory
│ ├── compressed_gifs/ # Compressed GIFs
│ │ └── gifsicle/ # Gifsicle engine outputs
│ ├── logs/ # Log files
│ ├── data/ # Analysis data (CSV files)
│ └── reports/ # Analysis reports
│
├── main.py # CLI entry point
├── analyze.sh # Shell script runner
├── run_analysis.py # Python script runner
├── install.py # Installation script
├── migrate_data.py # Data migration script
├── setup.py # Package setup file
├── tests/ # Test files
├── cleanup_plan.md # Documentation of directory cleanup
├── LICENSE # License file
└── README.md # This file
The tool follows a standard Python package structure:
- All core functionality is inside the
gif_analysispackage - Data files (GIFs to analyze) should be placed in
gif_analysis/data/ - Output files are stored in
gif_analysis/output/with dedicated subdirectories - Configuration is managed through
gif_analysis/config.yaml
If you're upgrading from a previous version with a different directory structure, you can use the migrate_data.py script to help migrate your files:
python migrate_data.py
This script will:
- Copy files from old directories to the new package structure
- Help you clean up redundant directories
- Guide you through the migration process
- SSIM (Structural Similarity Index): Measures the similarity of two images
- PSNR (Peak Signal-to-Noise Ratio): Measures the ratio between maximum possible power of a signal and the power of noise
- VMF (Video Multimethod Assessment Fusion): Netflix's algorithm for perceptual quality assessment that combines multiple metrics with content-adaptive weighting
- Gifsicle: High-quality GIF optimization with lossy compression options
- Uses 33 encoded compression levels (1000-3300) across 3 optimization levels
- Each optimization level (1-3) has 11 lossy values (0-300)
- Encoded as: (optimization_level * 1000) + lossy_value
- See GIFSICLE_ENCODING.md for detailed information
- Animately: Advanced GIF compression engine with superior compression ratios
- Uses 11 compression levels (0-100)
- Usually provides 10-16% better compression than gifsicle at equivalent levels
- Maintains high perceptual quality while achieving smaller file sizes
Based on our testing, the Animately engine typically outperforms Gifsicle:
| Engine | Level | Compression Ratio | Notes |
|---|---|---|---|
| Gifsicle | 0 | 1.01 | Minimal compression, lossless |
| Gifsicle | 50 | 0.94 | Good compression, some quality loss |
| Animately | 0 | 0.93 | Better than gifsicle level 50 |
| Animately | 50 | 0.84 | Best compression, acceptable quality |
You can test both compression engines on a single GIF file using:
python test_engines.py
This will compress the test.gif file with both Gifsicle and Animately engines at different compression levels and show detailed results.
- Netflix Technology Blog for VMF research: "Toward A Practical Perceptual Video Quality Metric"
- The Gifsicle project for their excellent GIF optimization tool
- Animately team for their advanced GIF compression technology