Home/News | About | Download | Documentation | Forum | Bug Reports | Contact | Donations | Consulting | Projects | Legal | Security | FATE


This forum has not been maintained for a long time and will probably get deleted in the near future.
For faster responses to your questions, please use StackOverflow instead and tag your questions with "FFmpeg".
If you need a backup of the posts from this forum, please contact me directly.

[Tutorial] How to make gifs using ffmpeg (for beginners)

A collection of useful tutorials for some common tasks.

[Tutorial] How to make gifs using ffmpeg (for beginners)

Postby zero-kanada » Tue May 23, 2017 6:19 pm

I have for a long time tried to learn how to make gifs in Linux, but when I look up tutorials most of the tools are only available for Windows/Mac. I have tried an array of different things until I decided to give ffmpeg a shot. The problem with this is that there isn’t (to my knowledge) a comprehensive tutorial on how to do this, so I had to hunt every piece of information I needed in order to make a good gif using this program, which is a slow learning process. So I have decided to compile everything I have learnt over the past couple of days into a tutorial for others who, like me, are not vexed into ffmpeg and are not particularly tech-savvy.

It should be noted that even though I used Linux (Mint), it probably works exactly the same on Windows, so it should help people looking to make gifs in that platform as well.

Introduction
First of all, if you are reading this and are unfamiliar with how ffmpeg works, you might want to check out the youtube videos by lcc0612 here and maybe the next oneas well. Most of it is irrelevant for gif making, but he explains very well the syntax of the program, and it did help me a lot understanding.

Without further ado, let’s jump in.

Let’s start at the end, which is a very good place to start.

In order to make a gif out of a video, I type (well, I copy/paste it, but technicalities) this is code into the terminal window (masking sure I’m in the path where my video is):

Code: Select all
ffmpeg -y -ss 00:04:08.5 -t 4 -i edafd.mp4 -vf fps=30,scale=400:-1:flags=lanczos,palettegen palette.png && ffmpeg -ss 00:04:08.5 -t 4 -i edafd.mp4 -i palette.png -filter_complex "setpts=1.2*PTS,crop=w=400:h=350:x=-200:y=100,subtitles=subtitle.ass,fps=10,scale=400:-1:flags=lanczos[x];[x][1:v]paletteuse" edafd.gif


And get the following gif in return:

Image

It looks pretty much ok, isn’t it?

Now, we are going to break down the above code in order to understand what each part does.

In order to make a gif out of the video, is as simple as just typing:

Code: Select all
ffmpeg -ss 00:04:08.5 -t 4 -i  edafd.mp4 edafd.gif


This indicates the program:

-ss 00:04:08.5 : the starting point where you want to start to capture, in hh:mm:ss
-t 4 : how long you want to record for in seconds
-i edafd.mp4 : the name of the output
edafd.gif : the output

And the resulting gif would be:

Image


But this is ugly. In fact, I named it ugly.gif. You’d be ashamed of going around the internet with that. You’d rather spend hours on giphy trying to find the perfect reaction gif that you remember you saw but don’t know how to describe.

So the first thing you need to do is make the gif look decent. A kind soul wrote a tutorial on how to make gifs look good. It looks great, but honestly it’s a big heavy for me. Thankfully, some other people broke it down into what needs to be added. Basically, you need to create a palette of colors of the section of the video you are trying to gif. This is what the first part of the code above is there to accomplish:

Code: Select all
ffmpeg -y -ss 00:04:08.5 -t 4 -i edafd.mp4 -vf fps=30,scale=400:-1:flags=lanczos,palettegen palette.png


The output from this is a small image with the palette of colors from the video. I just use this code, changing, of course, source, time and everything particular for each project.

Using this palette we can make much better quality gif. Next, I’m going to explain what each of the filters are and how they are used.

Code: Select all
ffmpeg -ss 00:04:08.5 -t 4 -i edafd.mp4 -i palette.png -filter_complex "setpts=1.2*PTS,crop=w=400:h=350:x=-200:y=100,subtitles=subtitle.ass,fps=10,scale=400:-1:flags=lanczos[x];[x][1:v]paletteuse" edafd.gif


Note: now the palette is included as an input.

Changing the speed
The first thing to note is that all the filters go after -filter_complex and are seen above between quotation marks. The first one can be used quote straightforwardly to change the speed of the gif.

Code: Select all
Setpts=1.2*PTS


