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.

3 Answers 3


The suggested method works on Linux with X11.

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 [extra non-gif encoder opts] <my_window_recording>.(gif|mp4|mov|...)

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:


#Frame rate for GIF output

# grab output file path from command line
outfile="${@: -1}"

function usage ()
  >&2 echo "Usage:   $(basename $0) [extra (non-gif) encoding options] <media file>.(gif|mp4|mov|...)" >&2

if [[ -z "$outfile" ]] || [[ "$outfile" = "$0" ]]
  exit 1

outfmt=$(basename $outfile 2>/dev/null)
if [[ "$?" -ne 0 ]]
  exit 1


# grab encoder options from command line

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

eval $GEOM

if [[ "$outfmt" = "gif" ||  "$outfmt" = "GIF" ]]
  #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 $outfile

  rm $SHM.mkv /dev/shm/palette.png
  [[ -f "$SHM" ]] && rm $SHM
  if [[ "$outfmt" = "mp4" || "$outfmt" = "MP4" ]]
  then # firefox-compatible mp4/h264
    ffmpeg -an  -f x11grab -s ${WD}x${HT} -i :0.0+${X},${Y} -vf scale="trunc(iw/2)*2:trunc(ih/2)*2" -vcodec libx264 -pix_fmt yuv420p -profile:v baseline -level 3 $moreopts $SHM
    [[ -f "$SHM" ]] && mv $SHM $outfile
  else # other output formats
    ffmpeg -f x11grab -s ${WD}x${HT} -i :0.0+${X},${Y} -vf scale="trunc(iw/2)*2:trunc(ih/2)*2" $moreopts $SHM
    [[ -f "$SHM" ]] && mv $SHM $outfile

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, 2018 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, 2018 at 20:23

You must log in to answer this question.

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