Compress and convert images and videos using ffmpeg. Use when the user wants to reduce file size, change format, resize, or optimize media files. Handles common formats like JPG, PNG, WebP, MP4, MOV, WebM. Triggers on phrases like "compress image", "compress video", "reduce file size", "convert to webp/mp4", "resize image", "make image smaller", "batch compress", "optimize media".
Compress and convert images and videos with intelligent defaults. Supports single files and batch processing.
Images: JPG, PNG, WebP, BMP, TIFF, GIF → JPG, PNG, WebP Videos: MP4, MOV, AVI, MKV, WebM, FLV, WMV, M4V → MP4
ffmpeg must be installed:
sudo apt update && sudo apt install ffmpegbrew install ffmpegVerify installation: ffmpeg -version
# Compress to target size (auto-adjusts quality)
python scripts/compress_image.py photo.jpg --max-size 200kb
# Resize + compress
python scripts/compress_image.py photo.png --width 800 --output photo_small.jpg
# Convert to WebP (better compression)
python scripts/compress_image.py photo.jpg --format webp
# Batch compress entire folder
python scripts/compress_image.py ./photos --output ./compressed --quality 80
# Preview mode - see what will happen without compressing
python scripts/compress_image.py photo.jpg --max-size 500kb --preview
# Keep backup of original file
python scripts/compress_image.py photo.jpg --max-size 500kb --backup
# Compress with default settings (good balance)
python scripts/compress_video.py video.mp4 --output video_small.mp4
# Resize to 720p
python scripts/compress_video.py video.mp4 --height 720
# Target specific file size (approximate)
python scripts/compress_video.py video.mp4 --target-size 50mb
# Lower quality for smaller file
python scripts/compress_video.py video.mp4 --crf 28
# Batch process all videos in folder
python scripts/compress_video.py ./videos --output ./compressed --height 480
# Preview mode - see what will happen without compressing
python scripts/compress_video.py video.mp4 --height 720 --preview
# Keep backup of original file
python scripts/compress_video.py video.mp4 --height 720 --backup
Most websites need images under 500KB:
python scripts/compress_image.py photo.jpg --max-size 500kb --format webp
Email often has 25MB limit:
python scripts/compress_image.py scan.pdf.jpg --max-size 5mb --quality 90
Platforms prefer 720p, smaller files:
python scripts/compress_video.py clip.mov --height 720 --crf 23 --preset fast
Maximum compression for storage:
python scripts/compress_video.py old_video.avi --crf 28 --preset slow --height 480
Keep quality, just change format:
# Image
python scripts/compress_image.py image.png --format jpg --quality 95
# Video
python scripts/compress_video.py video.mov --output video.mp4 --crf 18
compress_image.py)| Parameter | Description | Default |
|---|---|---|
input | Input file or directory (required) | - |
--output, -o | Output file or directory | Auto-generated |
--max-size | Target max file size (e.g., 500kb, 2mb) | None |
--quality, -q | JPEG/WebP quality (1-100) | 85 |
--width, -w | Max width in pixels | Original |
--height | Max height in pixels | Original |
--format, -f | Output format: jpg, png, webp | Original format |
--no-strip | Keep metadata (default: remove) | False |
--preview, -p | Preview mode: show settings without compressing | False |
--backup, -b | Keep backup of original file | False |
compress_video.py)| Parameter | Description | Default |
|---|---|---|
input | Input file or directory (required) | - |
--output, -o | Output file or directory | Auto-generated |
--crf | Quality 0-51 (lower=better). 18=visually lossless, 23=default, 28=smaller | 23 |
--preset | Encoding speed: ultrafast, superfast, veryfast, faster, fast, medium, slow, slower, veryslow | medium |
--height | Max height (480, 720, 1080) | Original |
--target-size | Approximate target size (e.g., 50mb, 1gb) | None |
--fps | Limit frame rate (e.g., 30, 24) | Original |
--audio-bitrate | Audio quality (e.g., 128k, 192k) | 128k |
--preview, -p | Preview mode: show settings without compressing | False |
--backup, -b | Keep backup of original file | False |
When input is a directory:
Example:
$ python scripts/compress_image.py ./vacation_photos --output ./compressed --max-size 500kb
找到 24 个图像文件
[1/24] 处理: IMG_001.jpg
✓ 2847.3KB → 456.2KB (84.0% 减少)
[2/24] 处理: IMG_002.png
✓ 1532.1KB → 298.5KB (80.5% 减少)
...
Install ffmpeg first:
# Ubuntu/Debian
sudo apt update && sudo apt install ffmpeg
# macOS
brew install ffmpeg
Make scripts executable:
chmod +x scripts/*.py
Increase quality setting:
# Image
python scripts/compress_image.py photo.jpg --quality 90
# Video
python scripts/compress_video.py video.mp4 --crf 20
--max-size to force target sizeCheck that all files in directory are valid images/videos. Corrupt files may cause errors.