Since this is a question-and-answer site strictly requiring a discussion to start with a question, I ask you whether you are interested in a recipe on how to record an application window and to produce a (size-)optimized animated Gif from it. I was asked this on the main site.

up vote 17 down vote accepted

The suggested method works on Linux with X11. Here we go:

For recording, FFmpeg is used, xwininfo for getting the window dimensions including the window handle and some Perl to process the information from xwininfo output.

The following script is invoked as:

ffmpegWinCast.sh <my_window_recording>.(gif|mp4|mpg|...)

After clicking an application window, recording starts. It is stopped by pressing q in the terminal window in which ffmpegWinCast.sh is running.

For posting on TeX.SX, GIF is the preferred output format.

Contents of file ffmpegWinCast.sh:

#!/bin/bash

#Frame rate for GIF output
FPS=12

if [[ -z $1 ]]
then
  echo "Usage:   $(basename $0) <media file>" >&2
  exit 1
fi

outfmt=$(basename $1)
SHM=/dev/shm/$outfmt
outfmt=${outfmt##*.}

echo "Click the application window to be recorded" >&2
GEOM=$(xwininfo | perl -w -n -e '
  if( /Width:\s+(\d+)/ ) {
    $wd=$1;
  }
  elsif(/Height:\s+(\d+)/) {
    $ht=$1;
  }
  elsif(/Relative upper-left Y:\s+(\d+)/) {
    $whd=$1; #window handle height
  }
  elsif(/Absolute upper-left X:\s+(\d+)/) {
    $x=$1;
  }
  elsif(/Absolute upper-left Y:\s+(\d+)/) {
    $y=$1;
  }
  END {
    $ht+=$whd;
    $y-=$whd;
    print "X=$x\n";
    print "Y=$y\n";
    print "WD=$wd\n";
    print "HT=$ht\n";
  }
')

eval $GEOM

if [[ $outfmt = "gif" ||  $outfmt = "GIF" ]]
then
  #record losslessly
  [[ -f $SHM.mkv ]] && rm $SHM.mkv
  ffmpeg -f x11grab -r $FPS -s ${WD}x${HT} -i :0.0+${X},${Y} -vf scale="trunc(iw/2)*2:trunc(ih/2)*2" -c:v libx264 -crf 0 -preset ultrafast $SHM.mkv

  #extract global palette of 265 colours from all frames
  ffmpeg -i $SHM.mkv -vf "palettegen=stats_mode=diff" -y /dev/shm/palette.png

  #re-encode video into GIF
  ffmpeg -i $SHM.mkv -i /dev/shm/palette.png -lavfi "paletteuse=dither=sierra2" -y $1

  rm $SHM.mkv /dev/shm/palette.png
else
  [[ -f $SHM ]] && rm $SHM
  ffmpeg -f x11grab -s ${WD}x${HT} -i :0.0+${X},${Y} -vf scale="trunc(iw/2)*2:trunc(ih/2)*2" $SHM
  mv $SHM $1
fi

For GIF generation, the script executes multiple ffmpeg passes in order to reduce the number of colours to the GIF limit of 256. A dithering method is used for re-encoding the initially recorded video to animated GIF.

Optionally, if GIF was chosen as the output format, the file size can be further reduced using gifsicle.

gifscicle -O3 orig.gif > optimized.gif

An example:

enter image description here

Thanks for sharing! I can definitely see such recordings being helpful in some situations, but I want to add a caveat:

Videos should only illustrate textual descriptions, but not replace them because of

  • accessibility for users with impaired reading skills or devices that don't display images/animated gifs, and printing (or you have to provide an elaborate alt-text)
  • Googleability -- it's fair to assume that we draw most our users (= readers) from search engines. Those unfortunately (?) aren't quite yet at the point of interpreting and indexing animated gifs.
  • unambiguousness -- the gifs will probably mostly be used in answers; imagining a question with an animated gif of code, compiling and an error message makes me shiver ...

Use Licecap. It provides the end user with "simple animated screen captures" and functions on all major distributions (Windows, OSX, Linux). Features include

  • Record directly to .GIF or .LCF.
  • Move the screen capture frame while recording.
  • Pause and restart recording, with optional inserted text messages.
  • Global hotkey (Shift + Space) to toggle pausing while recording
  • Adjustable maximum recording frame rate, to allow throttling CPU usage.
  • Basic title frame, with or without text.
  • Record mouse button presses.
  • Display elapsed time in the recording.
  • Thank you for this, didn't know licecap before. – AlexG Aug 9 at 4:56
  • Just visited its home page. I learned it is for Windows and OSX and doesn't run on Linux natively. – AlexG Aug 9 at 20:23

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .