Skip to content

Instantly share code, notes, and snippets.

@superdaigo
Last active June 21, 2023 02:34
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 superdaigo/bbf884fdd1336b8c3d0987f91a2537d2 to your computer and use it in GitHub Desktop.
Save superdaigo/bbf884fdd1336b8c3d0987f91a2537d2 to your computer and use it in GitHub Desktop.
Convert movie file to GIF animation using ffmpeg and jq
#!/usr/bin/env bash
# Convert movie file to GIF animation using ffmpeg and jq
set -e -o pipefail
MAX_WIDTH=1200
MAX_HEIGHT=1200
# ffprobe
stream_info=$(ffprobe -v quiet -print_format json -show_streams "$1")
width=$(echo "$stream_info" | jq '.streams[0].width')
height=$(echo "$stream_info" | jq '.streams[0].height')
# resize
if [ ${width} -gt ${height} ]; then
out_width=$width
out_height=-1
if [ ${width} -gt ${MAX_WIDTH} ]; then
out_width=$MAX_WIDTH
fi
else
out_height=$height
out_width=-1
if [ ${height} -gt ${MAX_HEIGHT} ]; then
out_height=$MAX_HEIGHT
fi
fi
scale="$out_width:$out_height"
# ffmepg
ffmpeg -i "$1" -vf scale="$scale" -r 10 "$1".gif
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment