Last active
October 4, 2021 13:42
-
-
Save Ultrabenosaurus/bbd4606ff444b4dc89c7e7b93b21c3ee to your computer and use it in GitHub Desktop.
Simple bash script to convert MP4 videos to GIF
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
############################################# | |
# | |
# mp4-to-gif | |
# | |
# Convert MP4 videos to GIF images | |
# Requires ffmpeg and imagemagick | |
# | |
# -g and --gifsicle options create a second optimised GIF for your consideration | |
# this obviously requires the gifsicle package | |
# http://www.lcdf.org/gifsicle/ | |
# | |
# Add to PATH and alias for easy converting | |
# alias m2g='mp4-to-gif.sh' | |
# | |
# Author: Daniel Bennett | |
# License: BSD 3-Clause <https://opensource.org/licenses/BSD-3-Clause> | |
# Source: https://gist.github.com/Ultrabenosaurus/bbd4606ff444b4dc89c7e7b93b21c3ee | |
# | |
# Only tested on Windows 10 using the Ubuntu shell under Windows Subsystem for Linux | |
# I fully accept the possibility that this code is a demonstration of how not to code for bash | |
# but it works (for me) | |
# | |
############################################# | |
e=0 | |
if [ $# -eq 0 ] || [ ! -f "`realpath "$1"`" ] ; then | |
echo "No file specified for conversion!" | |
echo "Usage: mp4-to-gif.sh path/to/video.mp4" | |
e=1 | |
fi | |
if [ "$(which ffmpeg)" = "" ] ; then | |
if [ $e = 1 ] ; then | |
echo "" | |
fi | |
echo "ffmpeg is required but not installed" | |
e=1 | |
fi | |
if [ "$(which convert)" = "" ] ; then | |
if [ $e = 1 ] ; then | |
echo "" | |
fi | |
echo "imagemagick is required but not installed!" | |
e=1 | |
fi | |
if [ $# -eq 2 ] ; then | |
if [ $2 = "-g" ] || [ $2 = "--gifsicle" ] ; then | |
if [ "$(which gifsicle)" = "" ] ; then | |
if [ $e = 1 ] ; then | |
echo "" | |
fi | |
echo "gifsicle is not installed - install it or stop using --gifsicle / -g options" | |
e=1 | |
fi | |
fi | |
fi | |
if [ $e = 1 ] ; then | |
echo "" | |
echo "Add to PATH for easy converting" | |
echo "Add alias m2g='mp4-to-gif.sh' to your ~/.profile" | |
echo "" | |
echo "Only tested on Windows 10 using the Ubuntu shell under Windows Subsystem for Linux" | |
exit 0 | |
fi | |
n=$RANDOM | |
t="mp4-to-gif" | |
r=10 | |
s=320 | |
p=`realpath "$1"` | |
d=`dirname "$p"` | |
f=`basename "$p"` | |
f=${f%.*} | |
while [ -d "./$t-$n" ] ; do | |
n=$RANDOM | |
done | |
mkdir -p ./$t-$n | |
ffmpeg -i "$p" -vf scale=$s:-1:flags=lanczos,fps=$r ./$t-$n/ffout%03d.png | |
convert -loop 0 ./$t-$n/ffout*.png "$d/$f.gif" | |
rm -r ./$t-$n | |
if [ $# -eq 2 ] ; then | |
if [ $2 = "-g" ] || [ $2 = "--gifsicle" ] ; then | |
gifsicle "$d/$f.gif" --colors 256 --optimize=3 > "$d/$f.g.gif" | |
fi | |
fi | |
############################################# | |
# | |
# Copyright 2019, Daniel Bennett | |
# | |
# Redistribution and use in source and binary forms, with or without modification, are | |
# permitted provided that the following conditions are met: | |
# | |
# 1. Redistributions of source code must retain the above copyright notice, this list | |
# of conditions and the following disclaimer. | |
# | |
# 2. Redistributions in binary form must reproduce the above copyright notice, this | |
# list of conditions and the following disclaimer in the documentation and/or other | |
# materials provided with the distribution. | |
# | |
# 3. Neither the name of the copyright holder nor the names of its contributors may be | |
# used to endorse or promote products derived from this software without specific | |
# prior written permission. | |
# | |
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY | |
# EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES | |
# OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT | |
# SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, | |
# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED | |
# TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR | |
# BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | |
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN | |
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH | |
# DAMAGE. | |
# | |
# https://opensource.org/licenses/BSD-3-Clause | |
# | |
############################################# |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment