CLIcwebpImageMagickffmpeg7 min read
Convert WebP Images via Command Line
Reference guide for cwebp, dwebp, ImageMagick, and ffmpeg. Batch one-liners for macOS, Linux, and Windows PowerShell.
| Tool | Direction | Best for |
|---|---|---|
| cwebp | JPG/PNG → WebP | Encoding, quality control, metadata |
| dwebp | WebP → PNG/PPM | Decoding WebP, exact pixel output |
| ImageMagick (magick/mogrify) | Any ↔ Any | Batch, resize+convert in one step |
| ffmpeg | Any → WebP / animated | Video frames, GIF → animated WebP |
Install the tools
# macOS (Homebrew) brew install webp imagemagick ffmpeg # Ubuntu / Debian sudo apt install webp imagemagick ffmpeg # Windows (winget) winget install Google.WebPCodec winget install ImageMagick.ImageMagick # ffmpeg: https://ffmpeg.org/download.html
cwebp — encode to WebP
# Basic conversion (quality 85) cwebp -q 85 input.jpg -o output.webp # High quality (near-lossless) cwebp -q 95 input.png -o output.webp # Lossless mode (for logos, icons) cwebp -lossless input.png -o output.webp # With metadata stripping (smaller file) cwebp -q 85 -metadata none input.jpg -o output.webp # Resize to max 1200px width while converting cwebp -q 85 -resize 1200 0 input.jpg -o output.webp # (0 = auto-calculate height to preserve aspect ratio) # Show encoding stats cwebp -q 85 -v input.jpg -o output.webp
Batch: JPG/PNG → WebP
# macOS / Linux — convert all JPG in current folder
for f in *.jpg; do cwebp -q 85 "$f" -o "${f%.jpg}.webp"; done
# Include PNG too
for f in *.{jpg,jpeg,png}; do
cwebp -q 85 "$f" -o "${f%.*}.webp"
done
# Windows PowerShell
Get-ChildItem *.jpg | ForEach-Object {
cwebp -q 85 $_.FullName -o "$($_.BaseName).webp"
}dwebp — decode WebP to PNG
# WebP → PNG (lossless, preserves transparency)
dwebp input.webp -o output.png
# WebP → PPM (for scripts that need raw pixels)
dwebp input.webp -ppm -o output.ppm
# Batch: all WebP → PNG
for f in *.webp; do dwebp "$f" -o "${f%.webp}.png"; doneImageMagick — universal converter
# Convert a single file
magick input.jpg output.webp
magick input.webp output.jpg
magick input.webp output.png
# With quality control
magick input.jpg -quality 85 output.webp
# Lossless WebP
magick input.png -define webp:lossless=true output.webp
# Resize and convert
magick input.jpg -resize 1200x output.webp
# Batch convert (mogrify — overwrites originals by default!)
# Always use -path to save to a different directory
mkdir webp_output
mogrify -format webp -quality 85 -path ./webp_output *.jpg
# Windows PowerShell
New-Item -ItemType Directory -Force -Path webp_output
Get-ChildItem *.jpg | ForEach-Object {
magick $_.FullName -quality 85 "webp_output\$($_.BaseName).webp"
}Warning:
mogrify without -path overwrites the original files. Always specify an output directory.ffmpeg — animated WebP and video frames
# Static image → WebP ffmpeg -i input.jpg output.webp # GIF → animated WebP ffmpeg -i animation.gif -c:v libwebp \ -lossless 0 -compression_level 6 \ -q:v 70 -loop 0 -preset picture \ -an -vsync 0 output.webp # Video → WebP (first 5 seconds, 15fps) ffmpeg -i video.mp4 -vf fps=15 -t 5 \ -c:v libwebp -q:v 80 -loop 0 output.webp