Add viewer count script for cronjob and iframe in corner of viewcount, show 0 when offline with nginx try_files

master
Thomas Lynch 2 years ago
parent d78d04dcf7
commit 562aec2a05
No known key found for this signature in database
GPG Key ID: FBAB081F9B6E14B2
  1. 2
      .gitignore
  2. 14
      nginx/rtmp.conf
  3. 18
      nginx/stream.conf
  4. 1
      nginx/www/stream/viewers.txt
  5. 33
      viewers.sh

2
.gitignore vendored

@ -1,2 +1,4 @@
node_modules/
nginx/www/stream/viewers/*
nginx/www/stream/posters/*
streamkeys.js

@ -1,25 +1,33 @@
rtmp_auto_push on;
rtmp_auto_push_reconnect 10s;
rtmp {
max_connections 1000;
server {
listen 172.16.0.1:1935;
allow 172.16.0.0/16;
deny all;
chunk_size 4096;
application live {
sync 10ms;
# sync 100ms;
# play_restart on;
# interleave on;
# wait_key on;
# wait_video on;
drop_idle_publisher 10s;
#dash
dash on;
dash_path /tmp/dash;
dash_fragment 5;
dash_playlist_length 60;
dash_playlist_length 30;
#hls
hls on;
hls_fragment_naming system;
hls_path /tmp/hls;
hls_fragment 5;
hls_playlist_length 60;
hls_playlist_length 30;
#rtmp
on_publish http://127.0.0.1:7069/forms/stream;
live on;

@ -1,3 +1,4 @@
server {
server {
server_name stream.loki www.stream.loki;
@ -19,19 +20,26 @@ server {
root /tmp/;
}
location /stat.html {
location = /streams {
rtmp_stat all;
rtmp_stat_stylesheet /stat.xsl;
add_header Refresh "60; $request_uri";
}
location /viewers.html {
location /stat.xsl {
root /var/www/stream/;
add_header Refresh "60; $request_uri";
}
location /stat.xsl {
location /viewers/ {
default_type text/plain;
add_header Content-Type "text/plain";
add_header Refresh "30; $request_uri";
root /var/www/stream/;
try_files $uri /viewers.txt =404;
}
location /posters/ {
root /var/www/stream/;
try_files $uri /offline.png =404;
}
location / {

@ -0,0 +1,33 @@
#!/bin/bash
# create text file with current viewer count on stream, based on logs
# get current and 1 minute in past timestamps
timestamp_now=$(date +'%s')
timestamp_prev=$(date -d '1 min ago' +'%s')
lf_now=$(date +'%d/%b/%Y:%H:%M:' -d @$timestamp_now)
lf_prev=$(date +'%d/%b/%Y:%H:%M:' -d @$timestamp_prev)
# tail the logs and get a count of viewers per streamer
viewers=`tail -n 10000 /var/log/nginx/access.log \
| grep -E 'GET /(hls|dash)/[a-z0-9]+.m3u8' \
| grep -E "${lf_now}|${lf_prev}" \
| awk '{print $3 " " $7}' \
| sort \
| uniq \
| awk -F"/" '{print $3}' \
| cut -d'.' -f1 \
| sort \
| uniq -c \
| sed 's/[^ a-zA-Z0-9]//g' \
| sed 's/^\s*//' `
# for each num, streamer pair, output a view number file
while read -r line
do
num=`echo $line | cut -f1 -d" "`
name=`echo $line | cut -f2 -d" "`
echo "👁 $num" > "/var/www/stream/viewers/$name.txt"
done <<< "$viewers"
# delete any not updated viewer files (no longer live)
find /var/www/stream/viewers/* -mmin +0.5 -exec rm {} \;
Loading…
Cancel
Save