Here's what worked for me:
ffmpeg -i animated.gif -movflags faststart -pix_fmt yuv420p -vf "scale=trunc(iw/2)*2:trunc(ih/2)*2" video.mp4
movflags – This option optimizes the structure of the MP4 file so the browser can load it as quickly as possible.
pix_fmt – MP4 videos store pixels in different formats. We include this option to specify a specific format which has maximum compatibility across all browsers.
vf – MP4 videos using H.264 need to have a dimensions that are divisible by 2. This option ensures that’s the case.
Source: http://rigor.com/blog/2015/12/optimizing-animated-gifs-with-html5-video
OK then
I started ffcast
, did vim
, quit ffcast
, then convert
ed .avi
→.gif
.
I ran the recording commands in another terminal. Polished script for your $PATH
at the end of this answer.
What happened?
Capturing
FFcast helps the user interactively select a screen region and hands over the geometry to an external command, such as FFmpeg, for screen recording.
ffcast
is the glorious product of some hacking at the Arch Linux community (mainly lolilolicon). You can find it on github (or in the AUR for Archers). Its dependency list is just bash
and ffmpeg
, though you'll want xrectsel
(AUR link) for interactive rectangle selection.
You can also append ffmpeg
flags right after the command. I set -r 15
to capture at 15 frames per second and -codec:v huffyuv
for lossless recording. (Play with these to tweak the size/quality tradeoff.)
GIFfing
ImageMagick can read .avi
videos and has some GIF optimisation tricks that drastically reduce file size while preserving quality: The -layers Optimize
to convert
invokes the general-purpose optimiser. The ImageMagick manual has a page on advanced optimisations too.
Final script
This is what I have in my $PATH
. It records into a temporary file before converting.
#!/bin/bash
TMP_AVI=$(mktemp /tmp/outXXXXXXXXXX.avi)
ffcast -s % ffmpeg -y -f x11grab -show_region 1 -framerate 15 \
-video_size %s -i %D+%c -codec:v huffyuv \
-vf crop="iw-mod(iw\\,2):ih-mod(ih\\,2)" $TMP_AVI \
&& convert -set delay 10 -layers Optimize $TMP_AVI out.gif
Thanks to BenC for detective work in figuring out the correct flags after the recent ffcast
update.
If you'd like to install the dependencies on a Debian-based distro, Louis has written helpful installation notes.
Best Answer
You can make a looping GIF by setting a bit in the header of the file (been doing that since 1989), but there is no such facility that I know of in the
.mp4
files. For them looping is controlled via an additional, player dependent, file.The absence of that also is the reason why you don't have any looping
.mp4
on YouTube.