# Converting your video (or photos) to “animated GIF”¶

doc

Before I say “It’s easy to convert your video to an animated GIF”, there is something I have to say.

First of all, ask yourself “Why should I make animated GIF?”. And, you should know that animated GIFs are annoying for “your readers” in most cases.

There are not many viewers that can handle animated GIF properly than you think. Also, there is almost no chance that the reader can perform operations such as playback start, end, pause, etc. In particular, when viewing an animated GIF referenced from html in a browser, in most cases it is necessary to reload the entire page to start from the beginning. In addition, animated GIFs often make the browser run very slowly.

Another thing to note is that GIF has distribution and publishing issues related to licensing.

If you understand these things and still want an animated GIF, for example, just do as follows:

[me@host: ~]$# convert your video (from 30s to 35s) to animated GIF [me@host: ~]$ ffmpeg -y -ss 30 -i your_video.mp4 -t 5 -final_delay 1000 out.gif
...
[me@host: ~]$# specify framerate [me@host: ~]$ ffmpeg -y -ss 30 -i your_video.mp4 -t 5 -r 6 -final_delay 1000 out.gif
...
[me@host: ~]$# convert your photos (named "img1.jpg", ...) to animated GIF [me@host: ~]$ ffmpeg -y -r 2 -i "img%d.jpg" -final_delay 500 out.gif
...
[me@host: ~]$# specify loop count (-loop 2 -> loop three times) [me@host: ~]$ ffmpeg -y -ss 30 -i your_video.mp4 -t 5 -loop 2 -final_delay 1000 out.gif
...


In my case, when I want to explain something with only a few key points, I may want to use an animated GIF rather than a few minutes of video. However, even if so, usually the slide show approach is more pleasing.

Perhaps the most typical thing to justify the use of animated GIFs is a video preview. Even if you come up with other use case, be calm and think seriously about whether it really benefits your readers.

## Converting the animated GIF to (Stone age technology) MHT¶

Because “Windows Photo Viewer” bundled on Windows 7 does not recognize animated GIF, if you belong to a large company (or a team with an old mindset) that tends to think “security is a closed network!”, even if you make an animated GIF for a brief technical explanation purpose, you will have trouble with how to distribute it.

If you belong to such an unfortunate team and all team members are using Windows, wrapping that animated GIF in a mht (Paleolithic technology) would be helpful. Double-clicking “.mht” triggers Microsoft Internet Explorer or Microsoft Outlook, both of which recognize animated GIFs.

Python script that wraps one animated GIF in mht:

img2mht.py
# -*- coding: utf-8 -*-
#
# Usage: python img2mht.py yourimg.gif
#
from __future__ import unicode_literals

from email.mime.image import MIMEImage
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
import sys
import os
import io

filename = sys.argv[1]
filebase = os.path.basename(filename)

outer = MIMEMultipart()
outer['Subject'] = filebase
outer.preamble = 'This is a multi-part message in MIME format.\n'

msg = MIMEText("""\
<html>
<body>
<img src="{}"/>
</body>
</html>
""".format(filebase), _subtype="html")
outer.attach(msg)
outer.attach(img)
temp = outer.as_string().split("\n")
temp[0] = temp[0].replace(
"Content-Type: multipart/mixed; ",
"Content-Type: multipart/related; type=text/html; ")
with io.open(filebase + ".mht", "w") as fo:
fo.write("\n".join(temp))


Warning

Don’t let mht spread around the Internet world, not limited to mht created by this script. mht is a Microsoft Windows-specific solution and an extremely outdated technology.