X
    Categories: Projects

Raspberry Pi Timelapse

Making time-lapse video with raspberry pi and using on-board graphic card to encode the videos! This is the Raspberry Pi Timelapse, using raspberry pi camera module and timelapse script

Requirements

Raspberry Pi (any version) amazon | ebay

Raspberry Pi Camera Module amazon | ebay

Process

adding the software for encoding

sudo apt-get install debian-keyring 
sudo sh -c "echo 'deb http://vontaene.de/raspbian-updates/ . main' >> /etc/apt/sources.list" gpg --keyserver pgp.mit.edu --recv-keys 0C667A3E gpg --armor --export 0C667A3E | apt-key add - sudo apt-get update

apt-get software:

sudo apt-get install libgstreamer1.0-0 liborc-0.4-0 gir1.2-gst-plugins-base-1.0 gir1.2-gstreamer-1.0 gstreamer1.0-alsa gstreamer1.0-omx gstreamer1.0-plugins-bad gstreamer1.0-plugins-base gstreamer1.0-plugins-base-apps gstreamer1.0-plugins-good gstreamer1.0-plugins-ugly gstreamer1.0-pulseaudio gstreamer1.0-tools gstreamer1.0-x libgstreamer-plugins-bad1.0-0 libgstreamer-plugins-base1.0-0

create folder to store images

mkdir /home/pi/timelapse

Download the time-lapse script from github repo (here)

sudo wget -O /usr/bin/timelapse.sh https://raw.githubusercontent.com/novaspirit/pi_timelapse/master/timelapse.sh
sudo chmod +x /usr/bin/timelapse.sh

edit crontab to execute raspistill every min

sudo crontab -e

at this line to the cron tab to automate the process

* * * * * sh /usr/bin/timelapse.sh 2>&1

command to sort photos to number query

ls -1v | awk 'BEGIN{ a=0 }{ printf "mv \"%s\" %04d.jpeg\n", $0, a++ }' | bash

command to convert photos to video

gst-launch-1.0 multifilesrc location=%04d.jpeg index=1 caps="image/jpeg,framerate=24/1" ! jpegdec ! omxh264enc ! avimux ! filesink location=timelapse.avi
novaspirit: Avid Thinker, Computer Programmer, Web evangelist, Hacking / Breaking expert, Problem Solver, Technology Obsessed, Gamer, 3D printing, Coffee lover!

