Skip to content

Instantly share code, notes, and snippets.

@andyman
Created September 12, 2015 18:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save andyman/8c8ab869fbcaca9126b1 to your computer and use it in GitHub Desktop.
Save andyman/8c8ab869fbcaca9126b1 to your computer and use it in GitHub Desktop.
Converts a video file to a gif. Needs ffmpeg.
#!/bin/bash
echo "USAGE"
echo "convert_to_gif.sh <video_file_name> <frames_per_second> <width> <start_second> <duration>"
echo "--------------------"
# from here:
# http://superuser.com/questions/556029/how-do-i-convert-a-video-to-gif-using-ffmpeg-with-reasonable-quality
INPUT_FILE=$1
FPS=$2
# width (it will keep the aspect ratio)
WIDTH=$3
# start time in seconds
START_TIME=$4
# duration after the start time
DURATION=$5
ffmpeg -ss $START_TIME -t $DURATION -y -i $INPUT_FILE \
-vf fps=$FPS,scale=$WIDTH:-1:flags=lanczos,palettegen palette.png
ffmpeg -ss $START_TIME -t $DURATION -i $INPUT_FILE -i palette.png -filter_complex \
"fps=$FPS,scale=$WIDTH:-1:flags=lanczos[x];[x][1:v]paletteuse" output.gif
rm palette.png
convert -layers Optimize output.gif $1.gif
rm output.gif
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment