To take a static screenshot of a selected part of my screen, I often use scrot with -s shot.png. This is great for adding illustrations to StackExchange posts. I even found this script to automatically upload such a screenshot to and put a link in my X clipboard!

Let's turn this up to twelve: How do I similarly create a GIF screencast?

There are programs like recordmydesktop, byzanz & co as discussed on Ask Ubuntu that aim to be "user friendly", but in my experience are buggy, inefficient, mostly unscriptable and unsuited for little one-off things like this.

I just want to select an area and record a GIF, with a console command I can understand, not some arcane unscriptable GUI monstrosity.

How can I do this?

up vote 60 down vote accepted

OK then

GIF vimcast!

I started ffcast, did vim, quit ffcast, then converted .avi.gif.

I ran the recording commands in another terminal. Polished script for your $PATH at the end of this answer.

What happened?


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.)


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.

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.


  • 1
    I've created a version who doesn't require bash, but who works on any POSIX compliant shell – Javier López Mar 22 '14 at 4:31
  • 2
    Looks like the command line syntax of ffcast has changed: – Jack O'Connor Sep 30 '14 at 20:25
  • 1
    The simplest way to do it is now ffcast -s rec [filename], though that doesn't give the exact settings you're using in your example. Unfortunately, to give exact settings, you now have to give the entire ffmpeg command. Your call about the best way to update this answer :) – Jack O'Connor Oct 1 '14 at 1:47
  • 4
    Based on the comments on GH, ffcast -s % ffmpeg -f x11grab -show_region 1 -framerate 20 -video_size %s -i %D+%c -codec:v huffyuv -vf crop="iw-mod(iw\\,2):ih-mod(ih\\,2)" $TMP_AVI seems to do the trick. – BenC Oct 1 '14 at 10:35
  • 1
    Some notes on installation of some of these things for anyone else without these already on their system – Louis Maddox Mar 24 '15 at 22:21

For me, the answer was to use ffcast with ffmpeg like so:

ffcast -w % ffmpeg -f x11grab -show_region 1 -framerate 20 -video_size %s -i %D+%c -codec:v huffyuv -vf crop="iw-mod(iw\\,2):ih-mod(ih\\,2)" out.avi

I then used ffmpeg to do the conversion from avi to gif - it's very fast and it keeps the framerate intact:

ffmpeg -i out.avi -pix_fmt rgb24 out.gif

Lastly I used convert in the same way as @anko's answer to optimise the gif, but I set a limit on resource usage to stop convert exiting with a killed message, and I removed the delay as ffmpeg has already handled that:

convert -limit memory 1 -limit map 1 -layers Optimize out.gif out_optimised.gif

This repository help you to create your gif from selection region and also optimize it for you

for my setup(ubuntu 16.04), ffcast doesn't work well as it's not updated on github for quite a while.

