Take Timed Lapsed Screen Shots From Terminal 3

Terminal

Share
Take Timed Lapsed Screen Shots From Terminal

Hey

I quite like time lapse movies. They are a cool way of generating movies over a period of time. I wondered if it was possible if you could make such movies of your screen with Terminal. I know there are various applications out there, but they cost money and are not as fun as doing it yourself. I want to create a very simple script in Terminal that would take a picture of my screen every n seconds. That way I could import those images and create a time lapse movie of my progress. It turns out, after a bit of trial and error, a simple script could be made and the results are very good. Since it is a script you can modify the code to your suiting and adjust it as you see fit.

Before we start, if you want to learn more on Bash and Terminal programming I recommend Learning the Bash Shell and Mac OS X Unix Toolbox from Amazon. They are both good resources if you want to learn more.

There are two versions of this script, one as a history state and one that can be easily used to make a time lapsed video. The first one uses the date as the file name for the video, the second a number. This arises because Quicktime (to compile the video) doesn’t seem to like dates, it does however work very well with numbers. I’ll show you both bits of code. Its very easy to modify the code as you see fit and I recommend you have a play around with what I have written. To run this app open Terminal in Applications > Utilities. I recommend you have a small bit of knowledge before you run this tutorial. You can find Terminal tips and tricks on this site and the Internet in general

The first script is:

while [ 1 ];do vardate=$(date +%d\-%m\-%Y\_%H.%M.%S); screencapture -t jpg -x ~/Desktop/screencapture/$vardate.jpg; sleep 5; done

That would produce a file with the name with the current date and time. The second seperate script will produce a file with a number as the file name.

i=1;while [ 1 ];do screencapture -t jpg -x ~/Desktop/screencapture/$i.jpg; let i++;sleep 1; done

If you want to reproduce this, run one of the two scripts. To quit the script in Terminal press Control + C.

The code is rather simple in both of the examples. It is essentially a loop which takes a picture and places it in a folder, it then pauses for a couple of seconds and repeats until you have finished. The code is as follows:

while [ 1 ];

This starts the loop, its begins the loop.

do vardate=$(date +%d\-%m\-%Y\_%H.%M.%S);

This sets a variable, vardate, to the current date. It is formatted as such “23-12-2009_20.51.53”

screencapture -t jpg -x ~/Desktop/screencapture/$vardate.jpg;

This runs a screencapture program in Terminal, it is the same one you use when you use the shortcuts. The modifiers (-t -x) set the code to run in silent mode and set the image format to jpeg. It will then place the image under ~/Desktop/screencapture/, if you want to place the images in a seperate folder change this file path. For more information on screencapture, in Terminal type “man screencapture” to find the help page.

sleep 5;

This pauses the loop for five seconds, change the integer to a different value to pause the script. For example sleep 1, will take a picture every 1 second. Sleep 10 will take a picture every 10 seconds and so on.

done

This finishes the loop and will allow the code to start from the beginning again.

The first script is useful for making a history log of images. However due to the file name structure it can be difficult to import into a video making app such as Quicktime. As a result the second script changes the variable to a number. This employs a piece of code to create a counter. As a result the code is as follows.

i=1;

This sets a variable “i” to a number, in this case one. The code is the same until you get to this point.

let i++;

This increase the variable, i, by one. As a result you have a counter and a different number for every file name. I find the second script more useful for time lapse images.

If you want to run this script yourself, all you have to do is change the folder location and number of seconds you want between images.

Once as the script has run for a couple of minutes you should have a folder full of images. It is now time to create a time lapse video.

Time Lapse Video

The following is a brief overview of how to put the image together as a time lapse video. I have ran the second script to generate a list of images named, 1.jpg 2.jpg etc. I am going to use Quicktime 7, although you can use any video editing app you want. The first step is to open Quicktime 7 located in Applications > Utilities on Snow Leopard.

Go to File > Open Image Sequence and select the first image from the list you have just created with the script. In the pop up select the number of frames per second you want the video you want to run at. 24-30 fps is good for normal video, however this can be a bit fast, I suggest a slower value of 15. The choice is yours, you can always play around with the settings.

quicktime frames per second

Press OK, and the video will load. Since the images are the full resolution of your screen (you can probably change this in the screencapture settings) you will need to shrink the video. This is done under View > Half Size. You are then ready to play your video.

quicktime video

If you want to save the movie as a file, go to File > Export and have a play with the settings.

Conclusion

There is a lot you can do with this script. There are more settings you can play with in the script and you can have a mess around with the video editing, Quicktime is good for making a video, however there are better video editing apps. Have a play around with the code. If you are stuck leave a comment or search the web. There are lots of bash and Terminal tutorials to make scripts. I hope you enjoyed this little tutorial. It was fun to make and the results are really good for a little bit of Terminal code.


If you want to keep up with the latests post from Mac Tricks And Tips I recommend you subscribe to the RSS Feed.

Where To Next?

  • Subscribe To Mac Tricks And Tips