I suppose that there’s some theory to explain here, but the gist of the question is, if you multiply for a number larger than one the gif slows down, if you multiply by a number lower than 1, it speeds up. You can find more information about this here.

Crop
The next one up is the crop function.

Code: Select all
crop=w=400:h=350:x=-200:y=100,


We are using 4 parameters here: w and h indicate the pixel width and height you are cropping to. The x and y settings allow to change the onset of the crop, automatically it will be centered.

There are other parameters for this filter, but I will not go into them. For more information, you can watch the aforementioned videos, other manuals or the documentation.

Subtitles
For me, personally, this was the trickier part of all, but it got a lot simpler once I was able to figure out how it worked.

Code: Select all
subtitles=subtitle.ass


The syntax is really simple, simply type subtitles followed by the subtitle filename. It accepts .srt as well as .ass, but I prefer to use .ass because I can do the formatting on the file itself, while .srt would need to be formatted with the filter settings in ffmpeg itself. I personally keep a subtitle file and use that one every time I need to add subs to a gif.

Code: Select all
[Script Info]
; This is a Sub Station Alpha v4 script.


[V4 Styles]
Format: Name, Fontname, Fontsize, PrimaryColour, SecondaryColour, TertiaryColour, BackColour, Bold, Italic, BorderStyle, Outline, Shadow, Alignment, MarginL, MarginR, MarginV, AlphaLevel, Encoding
Style: Default,Calibri,35,63968,2,3,0,-1,-1,1,3,0,2,10,10,10,0,00
Style: Top,Calibri,35,63968,2,3,0,-1,-1,1,3,0,6,10,10,10,0,0
Style: Middle,Calibri,35,63968,2,3,0,-1,-1,1,3,0,10,10,10,10,0,0
Style: White,Calibri,35,16777215,2,3,0,-1,-1,1,3,0,2,10,10,10,0,0
Style: WTop,Calibri,35,16777215,2,3,0,-1,-1,1,3,0,6,10,10,10,0,0
Style: WMiddle,Calibri,35,16777215,2,3,0,-1,-1,1,3,0,10,10,10,10,0,0

[Events]
Format: Marked, Start, End, Style, Name, MarginL, MarginR, MarginV, Effect, Text
Dialogue: Marked=0,0:00:00.00,0:00:06.00,White,,0000,0000,0000,,Example text



The script is really simple. I just keep 6 styles defined, in yellow and white, top, middle and bottom, and just switch it up as needed. You can just simply copy the above text into a text file, name it subtitle.ass and you’re good to go. After that, each of the styles can be defined as you prefer, changing size, color, margins, etc. For a guide on how .ass scripts work, I found this one really useful.

Note 1: Filters are applied linearly. If you are using the crop function, remember to use it before the subtitles, otherwise they will be cropped.

Note 2: Sometimes the palette gives the subtitles a weird color. If that happens, you can add the subtitles when creating the palette, this way it takes its color into account, as following:

Code: Select all
ffmpeg -y -ss 00:04:08.5 -t 4 -i edafd.mp4 -vf “subtitles.ass,fps=30,scale=400:-1:flags=lanczos,palettegen” palette.png


Frames per second and scale
The following two filters allow to define the frames per second. I have found that most of the times a large amount of frames do not greatly improve the gif quality. You can probably pass with 10-20 most of the times. The scale defines the final width and height. In the example, the final width is 400, and -1 indicates that the height will be adjusted proportionally. Alternative, values for both width and height can be set.

Code: Select all
fps=10
scale=400:-1


Using the palette
As I explained before, I don’t really understand the palette filter, I use always the same set up as above.
Code: Select all
flags=lanczos[x];[x][1:v]paletteuse


And thankfully that makes the gif look like the first one I inserted.

So we have reached the end. Of course, there are many more options and possibilities, including changing shades, colors, and other nifty editing filters, but these are not specific for gif making and any ffmpeg tutorial will probably cover them very well.

I hope some people will find this tutorial helpful. As I said, I have only been using ffmpeg for a couple of days, so there's probably a lot to be improved upon. Please feel free to expand on this tutorial if you want.

Note: in Linux at least, some of the features may not work at first because it is necessary to install other dependencies. If you find yourself with this problem, google the offending name, and you will find which dependency is necessary to install to make it work.
zero-kanada
 
Posts: 1
Joined: Sat May 20, 2017 7:52 pm

Return to Tutorials

Who is online

Users browsing this forum: No registered users and 2 guests