DEV Community

Yingtse
Yingtse

Posted on

Convert every video in a folder to GIF from command line

You recorded a few screen clips and want them as GIFs, so they play right in a README, a doc or a chat, with no click.

How do you do it?

You ask an AI for an ffmpeg command. The first GIF looks grainy, and it's huge. You ask again and get a longer command with palettegen and paletteuse, and now it looks right.

Next week you have a folder of ten recordings. The command does one file, so you ask for a loop. The loop breaks on Screen Recording 2026-11-20 at 10.15.32.mov, because of the spaces. You ask again.

Then you save the script somewhere: a Markdown note, or a to-gif.sh file. Next time you need a smaller GIF, you have to find it, then find the fps= and scale= in the middle of the ffmpeg line and edit them.

With cmdbook, the script has one place to live, and it gets a GUI for free.

A form with three fields

Here's how

1. Save the script once. Create a command in cmdbook and paste the script (it's at the end).

2. Mark what changes. The folder, the frame rate and the width become placeholders:

DIR="{{prompt:folder}}"
FPS={{prompt:fps?12|8|15|24}}
WIDTH={{prompt:width?640|480|800|1080}}
Enter fullscreen mode Exit fullscreen mode

?12|8|15|24 makes a list: the first one is the default.

3. Click Run. cmdbook shows a form with three fields. Drag the folder onto the first one, pick a frame rate and a width, press Enter.

4. Watch it run. Every video in the folder and its subfolders becomes a GIF next to it:

-> ./Screen Recordings/screen-recording.mov
   285M ->  22M  ./Screen Recordings/screen-recording.gif

Converted 1, skipped 0 (GIF already up to date), failed 0.
Enter fullscreen mode Exit fullscreen mode

That's a 60-second 1080p screen recording, as an 800 px GIF at 12 fps, in about 11 seconds on my Mac.

Run it again after adding a few clips: GIFs that are already up to date are skipped. A file ffmpeg can't read is reported and the rest still converts.

Next time, your last folder and sizes are already filled in.

That's all a script needs to get a GUI: mark the parts that change. The rest is the script you already have.

Try cmdbook here: printf.app/cmdbook

The full script (macOS, needs ffmpeg)

DIR="{{prompt:folder}}"
FPS={{prompt:fps?12|8|15|24}}
WIDTH={{prompt:width?640|480|800|1080}}

cd "$DIR" || exit 1
export PATH="/opt/homebrew/bin:/usr/local/bin:$PATH"
command -v ffmpeg >/dev/null || { echo "ffmpeg not found. Install it: brew install ffmpeg"; exit 1; }

DONE=0; SKIPPED=0; FAILED=0
while IFS= read -r -d '' IN; do
  OUT="${IN%.*}.gif"
  if [ -e "$OUT" ] && [ "$OUT" -nt "$IN" ]; then
    SKIPPED=$((SKIPPED + 1)); continue
  fi
  echo "-> $IN"
  if ffmpeg -nostdin -hide_banner -v error -y -i "$IN" \
      -vf "fps=$FPS,scale=$WIDTH:-2:flags=lanczos,split[a][b];[a]palettegen[p];[b][p]paletteuse" \
      "$OUT"; then
    echo "   $(du -h "$IN" | cut -f1) -> $(du -h "$OUT" | cut -f1)  $OUT"
    DONE=$((DONE + 1))
  else
    FAILED=$((FAILED + 1))
  fi
done < <(find . -type f \( -iname '*.mov' -o -iname '*.mp4' -o -iname '*.m4v' -o -iname '*.webm' -o -iname '*.mkv' \) -print0 | sort -z)

echo
echo "Converted $DONE, skipped $SKIPPED (GIF already up to date), failed $FAILED."
[ "$FAILED" -eq 0 ]
Enter fullscreen mode Exit fullscreen mode

Top comments (0)