Wednesday, July 9, 2014

AirCam to YouTube Time Lapse with Raspberry Pi

I setup a weather station with webcam to feed data to Wunderground. The station setup was an AirBridge with an AirCam. The only issue, was the uploading of a pic to Wunderground was not as reliable as I had hoped, and I couldn't save the time lapse videos offline for use later. So I added a Raspberry Pi to the network to download high quality images from the webcam and create the time lapse videos for uploading to YouTube.

The end result is an automated time lapse (from 4AM to 10 PM - 1 Image Per Minute - 1080 Images - Processed at 24FPS) uploaded to youtube that is 45 seconds long.
What is needed:
A Functional AirCam on the network, with a known IP and Allow Unauthenticated option turned on.
Raspberry Pi with a Raspian image on a least a 4GB card
A thumb drive inserted into the Raspberry Pi with a folder on it called "TimeLapse".
HDMI Cable from the Raspberry to your TV.
An Ethernat connection to the network.
Section 1 - Initial Setup:
Getting started, Update the Pi first:
sudo apt-get update
sudo apt-get upgrade

Install needed apps:
sudo apt-get install ffmpeg mencoder python-gdata python-pycurl python-progressbar

Use "ifconfig" to figure out the IP Address, then use Putty to SSH to the Raspberry Pi.

Section 2 - Building the Scripts
You can download the scripts with these, or paste them in manually.
sudo wget https://www.dropbox.com/s/glrjw1r0c0jp9ip/TimeLapse.sh -P /etc/
sudo wget https://www.dropbox.com/s/65e0hopek114uqc/TimeLapseExport.sh -P /etc/
sudo wget https://www.dropbox.com/s/rbdwkwma5lzrbin/TimeLapseCleanup.sh -P /etc/

*Skip to Section 3 if you downloaded them directly, after you edit TimeLapseExport.sh and change the username/password fields. Don't forget to update the YouTube Keywords and Description.

Run the following to create the script file,
sudo nano /etc/TimeLapse.sh

then paste the script below:
--------------------------------------------------------
#!/bin/sh
#/etc/TimeLapse.sh
export DATETIME=`date +%Y%m%d%H%M`
export DATE=`date +%Y%m%d`
export MONT=`date +%Y%m`
FILE=/mnt/USB/TimeLapse
CAMIP=192.168.1.20
if [ -d $FILE ]
then
echo "Thumb Drive Available @ $FILE"
mkdir /mnt/USB/TimeLapse/$MONT
mkdir /mnt/USB/TimeLapse/$MONT/$DATE
ffmpeg -i rtsp://$CAMIP/live/ch00_0 -y -f image2 -sameq -t 0.001 /mnt/USB/TimeLapse/$MONT/$DATE/$DATETIME.jpeg
else
echo "Thumb Drive not Mounted @ $FILE"
sudo mount -o uid=pi,gid=pi /dev/sda1 /mnt/USB
sudo mount -o uid=pi,gid=pi /dev/sdb1 /mnt/USB
fi
 
--------------------------------------------------------

Use CTRL+S to save, then CTRL+X to close.

Create another script file and paste the script text below (Edit the username, password and youtube data):
sudo nano /etc/TimeLapseExport.sh

--------------------------------------------------------
#!/bin/sh
#/etc/TimeLapseExport.sh
export DATETIME=`date +%Y%m%d%H%M`
export DATE=`date +%Y%m%d`
export MONT=`date +%Y%m`
export USER="someone@gmail.com"
export PASS="*********"

export YTTITLE="Weather Time Lapse KCASQUIR2-$DATE"
export YTDESC="Time Lapse from Squirrel Valley (Mountain Mesa) CA on $DATE"
export YTKEY="Weather, Time, Lapse, KCASQUIRL2, Lake Isabella, Sierra Nevada, SkyWarn, NWS, Hanford, Mountains, Clouds"
FILE=/mnt/USB/TimeLapse
TMP=/var/TimeLapse

UP=/etc/youtube-upload-0.7.3/bin
if [ -d $FILE ]
then
echo "Thumb Drive Available @ $FILE"
echo "Images found:"
ls -l $FILE/$MONT/$DATE | wc -l
echo "Generating List"
ls $FILE/$MONT/$DATE/*.jpeg > $FILE/$MONT/$DATE/stills.txt
echo "Begin processing Time Lapse"
mencoder -nosound -ovc lavc -lavcopts vcodec=mpeg4:aspect=16/9:vbitrate=8000000 -vf scale=1280:720 -o $TMP/TimeLapse-$DATE.avi -mf type=jpeg:fps=24 mf://@$FILE/$MONT/$DATE/stills.txt
echo "Cleanup image list"
rm $FILE/$MONT/$DATE/stills.txt
echo "Start Upload to YouTube"

$UP/youtube-upload --email=$USER --password=$PASS --title=$YTTITLE --description=$YTDESC --category=Entertainment --keywords=$YTKEY $TMP/TimeLapse-$DATE.avi
echo "Cleanup video file"
rm $TMP/TimeLapse-$DATE.avi
else
echo "Thumb Drive not Mounted @ $FILE"
sudo mount -o uid=pi,gid=pi /dev/sda1 /mnt/USB
sudo mount -o uid=pi,gid=pi /dev/sdb1 /mnt/USB
fi

--------------------------------------------------------

One more script to cleanup images older than 30 days:
sudo nano /etc/TimeLapseCleanup.sh
--------------------------------------------------------
#!/bin/bash

#/etc/TimeLapseCleanup.sh
find /mnt/USB -type f -mtime +30 | xargs rm

--------------------------------------------------------
 
Make your scripts executable by running the following:
sudo chmod u+x /etc/TimeLapse.sh
sudo chmod u+x /etc/TimeLapseExport.sh
sudo chmod u+x /etc/TimeLapseCleanup.sh

Section 3 - Setup Youtube-upload
Now we need to get the scripts to upload to youtube. Run the following commands:

cd /etc/
sudo wget http://youtube-upload.googlecode.com/files/youtube-upload-0.7.3.tgz
sudo tar xvzf youtube-upload-0.7.3.tgz
cd youtube-upload-0.7.3
 sudo python setup.py install

Section 4 - Automation
Setup Crontab to launch your scripts on a regular basis:
sudo crontab -e -u root

add the following:
-----------------------------------------------------------
* 4-21 * * * /etc/TimeLapse.sh
0 22 * * * /etc/TimeLapseExport.sh > /var/log/TimeLapseExport.log 2>&1
0 1 * * * /etc/TimeLapseCleanup.sh
0 0 * * * /sbin/shutdown -r now

-----------------------------------------------------------

Section 5 (Optional) - Setup SMB to access files from remote computer
This will allow you to access the files on the thumb drive from a PC:
sudo apt-get install samba samba-common-bin
sudo nano /etc/samba/smb.conf

Add the following:
-----------------------------------------------------------
[pihome]
   comment= Pi Home
   path=/mnt/USB
   browseable=Yes
   writeable=Yes
   only guest=no
   create mask=0777
   directory mask=0777
   public=no
-----------------------------------------------------------

CTRL+S to save, and CTRL+X to exit
Set a password for SMB Access:
sudo smbpasswd -a pi
*Enter password twice

Section 6 - RAM Drive for Video Output
We don't want to beat up the Thumb Drive, so do the following:
sudo mkdir /var/TimeLapse
sudo nano /etc/fstab
Add the following:
--------------------------------------------------------
tmpfs   /var/TimeLapse    tmpfs    defaults,noatime,nosuid,mode=0755,size=100m    0 0
--------------------------------------------------------

Save the file by pressing CTRL-X and select Y to save the changes.

Section 7 (Optional) - Setting a Static IP
Optional, Set a static IP Address for the Pi by running the following:
sudo nano /etc/network/interfaces

Remove the line that reads
iface eth0 inet dhcp

Add the following:
-----------------------------------------------------------
iface eth0 inet static
address 192.168.1.10
netmask 255.255.255.0
network 192.168.1.0
broadcast 192.168.0.255
gateway 192.168.1.1
-----------------------------------------------------------

Save the file by pressing CTRL-X and select Y to save the changes.

If you changed the IP to static, don't forget the DNS servers:
sudo nano /etc/resolv.conf

Replace the text with the following:
-----------------------------------------------------------
nameserver 8.8.8.8
nameserver 8.8.4.4
nameserver 4.2.2.1
-----------------------------------------------------------

Save the file by pressing CTRL-X and select Y to save the changes.

Section 8 (Optional) - Setup Web Interface to view images

Install Apache:
sudo apt-get install apache2

Edit the Apache Web Config:
sudo nano /etc/apache2/sites-enabled/000-default
Edit the Document:
Root and the Directory fields to the following:
DocumentRoot /mnt/USB
<Directory /mnt/USB/>

Save the file by pressing CTRL-X and select Y to save the changes.
Reboot the web service to accept the changes:
sudo service apache2 restart

Test the web portal from your computer/device by putting the IP of the Raspberry in the browser

Section 9 - Reboot
reboot with the following:
sudo reboot

Section 10 - Aircam Tuneup
The Aircam has a few "wonderful" settings that you may want to change to get more picture clarity. Look at these images that were taken around the same time of day. One looks out of focus, but it wasn't.
 
You can telnet into the camera and use the default "ubnt" as the username and password.

View the current settings:
cat /proc/isp0/ae/info 
cat /proc/isp0/awb/info

Execute the following commands to cleanup the image quality during the day:
echo 1 > /proc/isp0/ae/ev_mode
echo "w denoise 0" > /proc/isp0/command
echo "w crosstalk 30" > /proc/isp0/command
echo "w sharpness 100" > /proc/isp0/command

sed -i 's/max_quant.*/max_quant = 20/' /var/etc/ubnt-streamer.conf
sed -i 's/mjpeg_quality.*/mjpeg_quality = 100/' /var/etc/ubnt-streamer.conf
pkill ubnt-streamer

Ref: http://community.ubnt.com/t5/UniFi-Video/Tune-up-your-aircam/td-p/269226
Ref: https://community.ubnt.com/t5/UniFi-Video/snapshot-cgi-chan-0-how-to-reduce-compression-on-AirCam-FW-1-2/td-p/555637

No comments:

Post a Comment