Usage: ./video2gif.sh -i INPUT_FILE -o OUTPUT_FILE [-t TIME_RANGE] [-s POSITION] [-f FPS] [-w WIDTH] [-h HEIGHT] [-c MAX_COLORS]
-i input video file
-o output gif file
-s start position HH:MM:SS (example: 00:05:30, 05:30, 30)
-t time range in seconds
-f frames per second, by default 10
-w width, by default 320 pixel
-h height, by default respect aspect ratio
-c max colors, by default 64
#!/usr/bin/env bash
fps='10'
height='-1'
loglevel='warning'
max_colors=64
start=
timeout=
width='320'
function args_usage
{
echo "Usage: ${0} -i INPUT_FILE -o OUTPUT_FILE [-t TIME_RANGE] [-s POSITION] [-f FPS] [-w WIDTH] [-h HEIGHT] [-c MAX_COLORS]"
echo ' -i input video file'
echo ' -o output gif file'
echo ' -s start position HH:MM:SS (example: 00:05:30, 05:30, 30)'
echo ' -t time range in seconds'
echo " -f frames per second, by default ${fps}"
echo " -w width, by default ${width} pixel"
echo ' -h height, by default respect aspect ratio'
echo " -c max colors, by default ${max_colors}"
exit 1
}
function args_parse
{
while getopts 'i:o:s:t:f:w:h:c:' opt; do
case $opt in
i)
input_file=${OPTARG};;
o)
output_file=${OPTARG};;
s)
start="-ss ${OPTARG}";;
t)
timeout="-t ${OPTARG}";;
f)
fps=${OPTARG};;
w)
width=${OPTARG};;
h)
height=${OPTARG};;
h)
max_colors=${OPTARG};;
\?)
args_usage;;
esac
done
if [ -z ${input_file+x} ]; then
echo 'Please, specify input file!'
args_usage
fi
if [ -z ${output_file+x} ]; then
echo 'Please, specify output file!'
args_usage
fi
}
args_parse "${@}"
palette_file=$(tempfile --suffix ".png")
ffmpeg -v "${loglevel}" ${start} ${timeout} \
-i "${input_file}" \
-vf "fps=${fps},scale=${width}:${height}:flags=lanczos,palettegen=max_colors=${max_colors}" \
-y "${palette_file}" && \
ffmpeg -v "${loglevel}" ${start} ${timeout} \
-i "${input_file}" -i "${palette_file}" \
-lavfi "fps=${fps},scale=${width}:${height}:flags=lanczos [x]; [x][1:v] paletteuse" \
-f gif \
-y "${output_file}"
rc=${?}
if [ -e ${palette_file} ]; then
rm ${palette_file}
fi
exit ${rc}