Universal screen capturing with scrot, imagemagick and ffmpeg

2013-01-23
#ffmpeg #bash #scrot

You can record your screen using only the command line in Linux. The workflow will look like: Create a bunch of screenshots -> Crop screenshots -> Make a movie from images.

§ Making screenshots

scrot is a command-line screen capturing tool. Its basic usage is very simple:

1$ scrot

or

1$ scrot <filename>.png

if you want to specify the output filename.

Let’s write a script that captures the screen every 0.5 seconds and writes the result to screenshots/00000N.png (a filename with leading zeros):

 1#!/bin/bash
 2
 3t=0
 4
 5while true; do
 6    scrot screenshots/`printf "%06g\n" $t`.png
 7    sleep 0.5
 8    t=`expr $t + 1`
 9done
10
11exit 0

§ Cropping screenshots

We will use the mogrify utility from the ImageMagick package:

1$ for file in $(ls); do mogrify -crop <width>x<height>+<x_shift>+<y_shift> ${file}; done

You can determine the width, height, x_shift, and y_shift values by adjusting the rectangular selection tool in GIMP or any image viewer.

§ Creating movie from images

We will use ffmpeg for it. Here is command:

1ffmpeg -qscale 1 -r 20 -b 9600 -i %06d.png <output_file>.mp4

ffmpeg is no longer supported in Ubuntu 14.04, you may use avconv:

1avconv -qscale 1 -r 20 -b 9600 -i %06d.png <output_file>.mp4