Capturing time-lapse from mjpeg streamer or any jpg using aws lambda

Posted on Updated on

We have a raspberry pi running jpeg streamer through octoprint for checking the status of a remote 3d printer, it is also a pretty unusual print and unusually large so I thought we should capture the process for posterity.

I really did not want to spin up a server to capture the 96 hour print (1 image every 60 seconds/1 minute) so I thought why not write a was lambda function (server less!), it will be much cheaper and easier – no command line crons and server to set up and ssh into etc.

Here is how I done it:

Firstly rather than the stream action, we need to use the snapshot function (action=snapshot).

Next we create an S3 bucket to save our images I called mine “timelapse-camera-1”.

Screen Shot 2018-05-24 at 22.22.21

Following that we move onto was lambda and set up our role and function. I went into the membership menu, roles and manually attached this policy.

Screen Shot 2018-05-24 at 22.22.58

I then wrote the following code in lambda, saved and tested.

BUCKET_NAME = "timelapse-camera-1"
from __future__ import print_function

import boto3
import time
import urllib

BUCKET_NAME = "timelapse-camera-1"
TMP_FILE = "/tmp/tmp.jpg"

def lambda_handler(event, context):
camera_file = urllib.URLopener()
try:
camera_file.retrieve("http://000.000.000/webcam/?action=snapshot", TMP_FILE)

timestr = time.strftime("%Y%m%d-%H%M%S")

s3 = boto3.resource('s3')
s3.meta.client.upload_file(TMP_FILE, BUCKET_NAME, timestr + '.jpg')

return 'saved ' + timestr + '.jpg'

except Exception as e:
print(e)
raise e

Next we need to run our function every minute by calling lambda from was cloud watch.

There we go, easy 🙂 just remember to turn it off when your done.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s