summaryrefslogblamecommitdiffstats
path: root/skripti/videonadzor.sh
blob: e097c58fdf5875947c8c665b4e71512e15aa3755 (plain) (tree)























                                                                                                  
                                                                        












                                                                                                                                                                                                                
                                                                                         






                                                                                                                    

                                                                                      
















                                                                                                                                                                                                                                
                                                                                                    

































                                                                                                                            
#!/bin/bash
# SKRIPT za snemanje kamere, katere firmware je example arduino sketch za ESP32CAM CameraWebServer
# Neprestano zahteva JPEGe na /capture endpointu
# Vsako minuto iz JPEGov naredi MKV in nato JPEGe izbriše
# Vsako uro naredi iz MKVjev MP4 in nato MKVje izbriše
set -uo pipefail
q=4
since=0
rm -f concat.txt
curl --no-progress-meter "$1/control?var=framesize&val=13"
while :
do
	curl --no-progress-meter "$1/control?var=quality&val=$q"
	t=`date --utc --iso-8601=ns | cut -d+ -f1`
	oldstart=`ls --sort=time | grep jpeg$ | tail -n1 | cut -d: -f1,2`
	if [ ! $oldstart = `cut -d: -f1,2 <<<"$t"` ] && [ ! -f concat.txt ]
	then
		echo "ffconcat version 1.0" > concat.txt
		prev=devica
		first=ERROR
		for i in $oldstart:*.jpeg konec
		do
			if [ $prev = devica ]
			then
				first=`rev <<<"$i" | cut -d. -f2- | rev`
				prev=$i
				continue
			fi
			echo "file 'file:$prev'" >> concat.txt
			if [ ! $i = konec ]
			then
		       		echo "duration 0`dc <<<"10k$(date --utc --date $(rev <<<"$i" | cut -d. -f2- | rev) +%s.%N) $(date --utc --date $(rev <<<"$prev" | cut -d. -f2- | rev) +%s.%N)"-p`" >> concat.txt
			else
				echo "duration 1" >> concat.txt
			fi
			prev=$i
		done
		{
			ffmpeg -f concat -safe 0 -i concat.txt -vsync vfr file:$first.mkv
			if [ -s $first.mkv ]
			then
				while read line
				do
					grep ^file <<<$line > /dev/null && rm `cut -d: -f2- <<<$line | cut -d\' -f1`
				done < concat.txt
			fi
			oldstart=`ls --sort=time | grep mkv$ | tail -n1 | cut -d: -f1`
			if [ ! $oldstart = `cut -d: -f1 <<<"$t"` ]
			then
				echo "ffconcat version 1.0" > concat.txt
				prev=devica
				for i in $oldstart:*.mkv konec
				do
					if [ $prev = devica ]
					then
						prev=$i
						continue
					fi
					echo "file 'file:$prev'" >> concat.txt
					if [ ! $i = konec ]
					then
		       				echo "duration 0`dc <<<"10k$(date --utc --date $(rev <<<"$i" | cut -d. -f2- | rev) +%s.%N) $(date --utc --date $(rev <<<"$prev" | cut -d. -f2- | rev) +%s.%N)"-p`" >> concat.txt
					fi
					prev=$i
				done
				ffmpeg -f concat -safe 0 -i concat.txt -vsync vfr file:$oldstart.mp4
				if [ -s $oldstart.mp4 ]
				then
					while read line
					do
						grep ^file <<<$line > /dev/null && rm `cut -d: -f2- <<<$line | cut -d\' -f1`
					done < concat.txt
				fi
			fi
			rm concat.txt
		} &
	fi
	curl --no-progress-meter --fail -o$t.jpeg $1/capture
	curlexit=$?
	if [ $curlexit -eq 22 ]
	then
		q=$(($q+1))
		echo "Setting quality to $q"
		continue
	fi
	if [ ! $curlexit -eq 0 ]
	then
		echo ERROR!!! Curl returned with $curlexit
		continue
	fi
	if [ $since -ge 1024 ] && [ $q -gt 4 ]
	then
		q=$(($q-1))
	fi
	if [ ! `file $t.jpeg | grep -o [1-9][0-9]*x[1-9][0-9]*` = 1600x1200 ]
	then
		echo Popravljam ločljivost.
		curl --no-progress-meter "$1/control?var=framesize&val=13"
	fi
done