We help IT Professionals succeed at work.

Bash Script to Take Automated Screenshots in Linux (Ubuntu) at second intervals?

rickd12
rickd12 asked
on
2,377 Views
Last Modified: 2013-11-15
I would like to create a bash script to take screenshots using a program like scrot or imagemagick, I would like to set it to take them at something like 1 second intervals and I am wondering if it is possible to take the screenshots at even faster intervals (such as 5 per second) which could effectively provide enough images to make a slideshow / movie. I am aware of other screen capture programs (like istanbul, xvidcap, etc) but I would like to do this with a simiple bash script. Thanks
Comment
Watch Question

If you save this script as "screenshots.sh test 10", it will save 10 screenshots in the current directory as "test0.png - test9.png". If you call it as "screenshots.sh test", it will save screenshots until interrupted.

The saving process takes some time - a little less than a second. I don't know yet whether this can be sped up, but I'll take a look.
#!/bin/bash
 
BASENAME=$1
MAX=$2
 
if [ "$BASENAME" == "" ]; then
  echo "No name specified."
  exit 1
fi
 
I=0
while [ 1 ]; do
  FILENAME=${BASENAME}${I}.png
 
  echo "Saving ${FILENAME}"
  import -window root -silent ${FILENAME}
  I=$[ $I + 1 ]
 
  if [ $MAX -gt 0 -a $MAX -eq $I ]; then
    break
  fi
don

Open in new window

Top Expert 2008

Commented:
Maybe byzanz is what you're looking for? It's command line based too:

Description: Small screencast creator
 Byzanz is a command-line/GNOME Panel applet allowing you to record your
 current desktop or parts of it to an animated GIF file. This is especially
 useful for publishing on the web, since every browser understands the GIF
 format.
 .
 Byzanz is not limited to gnome, you can use it under non-GTK environments
 like KDE too.
 .
 Homepage: http://people.freedesktop.org/~company/byzanz/

Author

Commented:
wilhelm_voigt, thank you for your response, when I save the filename as you suggested: "screenshots.sh test" it does not show as being an executable file and I am unable to execute the file by opening it through clicking or through the command line.
Top Expert 2008

Commented:
The name of the file would just be "screenshots.sh" (without the "test", which is only  command line parameter to it).

After saving the file, you need to make it executable by changing its permissions. To do this, open your favourite terminal emulator, and enter this command:

chmod 755 /path/to/screenshots.sh

Now you should be able to start it using the command:

/path/to/screenshots.sh test

where "test" is just the name of the screenshot fes that will be generated.
With the current script it's not possible to simply doubleclick on it without further modifications, because it expexts the name of the screenshots file to be passed to it.

I'll write a modified version and will post it here in a few minutes.

Author

Commented:
agriesser, thank you, I installed the byzanz tool, however I am having some trouble getting it to work properly and the documentation I have found so far is not especially helpful, it says:

  -d, --duration=SECS      Duration of animation (default: 10 seconds)
  --delay=SECS             Delay before start (default: 1 second)

When I try: byzanz-record --duration=10 (or 100, 1000, etc) it still only gives me one screenshot and just names it "duration10". I have tried every other combination I could think of with -d, --duration and they only give errors, do you know the proper command to make the program take continuous screenshots for a certain time interval? thanks
Top Expert 2008

Commented:
OK, the code below should work without command line parameters, but you might need to modify the defaults.
There are four configuration variables:

INITIAL_SLEEP   - wait n seconds before starting the screencast, setting this value > 0  enables you to prepare
                             everything before the capture starts (default: 5 seconds)

BASENAME         - the directory in which the screencast files will be created (default: /tmp/screencast-XXXX, where
                            XXXX is a random number and will get generated everytime you run the script)

MAX                    - the maximum number of screenshots to take (setting this to 0 disables this limit) and you need to
                             stop the script from running using "CTRL-C" in the terminal window you started it

INTERVAL           - Specify how many seconds to wait between taking two screenshots. This value can be set to
                            anything reasonable, even floating point numbers are allowed, e.g. 0.2 (0.2 seconds) or 0 to
                            wait not at all.


#!/bin/bash
 
# wait 5 seconds before starting to capture the screen
# that way, you can prepare yourself for getting captured
INITIAL_SLEEP=5
 
# Directory to store the screencast files in
BASENAME=$(mktemp /tmp/screencast-XXXX)
 
# Maximum number of screenshots to take
# set this to 0 to disable a maximum (the script will then need to be shut
# down using CTRL-C in the terminal window)
MAX=0
 
# Interval between two screenshots (in seconds, can be floating numbers
# too, e.g. 0.2 for 0.2 seconds, etc)
INTERVAL=1
 
if [ "$BASENAME" == "" ]; then
  echo "No name specified."
  exit 1
fi
 
I=$INITIAL_SLEEP
while [ $I -gt 0 ]; do
  echo -n $I...
  sleep 1
done
echo "Starting now"
 
I=0
while [ 1 ]; do
  FILENAME=$BASENAME/$I.png
 
  echo -n "Saving ${FILENAME}"
  if import -window root -silent ${FILENAME}; then
    echo "done."
  else
    echo "failed."
  fi
 
  I=$(( $I + 1 ))
 
  if [ $MAX -gt 0 -a $MAX -eq $I ]; then
    break
  fi
  sleep $INTERVAL
done

Open in new window

Top Expert 2008

Commented:
byzanz generates an animated gif file, that means you will always get only one file as the output. It's like a video file where all the frames are stored in just one file' that's why I suggested it.

Try to open the generated file with your browser or any other software that can show animated gifs and you should see the screencast running.

Author

Commented:
agriesser, thanks for the above script and the update on byzanz, I will try tomorrow as its late here now and I am going to bed. That makes sense about the animatd .gif, I must not have an animated gif player installed so I was just viewing it as a regular gif, anyways that seems like a great solution for my needs.
Top Expert 2008
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION
If needed, you can split the animated GIF into its frames anyway.

(with ImageMagick: "convert +adjoin -coalesce anim.gif frame%02d.gif" )

Author

Commented:
Thanks, it works great

Author

Commented:
(with ImageMagick: "convert +adjoin -coalesce anim.gif frame%02d.gif" ) -- when I replace "anim.gif" with my filename it does not work, is there another way to split the files up into individual screenshots. The problem is that when I view it in a web browser now there is no way to fast forward or jump to certain parts so if its a long recording you have to watch the entire thing through, is there another animated.gif player?

Author

Commented:
Another issue with byzanz, it doesn't seem to be able to record any more than 10 seconds, even if I put in duration=100 or duration=1000 it just records for ten seconds then turns off, any ideas on this?
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.