Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save appleseed-iii/e73efe0f19b9585f6dd2ada2c119d100 to your computer and use it in GitHub Desktop.
Save appleseed-iii/e73efe0f19b9585f6dd2ada2c119d100 to your computer and use it in GitHub Desktop.
Convert Movie(.mov) file to Gif(.gif) file in one command line in Mac Terminal

This notes is written by Sheldon. You can find me with #iOSBySheldon in Github, Youtube, Facebook, etc.

Need

Convert .mov/.MP4 to .gif

Reason

As a developer, I feel better to upload a short video when I create the pull request to show other viewers what I did in this PR. I tried .mov format directly got after finishing recording screen using Quicktime, however, gif offers preview in most web pages, and has smaller file size.

This is not limited to developer, anyone has this need can use this method to convert the files.

Thoughts

I tried to use some software to do the job, but my hands are tied since I don't have admin in my office comupter, but I do have brew installed. And I think using command line tool will be a good choice. So I will use HomeBrew, ffmpeg, gifsicle to do the job.

Solution

  1. download HomeBrew
  2. $ brew install ffmpeg
  3. $ brew install gifsicle
  4. full command
    INPUT=/path/to/movie.mov
    OUTPUT=/path/to/movie.gif
    ffmpeg -i $INPUT -pix_fmt rgb8 -r 10 $OUTPUT && gifsicle -O3 $OUTPUT -o $OUTPUT

Explanation

  1. Convert the file to gif using ffmpeg
- input path argument `-i`
- pixel format argument `-pix_fmt`
- removing some frames using framerate argument `-r`
- end `ffmpeg` with new path/to/filename
  1. Optimize the same output file with third option -O3 and rewrite the generated gif file from last step
  2. Notes: using && to make sure the conversion sucess before optimizing

References

  1. https://brew.sh
  2. https://www.ffmpeg.org
  3. https://www.lcdf.org/gifsicle/
  4. full video tutorial with explanation https://www.youtube.com/watch?v=QKtRMFvvDL0
@appleseed-iii
Copy link
Author

appleseed-iii commented Jan 19, 2023

GIF to video

from: https://web.dev/replace-gifs-with-videos/

gif to mp4

ffmpeg -i my-animation.gif -b:v 0 -crf 25 -f mp4 -vcodec libx264 -pix_fmt yuv420p my-animation.mp4

^^^ but only works with even pixels 260x264, etc

for odd dimensions

ffmpeg -i my-animation.gif -vf "crop=trunc(iw/2)*2:trunc(ih/2)*2" -b:v 0 -crf 25 -f mp4 -vcodec libx264 -pix_fmt yuv420p my-animation.mp4

and gif to webm (lighter weight, less adoption)

ffmpeg -i my-animation.gif -c vp9 -b:v 0 -crf 41 my-animation.webm

implementation

<video autoplay loop muted playsinline>
  <source src="my-animation.webm" type="video/webm">
  <source src="my-animation.mp4" type="video/mp4">
</video>

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