View Comments (11)

  • thank u don for the great work
    i do have a question about crontab
    i'm working on an audio project and i wanna use crontab to a execute a shell on the top of each hour
    for example at 01:00 and 02:00
    can u please guide me on how to do that on
    thanks again in advance

      • I keep having a problem where it will save over the last picture, i am not sure what i am doing wrong here. I have re-installed the OS started from scratch multiple times, watched your video several times and i think you cut out a very important step. I have exhausted my google foo efforts at this point as i do not know the proper terminology for what this problem is. Please Help
        Thanx,
        ~Zaid

        • Ok I got help from a friend who noticed that the default "Date" script should have read "%d%m%Y_%H%M-%S" not "%d%m%Y_%H", apparently it was missing the hour and minutes.

  • Posted this on the Youtube ch too. It takes one pic every minute, then overwrites. Any thoughts on what I did?

  • Excellent work, thank you for sharing. I can get it all to work apart from the last line that makes the video. It seems to hand at "Pipeline is PREROLLING" ... any ideas? This is what I am executing with the output.

    gst-launch-1.0 multifilesrc location=%04d.jpeg index=1 caps="image/jpeg,framerate=24/1" ! jpegdec ! omxh264enc ! avimux ! filesink location=timelapse.avi
    Setting pipeline to PAUSED ...
    Pipeline is PREROLLING ...

    Any guidance as to what I need to do to resolve this final step would be really appreciated.

    • Wait till it's finished. It can take a long while sometimes if you have a few thousand or more jpegs to render eg 15 minutes for 2000 1080p pics for a 24fps video render.

  • Dude. This tutorial really isn't beginner friendly nor is the code above listed in a neat enough way to properly tell what's supposed to be separated as a lot of it is jumbled together and parts of the video have jump cuts that just gloss over things without explanation. This whole presentation needs to not be so chaotic to properly follow and execute by anyone who wants to use this. I followed these instructions several months ago and now want to set it up again and I'm struggling now as I did then. The first time it took me all day to get done mainly due to all the mistakes in there some you said you'll correct on the site some of which you did and some you didn't plus more that weren't mentioned in the video as well as some that seem to be newer ones. Thanks to a few commenters I fixed some of the issues on the first attempts earlier in the year but still took forever. Judging by the lack of your replies here and on the youtube video and for quite a while it looks like, I'm guessing it would be some luck if you did end up fixing this stuff on this page or redoing the video in a more straightforward way. Been attempting to do this for the last hour and I'm still stuck on the first few lines of code and getting nowhere...

  • Ok, for everyone having trouble with this I think this will be a more definitive explanation for this 'guide' since genius doesn't want to correct things properly. (If you haven't done so already, enable camera in Raspberry pi configuration from the raspbian's start menu. Also, enable SSH or VNC for remote wireless access to the pi from another device on your local network).

    1. Ignore the first part of the guide and skip to making a folder named timelapse in the home directory which can be done in the terminal by typing: mkdir /home/pi/timelapse (and hit enter).
    2. In the terminal type: cd Downloads/ (to get to your downloads folder in the home directory as the place the main script will be downloaded to), so you now see something like: pi@raspberry:~/Downloads $ (you are now in the 'Downloads' folder in your pi's 'home' directory).
    3. type: git clone http://github.com/novaspirit/pi_timelapse (hit enter to download). When it's done type ls and hit enter to see the folder's contents listed, which should be a single folder named; pi_timelapse
    4. Now enter it by typing: cd pi_timelapse/ and hit enter and you should see something like: pi@raspberry:~/Downloads/pi_timelapse $ (you are now in the pi_timelapse folder in pi/home/Downloads directory). Type ls and hit enter to see the contents of this folder which should be: README.md timelapse.sh (the ReadMe file if opened in file manager will have the same instructions as in the article above including all the incorrect ones that'll get you nowhere trying to set this stuff up. timelapse.sh is the file with all the main timelapse function code the pi will be using).
    5. Now make timelapse.sh executable by typing: sudo chmod +x timelapse.sh (hit enter).
    6. Type: nano timelapse (hit enter) to configure the jpeg settings. This is where you can change the resolution with the default being 1280x720 which can be changed to a much better 1920x1080 or whatever by deleting and retyping these numbers if you wanted.
    7. This part is very important. As Zaid pointed out above, the time/date readout (as seen in yellow onscreen above the resolution) is incorrect. Copy and paste this in its place instead: “%d%m%Y_%H%M-%S” (including the quotation marks. It may no longer be in yellow but is correct. If left unchanged jpegs will be taken in the pi/home/timelapse folder as they're supposed to be but there will be only one file and will be constantly overriding its self with a new jpeg instead of multiple separate jpegs taken over time which is used to render the final timelapse videos you want to make overwise that won't be possible. This line corrects that mistake). Now hit Ctrl+x to save the changes and type y when it asks you at the bottom of the screen followed by hitting enter to finalize the save and exit this screen.
    8. Move the timelapse.sh file out of the Downloads folder to the pi/usr/bin directory by typing: sudo mv timelapse.sh /usr/bin (and hit enter. If ever you want to change the jpeg settings/resolution again in future just go to cd pi/usr/bin directory in the terminal then type nano timelapse.sh just like here again but no longer from the Downloads folder, or you can use the file manager window and navigate to the pi/usr/bin folders and look for the timelapse.sh file and open it and change the values from in there like a notepad file but remember to save the changes for them to take effect before you close the file).
    9. Nearly done. Now, this is where the magic happens. We'll be using the pi's built-in Unix based scheduling app called Cron to shoot the jpegs at the scheduled intervals we'll set and from every time the pi boots up and is on. In any directory in the terminal type; crontab -e (and hit enter to bring up the Cron Table). Use the arrow keys on the keyboard to scroll to under the last line of text which like the rest of'em starts with a #. Type:* * * * * sh /usr/bin/timelapse.sh 2>&1 (This is the scheduling code for the file and directory we want to execute. Each of the five * * * * * (with spaces in between) represents different times in the order of something like - minutes, hour, the day of the month, month, the day of the week. The default setting is to exicute (take a jpeg) every minute but this can be changed for whatever schedule you want when you know how to use Cron which you can find tips and guides online but to simply change the minute schedule simply type after the first * a / and a number between 0-59 to represent how often in minutes you want jpegs to be shot. eg for every two minutes it should look like:*/2 * * * * sh /usr/bin/timelapse.sh 2>&1 (and so on)). Now hold Ctlr+x and hit y when asked then enter to save the changes and exit. If it worked jpegs should now be showing up in the timelapse folder in the pi/home directory. To change the cron schedule at any time simply open crontab -e in the terminal and change that number to whatever you want then save and exit. There's also a GUI for Cron on pi called Gnome and can be downloaded and installed in the pi's terminal with: sudo apt-get install gnome-schedule
    10. When you have enough jpegs to make a 24fps rendered timelapse video go into terminal and get to the timelapse folder/directory by typing: cd timelapse (hit enter) and you should see it say something like pi@raspberrypi:~/timelapse$. Then copy and paste this code next to it: ls -1v | awk 'BEGIN{ a=0 }{ printf "mv \"%s\" %04d.jpeg\n", $0, a++ }' | bash (hit enter). This will then rename the jpegs in order of a numbered queue that the AVI video rendering software can read.
    11. Now time to render the video. When that is done and still in the same directory, now copy and pastes this: gst-launch-1.0 multifilesrc location=%04d.jpeg index=1 caps="image/jpeg,framerate=24/1" ! jpegdec ! omxh264enc ! avimux ! filesink location=timelapse.avi (hit enter). Depending on how many and the data size of the jpegs it can take a while to render so go do something else for a while and come back later. Expect it to take something like 10 minutes per GB of jpegs to render on a RpiZero with 1080res jpegs being about 1MB each for instance. When done rename the timelapse AVI file to whatever you want and move to the video folder in the home directory then delete the original jpegs to create more space and prevent the pi from crashing as that can eventually happen as well sometimes after number queuing the jpegs and with too many files in the folder (like thousands) or for some reason or another in my experience so far but is still functional taking jpegs for days on end without a problem.
    ...And that's pretty much it.

    Just realize depending on the Pi came used as well as the fact that things are done in jpegs and AVI's etc. do not expect full HD video quality in the rendered videos or of the jpegs shot especially in low lighting conditions. The video's from the 1080res jpegs look far superior to the ones in 720res which really do look like a potato at 144p.

    Some tips: If using remote desktop apps for viewing and controlling of the pi from another device like a PC or a phone, the SSH protocol has most but not all functions you'd want, except for manly file transfer ability which you'll either need a separate FTP app on the other device or enable and use the VNC protocol which has everything that SSH can do but with fuller functionality including I think file transfers as well as basicly remote controlling the pi on a local network from another device.
    If doing this for a Pi zero use ssh or vnc from another device as it's quicker to copy and paste things over from something with a better ability to use web browsers than straight on the pi (in my limited experience with this so far). You may also need to download xrdc on the pi for SSH to work with other devices remotely.

Related Post