so I put up a script using slop( and ffmpeg.

an example:

yay it's working


read -r X Y W H G ID < <(slop -f "%x %y %w %h %g %i")
TMP_AVI=$(mktemp /tmp/outXXXXXXXXXX.avi)

ffmpeg -s "$W"x"$H" -y -f x11grab -i :0.0+$X,$Y -vcodec 
huffyuv -r 25 $TMP_AVI \
&& convert -set delay 5 -layers Optimize $TMP_AVI out.gif 

I had wrote an interactive wrapper script for unix desktops for this reason, and after a year of usage, i am happy to share it there!

Made with byzanz, gifsicle, xdotool, and the script is written in php.

Example output:

[1020px, not resized gif width 1020px, 70 seconds, 50 colors, 65Kb]

enter image description here

It provides good compressed gifs,and is a good showcase for this question.

This is a fairly simple base, ready to be hacked by you.

Functionalities: Gif record at mouse positions or fullscreen, resizing, compression, color compression, reverse/merge, curl upload.

To start a 10 seconds gif record: gif 10

To record multiple times with the same parameters: gif !

To start a fullscreen 5 second gif record: gif 5 --fullscreen

Script runnning, pleasantly recording himself:

[45 seconds, width 645px, full colors, 976kb]
enter image description here

Full 5kb script:


#> php xdotool byzanz gifsicle curl

echo "Usage: ./gif [time in seconds|!] [--fullscreen|-f]\n";
echo "--------------------------------------------------\n";
echo "Gif recorder tool\n";
echo "gif ! to call back last settings\n";
echo "Please move your mouse at the top left corner point\n";
echo "of the wanted gif area. Then press enter.\n";
echo "\n";

#~ Nico KraZhtest | 05/2017 |
#~ Create fluid GIF's fastly
#~ You can set the gif record time as argument: ./gif 10
#~ Default record time is 1 seconde, or set it now:
   $recordTime = 1;
#~ ----------------

$t = @$argv[1];


if (!isset($argv[1]) || @$argv[1] === "!") {
  $t = $recordTime;

if (@$argv[1] === "!") {
  $pos = file_get_contents("./.config/gif/pos");
  $pos = explode("\n", $pos);
  $x1 = $pos[0];
  $y1 = $pos[1];
  $x2 = $pos[2];
  $y2 = $pos[3];
  $gw = $pos[4];
  $gh = $pos[5];
  $t = $pos[6];
  @$GLOBALS['defc'] = $pos[7];
  @$GLOBALS['$rw'] = $pos[8];
   #~ echo $x1." ".$y1." ".$x2." ".$y2." ".$gw." ".$gh." ".$t." ".$defc." ".@$rw;

else if (@$argv[2] === "fullscreen" || @$argv[2] === "--fullscreen" || @$argv[2] === "-f" || @$argv[2] === "f") {
  echo "############\nStarting fullscreen record\n";
  $fs = system("xdpyinfo  | grep 'dimensions:'");
  echo "\n";
  $fs = explode("    ",$fs);
  echo $fs[1];
  $fs = explode(" ",$fs[1]);
  echo $fs[0];
  $fs = explode("x",$fs[0]);
  echo $fs[0]."\n";
  echo $fs[1];
  $x1 = "0";
  $y1 = "0";
  $x2 = "fs";
  $y2 = "fs";
  $gw = $fs[0];
  $gh = $fs[1];
  $t = $argv[1];
  system("mkdir -p ./.config/gif/");
  system("cd ./.config/gif/ && \
          echo '$x1\n$y1\n$x2\n$y2\n$gw\n$gh\n$t\n\n\n\n' > pos");

else {
  $stdin = fopen('php://stdin', 'r');
  $response = rtrim(fgets(STDIN));

  $p1 = system("xdotool getmouselocation");

  $pos1 = explode(" ",$p1);

  $x1 = $pos1[0];
  $x1 = explode(":",$x1);
  $x1 = $x1[1];
  echo "X1: ".$x1;

  $y1 = $pos1[1];
  $y1 = explode(":",$y1);
  $y1 = $y1[1];
  echo " Y1: ".$y1;

  echo "\nNow move your mousse at the bottom right corner.\nThen enter\n";

  $stdin = fopen('php://stdin', 'r');
  $response = rtrim(fgets(STDIN));

  $p2 = system("xdotool getmouselocation");

  $pos2 = explode(" ",$p2);

  $x2 = $pos2[0];
  $x2 = explode(":",$x2);
  $x2 = $x2[1];
  echo "X2: ".$x2;

  $y2 = $pos2[1];
  $y2 = explode(":",$y2);
  $y2 = $y2[1];
  echo " Y2: ".$y2;

  $gw = ($x2 - $x1);
  echo "\nGif width: ".$gw;

  $gh = ($y2 - $y1);
  echo "\nGif height: ".$gh;
  echo "\n".$x1." ".$y1." ".$x2." ".$y2." ".$gw." ".$gh." ".$t."\n";

  system("mkdir -p ./.config/gif/");
  system("cd ./.config/gif/ && \
          echo '$x1\n$y1\n$x2\n$y2\n$gw\n$gh\n$t\n\n\n\n' > pos");

$unix = date_timestamp_get(date_create());

echo "\n".$unix." | Starting ".$t."s gif record\n";

@system("byzanz-record \
        -v             \
        --duration=$t  \
        --x=$x1        \
        --y=$y1        \
        --width=$gw    \
        --height=$gh   \

$named = "gif".$unix;

echo "Saved as ~/Pictures/".$named.".gif\n";

echo "\nOptimize | How many colors to keep? (default 100, max 256) \n";

if (@$argv[1] === "!"){
  $pos = file_get_contents("./.config/gif/pos");
  $pos = explode("\n", $pos);
  $defc = $pos[7];

if (!isset($defc)){
  $defc = readline("Colors: ");

if (empty($defc)){
  $defc = "100";

echo "\nKeeping ".$defc." colors\n";

system("gifsicle --verbose -i ~/Pictures/$named.gif -O5 --colors=$defc -o ~/Pictures/$named\_reduced.gif");

echo "\nOptimize | Resize width in pixels (default 360px) \n";

if (@$argv[1] === "!"){
  $pos = file_get_contents("./.config/gif/pos");
  $pos = explode("\n", $pos);
  $rw = $pos[8];

if (!isset($rw)){
  $rw = readline("Width : ");

if (empty($rw)){
  $rw = "360";

echo "\nResized by ".$rw." pixels width\n";

@system("gifsicle --verbose -i ~/Pictures/$named\_reduced.gif --resize-width $rw -o ~/Pictures/".$named."_optimized.gif");

$opt = "~/Pictures/".$named."_optimized.gif";


echo "\nSpecial | Reverse and merge?\n";

system("xdg-open ~/Pictures/".$named."_optimized.gif > /dev/null");

if (@$argv[1] === "!"){
  $pos = file_get_contents("./.config/gif/pos");
  $pos = explode("\n", $pos);
  $rev = $pos[9];

if (!isset($rev)){
  $stdin = fopen('php://stdin', 'r');
  $rev = rtrim(fgets(STDIN));
  $rev = "1";

if (!isset($rev)){
  $rev = "0";

@system("cd ./.config/gif/ && sed -i '8s/.*/$defc/' pos");
@system("cd ./.config/gif/ && sed -i '9s/.*/$rw/' pos");
@system("cd ./.config/gif/ && sed -i '10s/.*/$rev/' pos");

if ($rev === "1"){
  @system("gifsicle                           \
            -i ~/Pictures/$named\_reduced.gif \
            '#-2-1'                           \
            -o ~/Pictures/".$named."_reversed.gif");

  $inv = "~/Pictures/".$named."_reversed.gif";


  @system("gifsicle                           \
            -i ~/Pictures/$named\_reduced.gif \
          --append $inv                       \
          --resize-width $rw                  \
          -o ~/Pictures/".$named."_merged.gif");


  system("xdg-open ~/Pictures/".$named."_merged.gif > /dev/null");


echo "\n####################";
echo "\nUpload to\n";

$stdin = fopen('php://stdin', 'r');
$response = rtrim(fgets(STDIN));

$m = "~/Pictures/".$named."_merged.gif";
$f = system("du -h $m");
$f = explode("  ",$f);
$f = $f[1];

$www = system('curl                         \
                --progress-bar              \
                -v                          \
                -F "file=@'.$f.'"           \
                -F "api_key=dc6zaTOxFJmzC"  \

$www = json_decode($www);

echo "\n\n".$www->data->id.".gif\n";

echo "\nThanks YOU!\n";

Reverse/merge capability, to create artistic stuffs.

Original (435kb)

enter image description here

Reversed, merged: (826kb)

enter image description here

To install, using phi:

php <(curl install


[1920*1080px, gif 400px, 50 seconds, 100 colors, 2Mb]

enter image description here

Source, with some more explanation and potential updates:

Your Answer


By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.