From e57c8015a195f4e7e3ee7afcf3224242d8112e75 Mon Sep 17 00:00:00 2001 From: Thomas Lynch Date: Sun, 20 Nov 2022 22:37:49 +1100 Subject: [PATCH] add README, fix a bunch of paths, reorganise, make more suitable for public --- README.md | 13 +++++++++++ anime.sh | 17 -------------- .../package-lock.json | 0 package.json => auth-server/package.json | 0 server.js => auth-server/server.js | 2 +- .../streamkeys.js.example | 0 crontab | 3 +++ music.sh | 21 ------------------ nginx/rtmp.conf | 6 +++-- nginx/{ => sites-available}/stream.conf | 15 +++++-------- posters.sh | 4 ++-- .../stream => public}/css/video-js.min.css | 0 {nginx/www/stream => public}/dash.html | 0 {nginx/www/stream => public}/favicon.ico | Bin {nginx/www/stream => public}/hls.html | 0 {nginx/www/stream => public}/js/video.min.js | 0 {nginx/www/stream => public}/offline.png | Bin {nginx/www/stream => public}/stat.xsl | 0 {nginx/www/stream => public}/viewers.txt | 0 viewers.sh | 4 ++-- 20 files changed, 30 insertions(+), 55 deletions(-) create mode 100644 README.md delete mode 100644 anime.sh rename package-lock.json => auth-server/package-lock.json (100%) rename package.json => auth-server/package.json (100%) rename server.js => auth-server/server.js (91%) rename streamkeys.js.example => auth-server/streamkeys.js.example (100%) create mode 100644 crontab delete mode 100644 music.sh rename nginx/{ => sites-available}/stream.conf (75%) rename {nginx/www/stream => public}/css/video-js.min.css (100%) rename {nginx/www/stream => public}/dash.html (100%) rename {nginx/www/stream => public}/favicon.ico (100%) rename {nginx/www/stream => public}/hls.html (100%) rename {nginx/www/stream => public}/js/video.min.js (100%) rename {nginx/www/stream => public}/offline.png (100%) rename {nginx/www/stream => public}/stat.xsl (100%) rename {nginx/www/stream => public}/viewers.txt (100%) diff --git a/README.md b/README.md new file mode 100644 index 0000000..5f7b384 --- /dev/null +++ b/README.md @@ -0,0 +1,13 @@ +# stream + +The source of [stream.loki](http://stream.loki) (or another simple nginx-rtmp-based streaming site). + +The aim is to have a simple streaming site with chat (irc iframe), view count, an index page ([/streams](http://stream.loki/streams)) of all live streams, and do it with minimal backend. + +This repo provides: +- the nginx configs and static files +- small nodejs http server for nginx to authenticate stream keys (optional) +- some cronjobs to generate viewer counts and stream thumbnails + +## License +GNU AGPLv3, see [LICENSE](LICENSE). diff --git a/anime.sh b/anime.sh deleted file mode 100644 index 215c16a..0000000 --- a/anime.sh +++ /dev/null @@ -1,17 +0,0 @@ -STREAM_NAME="" -STREAM_KEY="" - -while true; -do - rm ~/anime/list.* - for f in ~/anime/*.mp4 ; do - echo file "$f" >> ~/anime/list.txt; - #echo file "$f" >> ~/anime/list.tmp; - done - #shuf ~/anime/list.tmp > ~/anime/list.txt - ffmpeg \ - -re -f concat -safe 0 -i ~/anime/list.txt \ - -c:a copy -c:v copy \ - -f flv "rtmp://stream.loki/live/${STREAM_NAME}?key=${STREAM_KEY}" - sleep 15 -done diff --git a/package-lock.json b/auth-server/package-lock.json similarity index 100% rename from package-lock.json rename to auth-server/package-lock.json diff --git a/package.json b/auth-server/package.json similarity index 100% rename from package.json rename to auth-server/package.json diff --git a/server.js b/auth-server/server.js similarity index 91% rename from server.js rename to auth-server/server.js index 6f19b66..cad0b95 100644 --- a/server.js +++ b/auth-server/server.js @@ -11,7 +11,7 @@ const streamKeys = require(__dirname+'/streamkeys.js') app.set('query parser', 'simple'); app.use(express.urlencoded({extended: false})); -app.post('/forms/stream', (req, res, next) => { +app.post('/forms/stream', (req, res) => { if (req.body && req.body.name && req.body.key diff --git a/streamkeys.js.example b/auth-server/streamkeys.js.example similarity index 100% rename from streamkeys.js.example rename to auth-server/streamkeys.js.example diff --git a/crontab b/crontab new file mode 100644 index 0000000..83ac5c7 --- /dev/null +++ b/crontab @@ -0,0 +1,3 @@ +# every minute, run both scripts to update viewer count and stream thumbnails +* * * * * bash /var/www/stream/viewers.sh +* * * * * bash /var/www/stream/posters.sh diff --git a/music.sh b/music.sh deleted file mode 100644 index 89bbb28..0000000 --- a/music.sh +++ /dev/null @@ -1,21 +0,0 @@ -STREAM_NAME="" -STREAM_KEY="" - -while true; -do - rm ~/music/list.* - for f in ~/music/*.opus ; do - echo file "$f" >> ~/music/list.tmp; - done - shuf ~/music/list.tmp > ~/music/list.txt - #NOTE: creates 2x2 dummy video stream to make nginx happy, or else it will not show in the stream list. - #NOTE: timeout kills after 12 hours, because nginx is shite. it will loop, shuffle and run again. - timeout --foreground --kill-after=10 43200 ffmpeg \ - -re -f lavfi -i "color=size=2x2:rate=1:color=black" \ - -re -f concat -safe 0 -thread_queue_size 512 -i ~/music/list.txt \ - -map 0:v:0 -map 1:a:0 \ - -c:v libx264 -pix_fmt yuv420p -preset superfast -r 2 -g 2 -b:v 1k \ - -c:a aac -b:a 128k -bufsize 512k \ - -f flv "rtmp://stream.loki/live/${STREAM_NAME}?key=${STREAM_KEY}" - sleep 15 -done diff --git a/nginx/rtmp.conf b/nginx/rtmp.conf index 653da33..f764abd 100644 --- a/nginx/rtmp.conf +++ b/nginx/rtmp.conf @@ -7,8 +7,10 @@ rtmp { server { listen 172.16.0.1:1935; - allow 172.16.0.0/16; - deny all; + listen x.x.x.x:1935; + allow publish x.x.x.x; + deny publish all; + allow play all; chunk_size 4096; application live { # sync 100ms; diff --git a/nginx/stream.conf b/nginx/sites-available/stream.conf similarity index 75% rename from nginx/stream.conf rename to nginx/sites-available/stream.conf index c8a46f0..9301b99 100644 --- a/nginx/stream.conf +++ b/nginx/sites-available/stream.conf @@ -1,12 +1,8 @@ -server { server { server_name stream.loki www.stream.loki; - listen unix:/var/lib/haproxy/haproxy-nginx.sock; - real_ip_header X-Forwarded-For; - set_real_ip_from unix:; - + listen 172.16.0.1:80; allow 172.16.0.0/16; deny all; @@ -26,26 +22,25 @@ server { } location /stat.xsl { - root /var/www/stream/; + root /var/www/stream/public/; } location /viewers/ { default_type text/plain; add_header Content-Type "text/plain"; add_header Refresh "30; $request_uri"; - root /var/www/stream/; + root /var/www/stream/public/; try_files $uri /viewers.txt =404; } location /posters/ { - root /var/www/stream/; + root /var/www/stream/public/; try_files $uri /offline.png =404; } location / { - root /var/www/stream/; + root /var/www/stream/public/; try_files $uri $uri/hls.html =404; } - } diff --git a/posters.sh b/posters.sh index d13cfeb..a392b9e 100755 --- a/posters.sh +++ b/posters.sh @@ -10,8 +10,8 @@ do tsfile=`bash -c "ls -t /tmp/hls/$streamer-*.ts" | head -n1` ffmpeg -hide_banner -loglevel error -y \ -ss 00:00:00.00 -i "$tsfile" \ - -frames:v 1 "/var/www/stream/posters/$streamer.jpg" < /dev/null; + -frames:v 1 "/var/www/stream/public/posters/$streamer.jpg" < /dev/null; done <<< "$all_live" # delete old posters -bash -c 'find /var/www/stream/posters/*.jpg -mmin +2 -exec rm {} \;' +bash -c 'find /var/www/stream/public/posters/*.jpg -mmin +2 -exec rm {} \;' diff --git a/nginx/www/stream/css/video-js.min.css b/public/css/video-js.min.css similarity index 100% rename from nginx/www/stream/css/video-js.min.css rename to public/css/video-js.min.css diff --git a/nginx/www/stream/dash.html b/public/dash.html similarity index 100% rename from nginx/www/stream/dash.html rename to public/dash.html diff --git a/nginx/www/stream/favicon.ico b/public/favicon.ico similarity index 100% rename from nginx/www/stream/favicon.ico rename to public/favicon.ico diff --git a/nginx/www/stream/hls.html b/public/hls.html similarity index 100% rename from nginx/www/stream/hls.html rename to public/hls.html diff --git a/nginx/www/stream/js/video.min.js b/public/js/video.min.js similarity index 100% rename from nginx/www/stream/js/video.min.js rename to public/js/video.min.js diff --git a/nginx/www/stream/offline.png b/public/offline.png similarity index 100% rename from nginx/www/stream/offline.png rename to public/offline.png diff --git a/nginx/www/stream/stat.xsl b/public/stat.xsl similarity index 100% rename from nginx/www/stream/stat.xsl rename to public/stat.xsl diff --git a/nginx/www/stream/viewers.txt b/public/viewers.txt similarity index 100% rename from nginx/www/stream/viewers.txt rename to public/viewers.txt diff --git a/viewers.sh b/viewers.sh index d2877be..30e292a 100755 --- a/viewers.sh +++ b/viewers.sh @@ -37,8 +37,8 @@ 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" + echo "👁️ $num" > "/var/www/stream/public/viewers/$name.txt" done <<< "$viewers" # delete any not updated viewer files (no longer live) -bash -c 'find /var/www/stream/viewers/*.txt -mmin +0.5 -exec rm {} \;' +bash -c 'find /var/www/stream/public/viewers/*.txt -mmin +0.5 -exec rm {} \;'