Skip to content

Instantly share code, notes, and snippets.

@shazow
Last active January 22, 2024 10:13
Show Gist options
  • Star 10 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save shazow/dcb5c6744d1837bed6d2 to your computer and use it in GitHub Desktop.
Save shazow/dcb5c6744d1837bed6d2 to your computer and use it in GitHub Desktop.
Batch convert a directory of gifs into mp4
#!/usr/bin/bash
# Convert *.gif into *.mp4, skip if already exists.
outdir="."
for path in *.gif; do
out="${outdir}/${path/.gif/}.mp4"
[[ -f "$out" ]] && continue
ffmpeg -f gif -i "${path}" "${out}"
done
@shazow
Copy link
Author

shazow commented Aug 27, 2015

Feel free to copy-and-paste the body into your shell while in the dir with *.gif files, or download the file and:

$ chmod +x convert-gifs.sh  # Make it executable
$ ./convert-gifs.sh

@aimhighagency
Copy link

Thanks! This works perfect.

But I updated to this:
for path in *.gif; do out="${outdir}/${path/.gif/}.mp4"; [[ -f "$out" ]] && continue; ffmpeg -f gif -i "${path}" -pix_fmt yuv420p "${out}"; done

That way the output works in Quicktime

@qwksilver
Copy link

is there an easy way to modify this script to do this for subdirs also, leaving an mp4 in place, and moving the gif out to a mirrored structure in another location?

@shazow
Copy link
Author

shazow commented Mar 16, 2022

@qwksilver You'll need to use something like find to get all the gifs. Rough sketch:

find . -path '*.gif' | while read path; do
  ...
done

Then do the rest of the changes you want. :)

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