Title: Image and Video CLI Cheatsheet
Slug: image-and-video-cli-cheatsheet
Author: Kartones
Lang: en
Description: A cheatsheet of imagemagick and other CLI commands for processing images and videos.

### imagemagick

* Normalize all images of a folder converting them to JPG with 90% quality (warning, removes original files):
```bash
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:
```bash
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
```
* [Official examples of ImageMagick Usage](http://www.imagemagick.org/Usage/)
* [Fred's ImageMagick scripts](http://www.fmwconcepts.com/imagemagick/index.php)
* Resize an image pixelating, without blending pixels:
```bash
convert input.png -filter box -resize 400% output.png
```
* Generate a favicon from pngs (optimizes pretty well):
```bash
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:
```bash
convert -delay 100 -loop 0 -crop 725x540+0+0 +repage *.png output.gif
```


### webp

Also uses ImageMagick.


* Setup:
```bash
sudo apt install webp
```
* Convert to PNG:
```bash
dwebp file.webp -o file.png
```
* Convert to JPG:
```bash
dwebp file.webp -o – | convert – file.jpg
```
* Convert all folder images to JPG:
```bash
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):
```bash
for f in *.jpg; do cwebp "$f" -q 90 -m 6 -o "${f%.jpg}.webp" -quiet; done
```
* Convert to lossless Webp (equivalent to PNG):
```bash
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):
```bash
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:
```bash
ffmpeg -i input.gif -b:v 0 -crf 40 -vf scale=600:-1 output.mp4
```
* Cut a fragment from a video (without re-encoding):
```bash
# 150s (2.5 mins) from starting time
ffmpeg -ss 00:45:16 -i input.mp4 -t 150 -c:v copy -c:a copy output.mp4
```
* Remove the first X seconds from a video (without re-encoding):
```bash
ffmpeg -ss X -i input.mp4 -c copy output.mp4
```

* Reverse the frames from a video ([source](https://stackoverflow.com/questions/2553448/encode-video-in-reverse)):
```bash
# Dump all video frames
ffmpeg -i input.mp4 -an -qscale 1 %06d.jpg

# Dump audio
ffmpeg -i input.mp4 -vn -ac 2 output.wav

# Dump audio, skipping the first 5.5 seconds
ffmpeg -i input.mp4 -ss 00:00:05.5 output.mp3

# 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:
```bash
# convert m4a/m4b to mp3, specific bitrate chosen
ffmpeg -v 5 -y -i input.m4b -acodec libmp3lame -ac 2 -ab 192k output.mp3

# convert m4a to mp3, maximum quality
ffmpeg ffmpeg -i input.m4a  -q:a 0 -map a output.mp3

# remove video tracks if present, very high quality (0 highest, chosen 1)
ffmpeg -i input.m4b -vn -c:a libmp3lame -q:a 1 output.mp3
```

* And to cut fragments of MP3 files:
```bash
# Cut the first 60.5 seconds of an MP3 file
ffmpeg -ss 60.5 -y -i input.mp3 -c copy output.mp3 -y

# Remove the last 30 seconds from an MP3 (slightly more complex, as we need to first get the duration)
ffmpeg -i input.mp3 -t $(echo "$(ffprobe -v error -show_entries format=duration -of csv=p=0 input.mp3)-30" | bc) -c copy output.mp3 -y
```


### guetzli

Needs a [special binary](https://github.com/Kartones/guetzli).

* Compress:
```bash
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:
```bash
find . -type f -maxdepth 1 -name "*.jpg" -exec guetzli --quality 85 {} {}.jpeg \;
rm *.jpg
rename "s/\.jpeg$//" *.jpeg
```

### webm
* To capture screencasts, I use [Kazam Screencaster](https://launchpad.net/kazam) (Linux)
* To convert from `webm` to `gif`, I use [Online Converter](https://www.onlineconverter.com/convert/303aee2813c6043e6841c1c1e77e61bd56): Good compression quality and decent file size.

### favicons
* [Optimizing favicons](https://gist.github.com/jaydenseric/a647f56e09e7ccbcb05867f4da1b6df2) + [ImageOptim online](https://imageoptim.com/online)
