Skip to content

Instantly share code, notes, and snippets.

@apconole
Created January 13, 2017 21:50
Show Gist options
  • Save apconole/0f7536e9a2361e0d150ec18c2d5e8563 to your computer and use it in GitHub Desktop.
Save apconole/0f7536e9a2361e0d150ec18c2d5e8563 to your computer and use it in GitHub Desktop.
Record a screen (either full screen or grabbing a screen)
#!/bin/bash
# Copyright (C) 2016, 2017 Red Hat, Inc.
# Screen capture script (with fiddly goodies)
#
# Licensed under the GNU GPL, v2 or greater
#
# To record with sound, you must install an ffmpeg which is properly compiled
# with libalsa support. This may require using RPMFusion repositories, and
# installing the libass libavcodec supported ffmpeg.
NAME=screencast-$(date +%Y%m%d%H%M)
FPS=4
THREADS=3
HEADLESS="no"
NO_SOUND="no"
DELAY=2
while [ $# -gt 0 ]; do
if [ "$1" == "--headless" -o "$1" == "-n" ]; then
HEADLESS="yes"
fi
if [ "$1" == "--name" -o "$1" == "-o" ]; then
shift
NAME="$1"
fi
if [ "$1" == "--no-sound" -o "$1" == "-s" ]; then
NO_SOUND="yes"
fi
if [ "$1" == "--threads" -o "$1" == "-t" ]; then
shift
THREADS="$1"
fi
if [ "$1" == "--fps" -o "$1" == "-f" ]; then
shift
FPS="$1"
fi
if [ "$1" == "--delay" -o "$1" == "-d" ]; then
shift
DELAY="$1"
fi
shift
done
if [ "HEADLESS" != "yes" ]; then
echo "Click the window to capture and get ready!"
tmpfile=/tmp/screengrab.tmp.$$
trap 'touch $tmpfile; rm -f $tmpfile' 0
xwininfo > $tmpfile 2>/dev/null
left=$(grep 'Absolute upper-left X:' $tmpfile | awk '{print $4}');
top=$(grep 'Absolute upper-left Y:' $tmpfile | awk '{print $4}');
width=$(grep 'Width:' $tmpfile | awk '{print $2}');
height=$(grep 'Height:' $tmpfile | awk '{print $2}');
geom="-geometry ${width}x${height}+${left}+${top}"
echo "Geometry: ${geom}"
size="-s ${width}x${height}"
pos="+${left},${top}"
echo "pos=$pos size=$size"
fi
sleep $DELAY
SOUND_OPTS="-f alsa -ac 2 -i pulse -acodec pcm_s16le $NAME-temp.wav"
SOUND_ARGS=""
if [ "$NO_SOUND" == "yes" ]; then
SOUND_OPTS=""
fi
ffmpeg -y $SOUND_OPTS -f x11grab -r $FPS $size -i ${DISPLAY-0:0}${pos} -an -vcodec libx264 -preset ultrafast -threads 0 $NAME-temp.mp4
if [ "$NO_SOUND" == "yes" ]; then
exit 0
fi
if [ "$1" != "--headless" ]; then
echo "Merge audio+video and encode to webm - Abort now..." && read
fi
ffmpeg -i $NAME-temp.mp4 -i $NAME-temp.wav -acodec libvorbis -ab 128k -ac 2 -vcodec libvpx -qscale 8 -me_method full -mbd rd -flags +gmc+qpel+mv4 -trellis 1 -threads $THREADS $NAME.webm
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment