image-video-optimizer
v3.4.0
Published
CLI tool to optimize and compress images and videos with configurable settings
Downloads
2,531
Maintainers
Readme
Image Video Optimizer
A powerful CLI tool to optimize images, videos, audio, and PDFs with configurable resize and compression settings.
Features
- Image Optimization: Resize and convert images to specified formats
- Video Optimization: Resize videos and encode to specified formats
- Audio Optimization: Convert and compress audio files to MP3
- PDF Compression: Compress PDF files using Ghostscript
- Resume Support: Track processed files and resume interrupted sessions
- Configurable Settings: Use
.image-video-optimizer.conffiles for custom settings - Smart Compression: Only keeps optimized files if compression is effective (>1%)
- Recursive Search: Finds all media files in subdirectories
- Detailed Logging: Shows processing progress and summary statistics
Installation
npm install -g image-video-optimizerUsage
Basic Usage
image-video-optimizer . # will proceed the current directory
image-video-optimizer /path/to/directory # will proceed the specified directory
image-video-optimizer /path/to/directory/file.jpg # will proceed the specified image file
image-video-optimizer /path/to/directory/file.pdf # will proceed the specified pdf file
image-video-optimizer /path/to/directory/file.mp4 # will proceed the specified video file
image-video-optimizer /path/to/directory/file.mp3 # will proceed the specified audio fileOptions
image-video-optimizer /path/to/directory [options]<target>: Target directory or file to optimize (required)-r, --reset: Reset status file and start fresh-v, --verbose: Enable verbose logging-V, --version: Show version number-h, --help: Show help
Configuration
Create a .image-video-optimizer.conf file in your target directory to customize settings:
# Image settings
img_max_width=1080 # Maximum width for images (pixels)
img_format=jpg # Target format for image conversion
# Video settings
video_max_width=720 # Maximum width for videos (pixels)
video_encode=h264 # Video encoding format
# Audio settings
audio_ext=mp3 # Audio extension for audio files
# PDF settings
pdf_compress=true # Enable/disable PDF compressionDefault Configuration
If no configuration file is found, these defaults are used:
img_max_width: 1080img_format: jpgvideo_max_width: 720video_encode: h264audio_ext: mp3pdf_compress: true
Supported Formats
Images
- Input: jpg, jpeg, png, gif, bmp, tiff, webp
- Output: jpg, png, webp (configurable)
Videos
- Input: avi, mov, wmv, flv, webm, mkv, m4v, mp4
- Output: mp4 (with configurable encoding)
Audio
- Input: mp3, wav, flac, aac, ogg, m4a
- Output: mp3 (configurable)
Documents
- Input: pdf
- Output: pdf (compressed)
Processing Logic
Image Processing
- Searches for image files recursively
- Checks if image width exceeds
img_max_width - Resizes if necessary while maintaining aspect ratio
- Converts to target format if different
- Compares file sizes and keeps optimized version only if compression > 1%
Video Processing
- Searches for video files recursively
- Checks if video width exceeds
video_max_width - Resizes if necessary while maintaining aspect ratio
- Encodes to target format (default: H.264)
- Converts to MP4 format
- Compares file sizes and keeps optimized version only if compression > 1%
Audio Processing
- Searches for audio files recursively
- Converts to target format (default: MP3)
- Applies aggressive compression (single channel, 16kHz, 32k bitrate)
- Replaces original with compressed version
PDF Processing
- Searches for PDF files recursively
- Uses Ghostscript to compress PDFs
- Falls back to copy if Ghostscript is not available
- Replaces original with compressed version
Resume Logic
- Creates
.image-video-optimizer-status.jsonto track processed files - Skips already processed files on subsequent runs
- Updates status file after each file is processed
- Use
--resetto clear status and start fresh
Examples
Optimize a directory with default settings
image-video-optimizer ./photosReset status and start fresh
image-video-optimizer ./photos --resetVerbose output
image-video-optimizer ./photos --verboseCustom configuration
Create .image-video-optimizer.conf:
img_max_width=1920
img_format=webp
video_max_width=1080
video_encode=h265
audio_ext=mp3
pdf_compress=trueThen run:
image-video-optimizer . # and image-video-optimizer the same as it will proceed the current directory
image-video-optimizer ./media/path/to/directory # will proceed the specified directoryDependencies
- sharp - Image processing
- fluent-ffmpeg - Video processing
- commander - CLI framework
System Requirements
- Node.js >= 14.0.0
- FFmpeg (for video and audio processing)
- Ghostscript (for PDF compression, optional)
Installing Requirements
Arch/GopiOS:
sudo pacman -Syu ffmpeg x264Ubuntu/Debian:
sudo apt install ffmpeg ghostscriptmacOS:
brew install ffmpeg ghostscriptLicense
nirvána
