summaryrefslogtreecommitdiffstats
path: root/skripti/videonadzor.sh
blob: 3e6fa511d26cc2f2dc64e8ffe0ce519409614893 (plain) (blame)
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
#!/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=$i
				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 -fps_mode 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 -f1 | 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 -fps_mode 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