-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnetview
executable file
·152 lines (122 loc) · 3.19 KB
/
netview
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
#! /usr/bin/env bash
trim() {
# trim $1: cut of leading and trailing white space
# $1: the string to trim
echo "$1" | grep -oE "[^ \t\n].*[^ \t\n]"
}
get_config() {
# get_config $1 $2: load a configuration from a configuration file
# $1: the name of the configuration file
# $2: name of the configuration
if [ -e "$1" ]; then
trim "$(grep -E "^$2" < $1 | cut -d "=" -f 2)"
fi
}
clear_line() {
echo -en "\e[2K\e[1G2"
}
replace_line() {
echo -en "\e[2K\e[1G"
echo -n "$1"
}
connect() {
nc $CONFIG_TARGET_IP 9999
}
action_raw() {
cat
}
action_view() {
if [ "$1" == "grey" ]; then
mplayer - -demuxer rawvideo -rawvideo w=752:h=480:format=y8:fps=100
elif [ "$1" == "color" ]; then
mplayer - -demuxer rawvideo -rawvideo w=752:h=480:format=rgb24:fps=100
fi
}
action_imgseq() {
local FOLDER="$OPT_RECORDINGS_FOLDER/$(date "+%Y-%m-%d-%H_%M_%S")"
local i=0;
mkdir -p "$FOLDER"
while true; do
i=$[ $i + 1 ]
FILE_NAME="$FOLDER/image-$i.tif"
if [ "$1" == "grey" ]; then
./segment $[ 752 * 480 ] | convert -size 752x480 -depth 8 "GRAY:-" "TIFF:$FILE_NAME" || exit 1
elif [ "$1" == "color" ]; then
./segment $[ 3 * 752 * 480 ] | convert -size 752x480 -depth 8 "RGB:-" "TIFF:$FILE_NAME" || exit 1
fi
if [ "$OPT_IMGSEQ_FILE_NAMES" ]; then
echo "$FILE_NAME"
else
replace_line "Images written: $i" >&2
fi
sleep .5
done
}
action_record() {
local FILE="$OPT_RECORDINGS_FOLDER/$(date "+%Y-%m-%d-%H_%M_%S").mp4"
mkdir -p "$OPT_RECORDINGS_FOLDER"
if [ "$1" == "grey" ]; then
ffmpeg -f rawvideo -pix_fmt gray -s 752x480 -r 20 -i - -qscale "$OPT_FFMPEG_QSCALE" "$FILE"
elif [ "$1" == "color" ]; then
ffmpeg -f rawvideo -pix_fmt rgb24 -s 752x480 -r 20 -i - -qscale "$OPT_FFMPEG_QSCALE" "$FILE"
fi
}
action_relay() {
local TEMP_DIR=$(gmktemp -d)
local PIPE_1="$TEMP_DIR/pipe-1"
local CONTENT_LENGTH
{
echo "HTTP/1.1 200 OK"
echo "Content-Type: multipart/x-mixed-replace; boundary=boundary"
echo
echo "--boundary"
while true; do
CONTENT_LENGTH=$(./segment $[ 3 * 752 * 480 ] | convert -size "752x480" -depth 8 "RGB:-" "JPEG:" | tee "$PIPE_1" | wc -c)
echo "Content-Type: image/jpeg"
echo "Content-Length: $CONTENT_LENGTH"
echo
cat "$PIPE_1"
echo
echo "--boundary"
done
} | nc -l 8080
rm -rf "$TEMP_DIR"
}
OPT_ACTION="view"
OPT_DEBAYER=
OPT_RECORDINGS_FOLDER="recordings"
OPT_FFMPEG_QSCALE="4"
OPT_IMGSEQ_FILE_NAMES=
if [ "$1" == "raw" ] || [ "$1" == "view" ] || [ "$1" == "imgseq" ] || [ "$1" == "record" ] || [ "$1" == "relay" ]; then
OPT_ACTION=$1
shift
fi
while [ "${1:0:1}" == "-" ]; do
if [ "$1" == "-d" ]; then
OPT_DEBAYER=1
elif [ "$OPT_ACTION" == "record" ] && [ "$1" == "-q" ]; then
if ! [ "$2" ]; then
echo "Error: Option needs an arguemnt: $1" >&2
exit 1
fi
OPT_FFMPEG_QSCALE=$2
shift
elif [ "$OPT_ACTION" == "imgseq" ] && [ "$1" == "--file-names" ]; then
OPT_IMGSEQ_FILE_NAMES=1
shift
else
echo "Error: Unknown option: $1" >&2
exit 1
fi
shift
done
if [ "$1" ]; then
echo "Error: Unknown argument: $1" >&2
exit 1
fi
CONFIG_TARGET_IP=$(get_config ".config" "CONFIG_TARGET_IP")
if [ "$OPT_DEBAYER" ]; then
connect | ./debayer | "action_$OPT_ACTION" color
else
connect | "action_$OPT_ACTION" grey
fi