-
Notifications
You must be signed in to change notification settings - Fork 0
/
streamer
executable file
·250 lines (201 loc) · 5.51 KB
/
streamer
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
#!/bin/bash
test ! -z "${1}" && FQDN="${1}"
test -z "${FQDN}" && FQDN="_"
DEBIAN_FRONTEND=noninteractive apt -yq -o Dpkg::Options::="--force-confnew" install nginx libnginx-mod-rtmp ffmpeg ssl-cert
if [ ! -d "/etc/letsencrypt/live/${FQDN}" ]
then
CERT="/etc/ssl/certs/ssl-cert-snakeoil.pem"
PRIVATE_KEY="/etc/ssl/private/ssl-cert-snakeoil.key"
else
CERT="/etc/letsencrypt/live/${FQDN}/fullchain.pem"
PRIVATE_KEY="/etc/letsencrypt/live/${FQDN}/privkey.pem"
fi
APP_PATH="/opt/stream"
SCRIPT_PATH="${APP_PATH}/scripts"
CONTENT_PATH="${APP_PATH}/contents"
grep -q "^rtmp {" /etc/nginx/nginx.conf || cat >> /etc/nginx/nginx.conf << EOF
rtmp {
server {
listen 1935;
hls on;
hls_path /var/www/stream/hls;
hls_keys on;
hls_fragments_per_key 2;
hls_nested on;
hls_fragment_naming system;
dash on;
dash_path /var/www/stream/dash;
dash_nested on;
chunk_size 4096;
allow publish 127.0.0.1;
deny publish all;
application live {
live on;
record off;
}
}
}
EOF
cat > /etc/nginx/sites-available/stream << EOF
server {
listen 443 ssl;
listen [::]:443 ssl;
server_name ${FQDN};
ssl_protocols TLSv1.2 TLSv1.3;
ssl_ciphers ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-CHACHA20-POLY1305:ECDHE-RSA-CHACHA20-POLY1305:DHE-RSA-AES128-GCM-SHA256:DHE-RSA-AES256-GCM-SHA384;
ssl_prefer_server_ciphers off;
ssl_session_timeout 1d;
ssl_session_cache shared:SSL:10m; # about 40000 sessions
ssl_session_tickets off;
ssl_certificate ${CERT};
ssl_certificate_key ${PRIVATE_KEY};
root /var/www/stream;
# Add index.php to the list if you are using PHP
index index.php index.html;
# rtmp stat
location /stat {
rtmp_stat all;
rtmp_stat_stylesheet stat.xsl;
}
location /stat.xsl {
alias /var/www/stream/stat.xsl;
}
# rtmp control
location /control {
rtmp_control all;
}
location /keys {
root /var/www/stream;
}
location /hls {
types {
application/vnd.apple.mpegurl m3u8;
}
root /var/www/stream;
add_header Cache-Control no-cache;
add_header Access-Control-Allow-Origin *;
}
location /dash {
root /var/www/stream;
add_header Cache-Control no-cache;
# To avoid issues with cross-domain HTTP requests (e.g. during development)
add_header Access-Control-Allow-Origin *;
}
location / {
try_files \$uri \$uri/ =404;
}
}
server {
listen 80;
listen [::]:80;
server_name ${FQDN};
location / {
return 301 https://\$host\$request_uri;
}
}
EOF
ln -s /etc/nginx/sites-{available,enabled}/stream
rm -f /etc/nginx/sites-enabled/default
install -dD -o www-data -g www-data /var/www/stream
systemctl restart nginx.service
install -dD ${APP_PATH}/{scripts,contents}
install -dD "/etc/systemd/system"
# Delayed Stream Script and Service
cat > "${SCRIPT_PATH}/delayed-stream" << EOF
#!/bin/bash
APP_PATH="${APP_PATH}"
CONTENT_PATH="\${APP_PATH}/contents"
HLS_PATH="/var/www/stream/hls"
DASH_PATH="/var/www/stream/dash"
STREAM_NAME="delayed-stream"
MINUTE="30"
test -d "\${HLS_PATH}/\${STREAM_NAME}" && rm -rf \${HLS_PATH}/\${STREAM_NAME}/*
test -d "\${DASH_PATH}/\${STREAM_NAME}" && rm -rf \${DASH_PATH}/\${STREAM_NAME}/*
while SVIDEO="\$(ls -tr \${CONTENT_PATH}/*.mp4 | tail -n\${MINUTE} | head -n1)"
do
ffmpeg -re -i "\${SVIDEO}" -f flv -c copy "rtmp://localhost/live/\${STREAM_NAME}"
done
EOF
chmod +x "${SCRIPT_PATH}/delayed-stream"
cat > "/etc/systemd/system/delayed-stream.service" << EOF
[Unit]
Description=Delayed Stream
After=network-online.target
Wants=network-online.target
[Service]
ExecStart=${SCRIPT_PATH}/delayed-stream
Restart=always
RestartSec=2sec
[Install]
WantedBy=multi-user.target
EOF
# Record Stream Script and Service
cat > "${SCRIPT_PATH}/record-stream" << EOF
#!/bin/bash
APP_PATH="${APP_PATH}"
CONTENT_PATH="\${APP_PATH}/contents"
while true
do
ffmpeg -t 60 -i "rtsp://USERNAME:PASSWORD@IP_ADDRESS:PORT/STREAM_CHANNEL_URI" -c copy "\${CONTENT_PATH}/capture-\$(date "+%H-%M-%S").mp4"
done
EOF
chmod +x "${SCRIPT_PATH}/record-stream"
cat > "/etc/systemd/system/record-stream.service" << EOF
[Unit]
Description=Record Stream
After=network-online.target
Wants=network-online.target
[Service]
ExecStart=${SCRIPT_PATH}/record-stream
Restart=always
RestartSec=2sec
[Install]
WantedBy=multi-user.target
EOF
# Record Cleaner Script and Service
cat > "${SCRIPT_PATH}/record-cleaner" << EOF
#!/bin/bash
APP_PATH="/opt/stream"
CONTENT_PATH="\${APP_PATH}/contents"
MINUTE="30"
DAY_MINUTE="1440"
while true
do
if [ \$(ls -t \${CONTENT_PATH}/*.mp4 | wc -l) -eq "\${DAY_MINUTE}" ]
then
rm \$(ls \${CONTENT_PATH}/*.mp4 | sort | head -n\$((DAY_MINUTE-(MINUTE*2))))
fi
sleep \$((MINUTE*2))
done
EOF
chmod +x "${SCRIPT_PATH}/record-cleaner"
cat > "/etc/systemd/system/record-cleaner.service" << EOF
[Unit]
Description=Record Cleaner
[Service]
ExecStart=${SCRIPT_PATH}/record-stream
Restart=always
RestartSec=2sec
[Install]
WantedBy=multi-user.target
EOF
systemctl daemon-reload
systemctl enable record-stream.service record-cleaner.service delayed-stream.service
# Web Player
cat > /var/www/stream/index.html << EOF
<!DOCTYPE html>
<html lang="tr">
<head>
<meta charset="UTF-8">
<title>Stream Server</title>
<script type="text/javascript" src="https://cdn.jsdelivr.net/npm/@clappr/player@latest/dist/clappr.min.js"></script>
</head>
<body>
<div id="player"></div>
<script>
var player = new Clappr.Player({source: "/hls/delayed-stream/index.m3u8", parentId: "#player", autoPlay: true, controls: false});
</script>
</body>
</html>
EOF
chown www-data:www-data /var/www/stream/index.html