8

Many times, OPs are good enough to link videos to their question(s) so that we can have a view of the problem they are experiencing. The only problem with this is that it links to a video off of Ask Different. The solution (in most cases, there's no audio for example) is to create an animated GIF so that the picture is "in line" with the question and not an external link.

There's a good answer on SuperUser on how to do this. However, I wanted something that could automate this process as the commands are a bit onerous to remember each time.

My goal was to do this with as few 3rd party utilities as possible and to automate it so that it provided a good quality animation while respecting the 2MB image limit.

You will need ffmpeg and the script below. You can call it whatever you like (I used ffmp4gif); and make sure it's in a directory in your path. All operations will be done in the current directory.

It takes a single argument, that being the input filename. For example:

$ ffmp4gif foobar.mp4

This will create an animated GIF with the name foobar.gif in the same directory.

It's a pretty cut and dry script... feel free to make/suggest improvements.


#!/bin/bash

# $1 = input file name

# User editable variables

FS="1900000"                #Maximum file size; ffmpeg adds 1K to the file
FPS="fps=10"                #Max frames per second
SCALE="scale=320"           #X-Width pixels

# Not-so-user-editable varibles

PALETTE=`mktemp /tmp/palette.XXXXXX.png`             # Creates the palette file
FLAGS1="flags=lanczos,palettegen"                    # Flags for palette generation
FLAGS2="flags=lanczos[x];[x][1:v]paletteuse"         # Flags for animated GIF generation


# Function Definitions --------------------


function cleanup ()
{
    #Cleans up temp files used
    echo Cleaning up....
    rm -f $PALETTE
    echo Done....
}

function trap_ctrlc ()
{
   # clean up temp files created
   echo "Control-C was detected."
   cleanup
   exit 2

}

# Main Script ----------------------


# Initialzie trap to call tracp_ctrlc function
# if/when SIGINT is received 
trap "trap_ctrlc" 2


# Check if argument was supplied
if [ $# -eq 0 ] 
then
  echo "No arguments supplied. Exiting"
  exit 1
fi

# Set FILE to passed parameter
FILE="${1}"


# Check if file exists
if [ ! -f "${FILE}" ]
then
  echo "File not found.  Exiting"
  exit 3
fi

# Set the Output file name
OUTFILE=$(basename "${FILE}")
OUTFILE="${OUTFILE%.*}"

# Create the palette file
echo "Creating Palette"
ffmpeg -y  -i ${FILE} -vf ${FPS},${SCALE}:-1:${FLAGS1} ${PALETTE} >/dev/null 2>&1

# Create the GIF using the palette file
echo "Creating Animated GIF File:  ${OUTFILE}.gif"
ffmpeg -y  -i ${FILE} -i $PALETTE -filter_complex "${FPS},${SCALE}:-1:${FLAGS2}" -fs ${FS} ${OUTFILE}.gif >/dev/null 2>&1

# Cleanup temp files and exit
cleanup
exit 0
  • 1
    Awesome, thanks Allan! I probably would use mktemp for the palette file (and set a trap to remove it even if the user exits with Ctrl-C) but that‘s definitively a detail :-) – nohillside Jan 5 at 9:33
  • @nohillside - thanks for the suggestions! I added both though I don't think trapping would be much of an issue - I had to add a sleep 1000 statement just to test it out as it was just too fast! But, it's in there if someone uses it on a slow enough computer. :-) – Allan Jan 8 at 21:50
  • 2
    I really hate videos as information carrier. But for this you really deserve a medal of distinction in reducing the carbon footprint of these videos and load-times (that is really added quality in the life-time for users!) – LangLangC Jan 12 at 13:30

You must log in to answer this question.

Browse other questions tagged .