Skip to content

Instantly share code, notes, and snippets.

@sash13
Forked from rkfg/gifcreate
Last active December 11, 2015 01:29
Show Gist options
  • Save sash13/4523963 to your computer and use it in GitHub Desktop.
Save sash13/4523963 to your computer and use it in GitHub Desktop.
#!/bin/sh
help () {
echo "Usage: `basename $0` [-h] [-n] [-a] [-s hh:mm:ss] [-d ss] [-w px] [-f n] [-S n] [-b n] <filename> <result.gif>
-h show this help
-n turn off subtitles
-a don't open directory with frames in filemanager
-s start time in seconds or as hours:minutes:seconds, default: 0
-d duration in seconds or as hh:mm:ss, default: 5
-w resulting width in pixels (height is set according to the aspect), default: 320
-f use every nth frame to get smaller file; use 2-3 for anime, default: 2
-S use nth subtitles track
-b use n fake (dithered) bits for color, you can also use form r,g,b specifying number of levels per channel (0-255), default: 24
<filename> input filename
<result.gif> output filename
"
}
log () {
echo "$1" >&2
}
STARTTIME=0
DURATION=5
WIDTH=320
FRAMESTEP=2
RUNFM=1
FAKEBITS=24
OUTPUT=$( mktemp -u /tmp/videoXXXXXX.gif )
if [ $# -eq 0 ]
then
help
exit
fi
OPTS=
while getopts hnas:d:w:f:o:S:b: flag
do
case $flag in
h)
help
;;
n)
OPTS="-nosub -noautosub $OPTS"
;;
a)
RUNFM=
;;
s)
STARTTIME=$OPTARG
;;
d)
DURATION=$OPTARG
;;
w)
WIDTH=$OPTARG
;;
f)
FRAMESTEP=$OPTARG
;;
S)
OPTS="-sid $OPTARG $OPTS"
;;
b)
FAKEBITS=$OPTARG
;;
esac
done
shift $(( OPTIND-1 ))
if [ -z "$1" ]
then
log "No input file defined."
exit 1
fi
if [ -z "$2" ]
then
log "No output file defined. The resulting GIF will be saved as $OUTPUT"
else
OUTPUT=$( readlink -f "$2" )
fi
TEMP=`mktemp -d /tmp/mpgifXXXXXX`
FILENAME=$( readlink -f "$1" )
cd "$TEMP"
FPS=$( mplayer "$FILENAME" $OPTS -ss $STARTTIME -endpos $DURATION -vf scale=$WIDTH:-2,framestep=$FRAMESTEP -nosound -vo png:z=9 -speed 100 2>&1 | sed -n 's/.* \([0-9]*\)\.[0-9]* fps .*/\1/p' | head -n 1 )
echo $FPS
DELAY=$(( 100 * STEP / FPS ))
if [ -n "$RUNFM" ]
then
xdg-open "$TEMP"
read -p "Now remove any excessive frames and press Enter to combine them to a nice gif." null
fi
convert -delay $DELAY "$TEMP/*.png" -ordered-dither o8x8,$FAKEBITS -layers optimize-transparency "$OUTPUT"
rm -rf "$TEMP"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment