Skip to content

Instantly share code, notes, and snippets.

@dnywh
Created August 29, 2025 01:58
Show Gist options
  • Save dnywh/5108e9fd767521157f48f5ed8e453725 to your computer and use it in GitHub Desktop.
Save dnywh/5108e9fd767521157f48f5ed8e453725 to your computer and use it in GitHub Desktop.
Batch shrink videos with ffmpeg.
# shrinkvid - batch shrink videos with ffmpeg
# Add this to your ~/.zshrc
# --------------------------------------------
# Usage:
# shrinkvid [directory] [format] [crf]
#
# Examples:
# shrinkvid # defaults: ~/Downloads, mp4, CRF=28
# shrinkvid ~/Movies # custom directory
# shrinkvid . mkv 24 # current dir, mkv files, CRF=24
#
# Notes:
# - Originals are renamed with `_Original` suffix.
# - Skips files already ending in `_Original`.
# - Does not overwrite existing `_Original` files.
shrinkvid() {
emulate -L zsh
set -o pipefail
setopt null_glob
local dir="${1:-$HOME/Downloads}"
local fmt="${2:-mp4}"
local crf="${3:-28}"
builtin pushd -q "$dir" || { echo "Directory not found: $dir"; return 1; }
for i in *.${fmt}; do
[[ "$i" == *_Original.${fmt} ]] && continue
local original="${i%.*}_Original.${fmt}"
mv -n -- "$i" "$original" || { echo "Skipping $i (backup exists)"; continue; }
echo "Shrinking $i with CRF=$crf..."
ffmpeg -y -i "$original" -vcodec libx264 -crf "$crf" "$i"
done
popd -q
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment