/
mp42gif.sh
executable file
·52 lines (43 loc) · 1.98 KB
/
mp42gif.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#!/usr/bin/env bash
# author: Lilian BESSON
# email: Lilian.BESSON[AT]ens-cachan[DOT]fr
# date: 08-05-2015.
# web: https://bitbucket.org/lbesson/bin/src/master/mp4_to_gif.sh
#
# Convert a MP4 movie to a GIF animated picture..
#
# Usage: mp4_to_gif.sh movie1.mp4 [movie2.mp4 [..]]
#
# Example: mp4_to_gif.sh mysmallmovie.mp4
# Source: http://dtbaker.net/random-linux-posts/convert-video-avi-mpeg-mp4-to-a-gif-animation-on-linux/
#
# Licence: GPLv3 (http://perso.crans.org/besson/LICENSE.html)
#
mkdir -p /tmp/gif/frames/
echo "Deleting everything in /tmp/gif/*.gif?"
rm -rvI /tmp/gif/*.gif
for infile in "$@"; do
echo -e "${green}Working on${white} '${infile}' :"
echo "Deleting everything in /tmp/gif/frames/*.gif?"
rm -rvI /tmp/gif/frames/*.gif
if [ "${infile%.mp4}.mp4" != "${infile}" ]; then
echo -e "${red}Wrong file:${white}'${infile}' is not a MP4 video."
exit 2
fi
outfile="${infile%.mp4}.gif"
if [ -f "${outfile}" ]; then
echo -e "${yellow}Outfile '${outfile}' was already present.${white} I moved it to /tmp/gif/"
mv -vf "${outfile}" /tmp/
fi
small_outfile="$(basename "${outfile}")"
echo -e "${blue}Small outfile is:${white} '${small_outfile}'."
# ffmpeg -i "$infile" -r 10 -s 711x400 /tmp/gif/frames/out%04d.gif
echo -e "${magenta}Launching: ${black}'ffmpeg -i \"$infile\" -r 10 /tmp/gif/frames/out%04d.gif'${white} :"
time ffmpeg -i "$infile" -r 10 /tmp/gif/frames/out%04d.gif
# Combine these images together into a GIF animation:
echo -e "${magenta}Launching: ${black}'gifsicle -delay=10 -loop /tmp/gif/frames/*.gif > \"/tmp/${small_outfile}\"'${white} :"
gifsicle --careful --delay=10 -loop --merge /tmp/gif/frames/*.gif > "/tmp/${small_outfile}"
# Optimise the GIF animation so the file size is smaller:
# echo -e "${magenta}Launching: ${black}'convert -layers Optimize \"/tmp/${small_outfile}\" \"${outfile}\"'${white} :"
# convert -layers Optimize "/tmp/${small_outfile}" "${outfile}"
done