Home

Over the weekend I was working on a little hack called Help me man!, which is a way for looking up arguments meaning from the man page while you are typing a command, and I wanted to make a gif demo. It was easy making a screencast, Camstudio was a free and easy way to record a particular window, the problem is converting it into a gif.

I’ve heard quite a bit about ffmpeg as a swiss army knife for manipulating videos, so I decided to try it out.

ffmpeg can be downloaded from its homepage, and the documentation is available here.

I couldn’t find a package for it on Ubuntu, so I downloaded the binary file and it ran fine

There are so many command line parameters available that it can be daunting to use this tool. But since my use cases were pretty common, a quick search online turned out the right commands to run.

The simplest command here is this:

$ ./ffmpeg -i demo.mp4 demo.gif

This converts a video file to a gif. The -i parameter specifies the input file, and the last argument is the name of the output file. ffmpeg will look at the name of the output file and determine what is the type of the file it will write to.

However, the gif turned out to be too huge.

One way to reduce the size of the gif is to reduce the screen resolution:

$ ./ffmpeg -i demo.mp4 -vf scale=320x260 demo.gif

The -vf argument takes a scale in the format of WIDTHxHEIGHT. This requires us to calculate both the height and width of the output we want, but there is a better way:

$ ./ffmpeg -i demo.mp4 -vf scale=320:-1 demo.gif

By writing :-1, ffmpeg will automatically scale the video to fit!

We can also crop the video using ffmpeg:

$ ./ffmpeg -i demo.mp4 -ss 00:00:04 demo.gif

The -ss paramter specifies the time you want the video to crop to.

To specify the end time, there are a couple of ways

$ ./ffmpeg -i demo.mp4 -ss 4 -t 10 demo.gif

Note that you can specify the number of seconds as well, so you don’t have to keep typing 00:00:04.

The -t paramter specifics the duration of the video to run, so in the above example, we start from 4 seconds into the video, and run for 10 seconds. Our resulting gif is then 10 seconds long.

$ ./ffmpeg -i demo.mp4 -ss 4 -to 10 demo.gif

If you instead use -to, it will seek to 4 seconds and stop at 10 seconds, the resulting gif is then 6 seconds (10 - 4) long.

The last thing I did to reduce the file size was to reduce the frame rate of the resulting video.

$ ./ffmpeg -i demo.mp4  -r 6 demo.gif

The -r parameter specifies the frame rate of the output file, in this case we set the output frame rate to 6 frames per second.

One last helpful thing, if you call ffmpeg without an output file name, it will print out the info of the input file.

$ ./ffmpeg -i demo.mp4