Skip to content

Instantly share code, notes, and snippets.

@bf4
Forked from jaredrummler/video_to_gif_osx.sh
Created May 2, 2016 03:41
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 bf4/2410065bff6654835bacc42b83f9193f to your computer and use it in GitHub Desktop.
Save bf4/2410065bff6654835bacc42b83f9193f to your computer and use it in GitHub Desktop.
Convert Video to GIF on OSX https://github.com/vvo/gifify
for f
do
# Change this value to increase maximum size
GIF_MAX_SIZE=480
dir="$(dirname "$f")"
name="$(basename "$f")"
cd "$dir"
video_properties=$(/usr/local/bin/mplayer -really-quiet -ao null -vo null -identify -frames 0 "$f")
video_width=$(echo $video_properties | sed -e 's/.*\ID_VIDEO_WIDTH=\([0-9]*\).*/\1/')
video_height=$(echo $video_properties | sed -e 's/.*\ID_VIDEO_HEIGHT=\([0-9]*\).*/\1/')
aspect_ratio=$(echo "$video_width $video_height" | awk '{printf "%.5f", $1/$2}')
# shrink larger dimension to GIF_MAX_SIZE;
if [ $video_height -lt $video_width ]
then
final_width=$GIF_MAX_SIZE
final_height=$(echo "$final_width $aspect_ratio" | awk '{printf "%3.0f", $1/$2}')
else
final_height=$GIF_MAX_SIZE
final_width=$(echo "$final_height $aspect_ratio" | awk '{printf "%3.0f", $1/(1/$2)}')
fi
# Don't change dimensions if both are below GIF_MAX_SIZE
if [$video_width -lt $GIF_MAX_SIZE] && [$video_height -lt $GIF_MAX_SIZE]
then
final_width=$video_width
final_height=$video_height
fi
/usr/local/bin/mplayer -ao null -vo png:z=1:outdir=.temp -vf scale=$final_width:$final_height "$f"
/usr/local/bin/convert +repage -fuzz 1.6% -delay 1.7 -loop 0 .temp/*.png -layers OptimizePlus -layers OptimizeTransparency .temp.gif
/usr/local/bin/gifsicle -O3 --colors 256 .temp.gif > "${name%.*}.gif"
# Cleanup
rm -rf .temp
rm -rf .temp.gif
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment