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
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
:
#!/bin/bash
#Frame rate for GIF output
FPS=12
# 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" ]]
then
usage
exit 1
fi
outfmt=$(basename $outfile 2>/dev/null)
if [[ "$?" -ne 0 ]]
then
usage
exit 1
fi
SHM=/dev/shm/$outfmt
outfmt=${outfmt##*.}
# grab encoder options from command line
moreopts="${@:1:$(($#-1))}"
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 $outfile
rm $SHM.mkv /dev/shm/palette.png
else
[[ -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
fi
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:
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.
-
-
Just visited its home page. I learned it is for Windows and OSX and doesn't run on Linux natively.– AlexGCommented Aug 9, 2018 at 20:23