Skip to content

Instantly share code, notes, and snippets.

@SheldonWangRJT
Last active May 1, 2024 16:22
Show Gist options
  • Save SheldonWangRJT/8d3f44a35c8d1386a396b9b49b43c385 to your computer and use it in GitHub Desktop.
Save SheldonWangRJT/8d3f44a35c8d1386a396b9b49b43c385 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. $ffmpeg -i in.mov -pix_fmt rgb8 -r 10 output.gif && gifsicle -O3 output.gif -o output.gif

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
@seanf
Copy link

seanf commented Jan 17, 2022

Thanks for the function @mcmoe! But maybe return 1 would be safer than exit 1 in a function. My whole shell exited the first time I ran v2g without arguments.

@mcmoe
Copy link

mcmoe commented Jan 18, 2022

Ah, a return would be much better indeed @seanf 👍
I've updated that in my fork: https://gist.github.com/mcmoe/c76895ee86bd5293d58aca7a75afb6b2 and in my comment above

@Vages
Copy link

Vages commented Aug 16, 2022

Here is a simple shell function I wrote encapsulating this:

,to-gif() {
    # Based on https://gist.github.com/SheldonWangRJT/8d3f44a35c8d1386a396b9b49b43c385
    output_file="$1.gif"

    ffmpeg -i $1 -pix_fmt rgb8 -r 10 $output_file && gifsicle -O3 $output_file -o $output_file
}

@mcmoe's function has way more options, and is probably better if you care more about your results than I 😅

@toridoriv
Copy link

Thanks! To be honest, I'm way more comfortable with the terminal than with GUIs, so this is greatly appreciated 😊

@BinaryShrub
Copy link

BinaryShrub commented Feb 7, 2023

Update on @Vages to mute console and downscale by 50% for propersize with retina on macOS

gif() {
    # Based on https://gist.github.com/SheldonWangRJT/8d3f44a35c8d1386a396b9b49b43c385
    output_file="$1.gif"
    ffmpeg -y -i $1 -v quiet -vf scale=iw/2:ih/2 -pix_fmt rgb8 -r 10 $output_file && gifsicle -O3 $output_file -o $output_file
}

@pdegnan
Copy link

pdegnan commented Sep 22, 2023

very handy, thanks!

@iBanJavascript
Copy link

Glad I came across this via ddg search. Thanks for sharing. Worked for me perfectly.

@tommydangerous
Copy link

Yea this is awesome, thank you.

@adamamyl
Copy link

adamamyl commented Feb 5, 2024

Update on @Vages to mute console and downscale by 50% for propersize with retina on macOS

gif() {
    # Based on https://gist.github.com/SheldonWangRJT/8d3f44a35c8d1386a396b9b49b43c385
    output_file="$1.gif"
    ffmpeg -y -i $1 -v quiet -vf scale=iw/2:ih/2 -pix_fmt rgb8 -r 10 $output_file && gifsicle -O3 $output_file -o $output_file
}

I got a bit annoyed by Screen Recording YYYY-MM-DD… and superflous extensions, so I've added some extension stripping (far from perfect, but YOLO) and some quoting

v2gif() {
    # Based on https://gist.github.com/SheldonWangRJT/8d3f44a35c8d1386a396b9b49b43c385
    output_file="${1%.*}.gif"
    ffmpeg -y -i "$1" -v quiet -vf scale=iw/2:ih/2 -pix_fmt rgb8 -r 10 "$output_file" && gifsicle -O3 "$output_file" -o "$output_file"
}

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