Skip to content

Instantly share code, notes, and snippets.

@Brainiarc7
Forked from dplesca/gifenc.sh
Created December 17, 2016 16:08
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save Brainiarc7/85c4668751f75a5bd106ac6ed352a930 to your computer and use it in GitHub Desktop.
Save Brainiarc7/85c4668751f75a5bd106ac6ed352a930 to your computer and use it in GitHub Desktop.
#gif with text overlay #generator using #ffmpeg - inspired by http://blog.pkh.me/p/21-high-quality-gif-with-ffmpeg.html

###Simple gif generator with text overlay

This generator was inspired from the nice post for high quality gif with ffmpeg. It's pretty simple and only needs ffmpeg as a prerequisite. You might want to change the fontfile path in line 29 for other fonts though.

#!/bin/bash
#parameters:
# $1 - mp4 url
# $2 - start time in format hh:mm:ss.mic
# $3 - duration in seconds
# $4 - text for gif overlay
# $5 - output file without extension
if [ "$#" -ne 5 ]
then
echo "Usage: gifenc.sh \$1 \$2 \$3 \$4 \$5
parameters:
\$1 - mp4 url
\$2 - start time in format hh:mm:ss.mic
\$3 - duration in seconds
\$4 - text for gif overlay
\$5 - output file without extension"
exit 1
fi
#temporary mp4 files
tempvideo="$5.mp4"
tempvideo2="$5$3.mp4"
#params for gif creation
palette="$5.png"
filters="fps=15,scale=480:-1:flags=lanczos"
outputfile="$5.gif"
#first cut a small video from start_time of duration seconds from the initial mp4 file
ffmpeg -v warning -ss $2 -t $3 -i $1 -y $tempvideo
#draw text on video
ffmpeg -v warning -i $tempvideo -vf drawtext="fontfile=impact.ttf:text=\'$4\':fontcolor=white:fontsize=40:x=(w-text_w)/2:y=h-text_h-line_h:borderw=2" -c:a copy -y $tempvideo2
#2 step gif generation - http://blog.pkh.me/p/21-high-quality-gif-with-ffmpeg.html
ffmpeg -v warning -i $5.mp4 -vf "$filters,palettegen" -y $palette
ffmpeg -v warning -i $tempvideo2 -i $palette -lavfi "$filters [x]; [x][1:v] paletteuse" -y $outputfile
#cleanup
rm "$palette"
rm "$tempvideo"
rm "$tempvideo2"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment