summaryrefslogtreecommitdiff
path: root/capture.sh
blob: eff84a0c9ebf7b6917e168457bfc1cdcbe2474ee (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#!/bin/bash
#this script repeatedly captures images from a video device (e.g. /dev/video0) at regular intervals
#and it compresses images into an .tar.gz after some number and deletes those images
#and it deletes old archives after some number have been made
#you can provide a directory in which to save the images if desired 

MAX_SIZE=1073741824 #1G in bytes
TIME_INTERVAL=15 #seconds between taking snapshots
THRESHOLD_ARCHIVE=5760 #number of images after which archiving should be done. 5760 is one day if pics are every 15s
THRESHOLD_REMOVE=30 #number of image archive tarballs after which a handful of the oldest archives will be deleted

#set the working directory 
if [[ ${1} && -d ${1} ]]; then 
	cd ${1}
fi

while [[ true ]]; do
	d=`date +%Y%m%d_%H%M%S`
	snapshot ${d}.png
	ln -snf ${d}.png current.png
	sleep ${TIME_INTERVAL}
	if [[ `find . -maxdepth 1 -name "*png" | wc -l` -gt ${THRESHOLD_ARCHIVE} ]]; then
	#if [[ `ls *png | wc -l` -gt ${THRESHOLD_ARCHIVE} ]]; then
		echo "time to archive captured images"
		mkdir archive${d}
		mv *png archive${d}/
		tar -czvf archive${d}.tar.gz archive${d}/*
		echo "archive made. images compressed. now deleting source files."
		rm -rf archive${d}
	fi
	if [[ `ls archive* 2&>/dev/null | wc -l` -gt ${THRESHOLD_REMOVE} ]]; then
		echo "time to remove some old archived image sets"
		rm `ls -t . | grep archive | tail -n 5` -rf
	fi
done


#if [ `du . -b | awk '{print $1}'` -gt ${MAX_SIZE} ]; then