Image and Video CLI Cheatsheet

imagemagick

  • Normalize all images of a folder converting them to JPG 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
convert input.png -filter box -resize 400% output.png
  • Generate a favicon from pngs (optimizes pretty well):
convert 16.png 32.png 48.png favicon.ico
  • Generate a .gif from all .png files in a folder (delay is x/100, hundredths of a second), at 1 FPS, cropping all images to 725x540 pixels:
convert -delay 100 -loop 0 -crop 725x540+0+0 +repage *.png output.gif

webp

Also uses ImageMagick.

  • Setup:
sudo apt install webp
  • Convert to PNG:
dwebp file.webp -o file.png
  • Convert to JPG:
dwebp file.webp -o – | convert – file.jpg
  • Convert all folder images to JPG:
for f in *.webp; do dwebp "$f" -o "${f%.webp}.jpg" && magick "${f%.webp}.jpg" "${f%.webp}.jpg"; done
  • Convert to Webp lossy (equivalent to JPG):
for f in *.jpg; do cwebp "$f" -q 90 -m 6 -o "${f%.jpg}.webp" -quiet; done
  • Convert to lossless Webp (equivalent to 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 (equivalent to GIF? pending testing):
for f in *.gif; do cwebp "$f" -q 90 -alpha_q 100 -m 6 -o "${f%.gif}.webp" -quiet; done

ffmpeg

  • Convert 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 re-encoding):
# 150s (2.5 mins) from starting time
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
ffmpeg -i test.mp4 -vn -ac 2 audio.wav

# Send 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
  • Can also be used to convert audio formats:
# convert m4a/m4b to mp3
ffmpeg -v 5 -y -i input.m4b -acodec libmp3lame -ac 2 -ab 192k output.mp3

guetzli

Needs a special binary.

  • Compress:
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

webm

favicons

Image and Video CLI Cheatsheet page, written by Kartones