Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
image & video (Imagemagick, ffmpeg, webp...) commands


  • Nornalize all images of a folder converting them to jpgs with 90% quality (warning, removes original files):
rename 's/\.JPG$/\.jpg/' *.JPG
rename 's/\.JPEG$/\.jpg/' *.JPEG
rename 's/\.jpeg$/\.jpg/' *.jpeg
mogrify -format jpg *.png
mogrify -resize 1600x1280\> -quality 90 *.jpg
rm *.png
  • Generate 100x100 JPG thumbnails at a `/thumbs' subfolder from all JPG images of current folder, padding with black:
mkdir thumbs
mogrify -format jpg -quality 90 -path thumbs -unsharp 0x.5 -thumbnail 100x100 -background black -gravity center -extent 100x100 *.jpg
cd thumbs
rename 's/^/t_/' *.jpg


  • setup:
sudo apt install webp
  • convert to png:
dwebp file.webp -o file.png
  • convert one image to jpg (using ImageMagick):
dwebp file.webp -o – | convert – file.jpg
  • convert all images from folder to jpg (using ImageMagick):
for f in *.webp; do dwebp "$f" -o - | convert - "${f%.webp}.jpg"; done
  • convert to webp lossy - JPG:
for f in *.jpg; do cwebp "$f" -q 90 -m 6 -o "${f%.jpg}.webp" -quiet; done
  • convert to lossless webp - PNG:
for f in *.png; do cwebp "$f" -lossless -m 6 -q 100 -o "${f%.png}.webp" -quiet; done
  • convert to webp lossy with alpha channel - GIF? (pending testing):
for f in *.gif; do cwebp "$f" -q 90 -alpha_q 100 -m 6 -o "${f%.gif}.webp" -quiet; done


Animated GIF to MP4 video optimized for web:

ffmpeg -i animation.gif -b:v 0 -crf 40 -vf scale=600:-1 video.mp4

Cut a fragment from a video (without reencoding):

# 2.5 mins from starting time, without reencoding
ffmpeg -ss 00:45:16 -i source.mp4 -t 150 -c:v copy -c:a copy test.mp4

Reverse the frames from a video (Source:

# Dump all video frames
ffmpeg -i test.mp4 -an -qscale 1 %06d.jpg

# Dump audio (TODO: remove/skip)
ffmpeg -i test.mp4 -vn -ac 2 audio.wav

# Cat video frames in reverse order to FFmpeg as input
cat $(ls -r *jpg) | ffmpeg -f image2pipe -vcodec mjpeg -r 25 -i - -i audio.wav -vcodec libx264 -preset slow  -crf 20 -threads 0 -acodec flac reversed.mkv


Needs special binary

find . -type f -name "*.jpg" -exec guetzli --quality 85 {} {}.jpeg \;
find . -type f -name "*.jpg" -exec rm {} +
find . -type f -name "*.jpeg" | rename "s/\.jpeg$//"

If don't want to recurse directories:

find . -type f -maxdepth 1 -name "*.jpg" -exec guetzli --quality 85 {} {}.jpeg \;
rm *.jpg
rename "s/\.jpeg$//" *.jpeg



Copy link

a-eniou-pvotal commented Apr 19, 2022

for a lot of images you could use parallel
parallel -eta cwebp {} -progress -short -mt -o {.}.webp ::: *.jpeg|*.png|*.jpg

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment