Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 25 You must be signed in to star a gist
  • Fork 5 You must be signed in to fork a gist
  • Save mcmoe/c76895ee86bd5293d58aca7a75afb6b2 to your computer and use it in GitHub Desktop.
Save mcmoe/c76895ee86bd5293d58aca7a75afb6b2 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 note 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

Simplify usage via a bash function

You can define the below function and add it to your .bashrc or the like for use at anytime:

function v2g() {
    src="" # required
    target="" # optional (defaults to source file name)
    resolution="" # optional (defaults to source video resolution)
    fps=10 # optional (defaults to 10 fps -- helps drop frames)

    while [ $# -gt 0 ]; do
        if [[ $1 == *"--"* ]]; then
                param="${1/--/}"
                declare $param="$2"
        fi
        shift
    done

    if [[ -z $src ]]; then
        echo -e "\nPlease call 'v2g --src <source video file>' to run this command\n"
        return 1
    fi

    if [[ -z $target ]]; then
        target=$src
    fi

    basename=${target%.*}
    [[ ${#basename} = 0 ]] && basename=$target
    target="$basename.gif"

    if [[ -n $fps ]]; then
        fps="-r $fps"
    fi

    if [[ -n $resolution ]]; then
        resolution="-s $resolution"
    fi

    runcommand="ffmpeg -i "$src" -pix_fmt rgb8 $fps $resolution "$target" && gifsicle -O3 "$target" -o "$target""

    echo ""
    echo ".------------------------."
    echo "|\\\\////////       90 min |"
    echo "| \\/  __  ______  __     |"
    echo "|    /  \|\.....|/  \    |"
    echo "|    \__/|/_____|\__/    |"
    echo "| A                      |"
    echo "|    ________________    |"
    echo "|___/_._o________o_._\___|"
    echo ""
    echo "ツ running >> $runcommand"
    echo "ツ ..."
    echo ""

    eval " $runcommand"
    osascript -e "display notification \"$target successfully converted and saved\" with title \"v2g complete\""
}

You can then call it as such

v2g --src orig.mp4 --target newname --resolution 800x400 --fps 30
@brotherko
Copy link

Hello thanks for the awesome script. But this is the error message that Im receiving on my Macbook(it's a M1 machine I don't know if it's related)

Unrecognized option 'r 30'.
Error splitting the argument list: Option not found

@albeorla
Copy link

albeorla commented Jun 2, 2022

ffmpeg -i in.mov -pix_fmt rgb8 -r 10 output.gif && gifsicle -O3 output.gif -o output.gif worked for me, however, I had the same issue as the comment above when using the script on an M1.

Maybe something going on with the following line?

if [[ -n $fps ]]; then
    fps="-r $fps"
fi

@mcmoe
Copy link
Author

mcmoe commented Jun 3, 2022

@brotherko @albertjorlando What does the echo command print out?

Copy link

ghost commented Jul 25, 2022

I changed the name of the method but this is the response I get for the issue above.

gifconvert --src quantity_test_scenarios.mp4
ffmpeg -i quantity_test_scenarios.mp4 -pix_fmt rgb8 -r 10  quantity_test_scenarios.gif && gifsicle -O3 quantity_test_scenarios.gif -o quantity_test_scenarios.gif
ffmpeg version 5.0.1 Copyright (c) 2000-2022 the FFmpeg developers
  built with Apple clang version 13.1.6 (clang-1316.0.21.2.5)
  configuration: --prefix=/opt/homebrew/Cellar/ffmpeg/5.0.1_3 --enable-shared --enable-pthreads --enable-version3 --cc=clang --host-cflags= --host-ldflags= --enable-ffplay --enable-gnutls --enable-gpl --enable-libaom --enable-libbluray --enable-libdav1d --enable-libmp3lame --enable-libopus --enable-librav1e --enable-librist --enable-librubberband --enable-libsnappy --enable-libsrt --enable-libtesseract --enable-libtheora --enable-libvidstab --enable-libvmaf --enable-libvorbis --enable-libvpx --enable-libwebp --enable-libx264 --enable-libx265 --enable-libxml2 --enable-libxvid --enable-lzma --enable-libfontconfig --enable-libfreetype --enable-frei0r --enable-libass --enable-libopencore-amrnb --enable-libopencore-amrwb --enable-libopenjpeg --enable-libspeex --enable-libsoxr --enable-libzmq --enable-libzimg --disable-libjack --disable-indev=jack --enable-videotoolbox --enable-neon
  libavutil      57. 17.100 / 57. 17.100
  libavcodec     59. 18.100 / 59. 18.100
  libavformat    59. 16.100 / 59. 16.100
  libavdevice    59.  4.100 / 59.  4.100
  libavfilter     8. 24.100 /  8. 24.100
  libswscale      6.  4.100 /  6.  4.100
  libswresample   4.  3.100 /  4.  3.100
  libpostproc    56.  3.100 / 56.  3.100
Unrecognized option 'r 10'.
Error splitting the argument list: Option not found

I tried the same, using a .mov file but the results were the same.

Update: I attempted the same file but manually using this command, and it worked fine.

ffmpeg -i quantity_test_scenarios.mp4 -pix_fmt rgb8 -r 10 output.gif && gifsicle -O3 output.gif -o 
output.gif

@mcmoe
Copy link
Author

mcmoe commented Jul 29, 2022

I'm now using eval to run the command. Let me know if this fixes your issue @davidg-zipline @albertjorlando @brotherko

Copy link

ghost commented Jul 29, 2022

Very nice! I tested your new gist and it worked flawlessly on mac M1.

@albeorla
Copy link

Thanks, @mcmoe !!! Works for me as well.

@nemwiper
Copy link

nemwiper commented Aug 2, 2022

This is awesome! Thanks!

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