Tag: bash script

Capturing SAT24 satellite images to create an animation

During the solar eclipse on March 20th 2015 it was nothing but clouds over here in The Netherlands.
So besides following the eclipse on TV I also watched the satellite images on the SAT24 site to track the Moon’s shadow over the continent.
I got the idea to create a small bash script that downloads the satellite images every X minutes in a separate directory.
Those images can be stitched together to create a nice movie.

So, here’s the code:

INFRARED=false
TMPDIR=/home/pi/sat24/tmp
IMAGEDIR=/home/pi/sat24/images
CURRENTIMAGE=$(date +"%Y%m%d%H%M")

wget -O $TMPDIR/temp.jpg "http://www.sat24.com/image2.ashx?region=eu&ir=$INFRARED"

MD5NEW=`/usr/bin/md5sum $TMPDIR/temp.jpg | cut -d ' ' -f 1`
MD5OLD=`/usr/bin/md5sum $TMPDIR/prev.jpg | cut -d ' ' -f 1`

if [ "$MD5NEW" != "$MD5OLD" ]; then
{
cp $TMPDIR/temp.jpg $TMPDIR/prev.jpg
cp $TMPDIR/temp.jpg $IMAGEDIR/$CURRENTIMAGE.jpg
}
fi

It’s pretty straight forward.
The image file name is the current date and time and a new image is downloaded using wget.

The only thing that needs an explanation is the md5sum stuff.
Sometimes it happens that the script downloads a image from a few minutes ago and not the current one.
That means that most of the time it’s an image you already have.
I guess this has something to do on the Sat24 side: web servers not in sync or load balancers that don’t balance load.
With md5sum you create a MD5 hash which is ‘unique’.
If the new image has the same MD5 has as the previous image, this new image can be discarded because you’ve already downloaded it.

To make this script run automatically, simply add it to the crontab (I’m using the cron of user pi on my Raspberry Pi here):

<code>*/4 * * * * /home/pi/sat24/sat24.sh</code>

I use intervals of 4 minutes but you can tune that if you like.
Oh, and do not for get to turn on the execute bit of your script.

Okay, you’ve been running this script for a couple of days and you’ve collected hundreds of images.
Now it’s time to stitch them together and make an animation!

Thanks to a great command line tool called avconv (previously known as ffmpeg), you can create a movie file using separate images:

cat images/*.jpg | avconv -f image2pipe -c:v mjpeg -i - -r 25 -map 0 -s 1280x780 -filter:v "setpts=4.0*PTS" out.mp4

Err…yes! It’s that simple!
Just cat the files and pipe them into avconv.
The -s 1280×780 parameter creates a 720p HD video file.
The -filter:v “setpts=4.0*PTS” parameter slows the playback rate down because 0therwise the animation goes a bit too fast. You can fiddle with the number to your taste.
The video created is called out.mp4 which you can rename if you want, of course.

And this is the result:

Some double images might be downloaded once in a while, which you can delete by yourself by wading through the images directory.
Or you could write a script that checks and deletes duplicates for you.