-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmakevideo
executable file
·92 lines (80 loc) · 2.4 KB
/
makevideo
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
#!/bin/bash
#######################################################################
# Made by : Ewoud Dronkert
# Licence : GNU GPL v3
# Platform : Raspberry Pi
# Requires : bash, ssh access, avconv
# Location : /usr/local/bin/
# Name : makevideo
# Version : 1.0.0
# Date : 2016-08-01
# Purpose : Create video from remote dir full of webcam jpegs.
# Parameters : none
# Settings : SERVER = remote server holding the webcam jpegs,
# capable of ssh / scp / rsync
# RDIR = remote directory containing the jpegs,
# also where the video will be uploaded
# Exit 0 : No errors (but rsync/ssh/touch/etc errors not monitored)
# 1 : No jpegs found
#######################################################################
# Server
SERVER="user@server"
IDFILE="/home/pi/.ssh/id_rsa"
# Remote and local directories
RDIR="remote-dir"
LDIR="/tmp"
VIDEODIR="/home/pi/Videos"
# Remote and local file name patterns, format
RPAT="[012][0-9][0-5][0-9].jpg"
LPAT="img-[0-9][0-9][0-9][0-9].jpg"
LFMT="img-%04d.jpg"
# Download webcam shots
rsync -qau -e "ssh -i '$IDFILE'" \
"$SERVER":"$RDIR/$RPAT" \
"$LDIR/"
# Avoid using old files for making the video
# (not all remote 'HHMM.jpg' files may heve been updated in case of upload problems)
# Assume webcam runs between 8:00 and 22:00
NOW=$(date +%-H%M)
if (( NOW >= 800 )); then
# Webcam running or today completed, use files from today
REF=$(date +%Y%m%d)
else
# Use files from yesterday
REF=$(date -d yesterday +%Y%m%d)
fi
touch -t "${REF}0000.00" "$LDIR/midnight"
# Generate consecutive numbering for all relevant files
# Save rename data in list to restore later
RENAME="$LDIR/mv.txt"
: > "$RENAME"
find "$LDIR/"$RPAT -newer "$LDIR/midnight" \
| sort | cat -n | while read i f; do
f=$(basename "$f")
printf "%s $LFMT\n" "$f" "$i" >> "$RENAME"
done
# Move files temporarily for making video
rm -f "$LDIR/"$LPAT
cat "$RENAME" | while read a b; do
mv -f "$LDIR/$a" "$LDIR/$b"
done
# Number of jpegs
n=$(ls "$LDIR/"$LPAT | wc -l)
if (( n == 0 )); then
exit 1
fi
# Make video
VID="$VIDEODIR/webcam-$REF.mp4"
avconv -y \
-r 4 -i "$LDIR/$LFMT" \
-an -r 4 -c:v libx264 "$VID" \
&>/dev/null
chown pi:pi "$VID"
# Move back jpegs
cat "$RENAME" | while read b a; do
mv -f "$LDIR/$a" "$LDIR/$b"
done
chown pi:pi "$LDIR/"$RPAT
# Upload video
[ -r "$VID" ] && scp -p -q -i "$IDFILE" \
"$VID" "$SERVER":"$RDIR/$REF.mp4"