From 012e9283ef3fa4ea833ce4c36e47714c3c580653 Mon Sep 17 00:00:00 2001
From: Pavel Kirilin <win10@list.ru>
Date: Sat, 23 May 2020 14:15:10 +0400
Subject: [PATCH] Fixed encoding, analytics. Description: - Audiocodec changed
 to vorbis. - Fixed sout-keep dropping. - Added nginx config and snapshoting
 script. - Added analitics scripts.

Signed-off-by: Pavel Kirilin <win10@list.ru>
---
 Makefile                           |  8 +--
 assets/analitics/graphics.py       | 39 ++++++++++++++
 assets/scripts/playerctl_loader.sh | 86 ++++++++++++++++++++----------
 assets/utils/nginx.conf            | 41 ++++++++++++++
 assets/utils/stream_snapshot       | 10 ++++
 5 files changed, 151 insertions(+), 33 deletions(-)
 create mode 100644 assets/analitics/graphics.py
 create mode 100644 assets/utils/nginx.conf
 create mode 100755 assets/utils/stream_snapshot

diff --git a/Makefile b/Makefile
index f288578..ea03b43 100644
--- a/Makefile
+++ b/Makefile
@@ -21,8 +21,8 @@ ifndef back
 	$(eval back=${DEFAULT_BG})
 endif
 	@echo "Compiling ending"
-	@sh ${ASSETS_DIR}/scripts/downloader.sh "${ASSETS_DIR}backgrounds/${back}" "${OUT_DIR}/ending_src"
-	@ffmpeg -stream_loop -1 -i "${OUT_DIR}/ending_src" -i "${ASSETS_DIR}/sounds/${sound}" -vf "subtitles=${ASSETS_DIR}/messages/announce.ass" -c:a copy -strict -1 -shortest -y "${OUT_DIR}ending.mp4"
+	@sh ${ASSETS_DIR}/scripts/downloader.sh "${back}" "${OUT_DIR}/ending_src"
+	@ffmpeg -stream_loop -1 -i "${OUT_DIR}/ending_src" -i "${sound}" -vf "subtitles=${ASSETS_DIR}/messages/announce.ass" -c:a copy -strict -1 -shortest -y "${OUT_DIR}ending.mp4"
 	@echo "Ending compiled successfully"
 
 build_ad: cleanup out_dir
@@ -61,8 +61,8 @@ preview:
 playerctl_load:
 	@sh ${ASSETS_DIR}/scripts/playerctl_loader.sh
 
-play_radio:
-	vlc --no-dbus --sout '#transcode{vcodec=VP80,ab64,acodec=opus}:standard{access=http,mux=ogg,dst=:8888/}' --sout-mux-caching 10000
+stream:
+	vlc --no-dbus -vv --sout-keep --sout '#transcode{vcodec=VP80,ab64,acodec=vorbis}:gather:std{access=http,mux=ogg,dst=:8888/}' --sout-all --sout-mux-caching 10000
 
 cleanup:
 	@echo "Cleaning up outdir"
diff --git a/assets/analitics/graphics.py b/assets/analitics/graphics.py
new file mode 100644
index 0000000..835f157
--- /dev/null
+++ b/assets/analitics/graphics.py
@@ -0,0 +1,39 @@
+#!/usr/bin/env python
+# -*- coding: utf-8 -*-
+
+import pathlib
+from dateutil import parser
+from matplotlib import pyplot as plt
+
+
+def format_record(record_line):
+    time_str, count = record_line.split(']')
+
+    time_str = time_str.rstrip(']').lstrip('[').strip()
+    date = parser.parse(time_str)
+
+    count = int(count.strip())
+    return (date, count)
+
+
+def get_records():
+    record_path = pathlib.PosixPath('~/.stream_stats')
+    records = record_path.expanduser()
+    stats = []
+    with open(records) as f:
+        stats = list(map(format_record, f.readlines()))
+    return stats
+
+
+def draw(records):
+    dates = list(map(lambda x: x[0], records))
+    listeners = list(map(lambda x: x[1], records))
+    plt.bar(dates, listeners)
+    plt.xlabel('time')
+    plt.ylabel('listeners')
+    plt.show()
+
+
+if __name__ == "__main__":
+    records = get_records()
+    draw(records)
diff --git a/assets/scripts/playerctl_loader.sh b/assets/scripts/playerctl_loader.sh
index 690237f..637eec9 100644
--- a/assets/scripts/playerctl_loader.sh
+++ b/assets/scripts/playerctl_loader.sh
@@ -1,21 +1,55 @@
 #!/bin/bash
 
-if [[ !( $(playerctl status) ) ]]; then
-  echo "Run tuijam or some other player"
-  exit 1
-fi
-
-if [[ $(playerctl status) != "Playing" ]]; then
-  playerctl next
-fi
-
 workdir="$(mktemp -d playlist-XXXXXX)"
 ads_dir="../assets/ads/"
 today="$(date +%D)"
 res_dir="radio/${today}"
-
+current_player=""
+job_started=0
 mkdir -p "$res_dir"
 
+function player(){
+  if [[ -n "$current_player" ]];then
+    res="$(eval "playerctl --player=$current_player $@ || echo 'ERROR'")" 
+  else
+    res="$(eval "playerctl $@ || echo 'ERROR'")"
+  fi
+  echo "$res"
+}
+
+function cleanup_and_exit() {
+  if [[ "$job_started" = "1" ]];then
+    rm -fv "current.mp3" "bg.png" "cover.jpg" "cover.png"
+    ffmpeg -f concat -i concat -c copy "compiled-$((compiled_index + 1)).webm"
+    rm -fv "concat"
+    popd
+    ls "$workdir" | grep compiled | xargs -I '{}' mv -v $workdir/'{}' "$res_dir"
+  fi
+  rm -rfv "$workdir"
+}
+
+function read_player(){
+  echo "Which player should I use?"
+  printf "Available players:\n$(playerctl -l | sed "s/^/* /")\n"
+  read -p '> ' current_player
+}
+
+trap "cleanup_and_exit" EXIT
+
+playerctl -l
+if [[ !( $(playerctl -l | wc -l) -eq 1) ]];then
+  read_player
+fi
+
+if [[ !( $(player status) ) ]]; then
+  echo "Can't find any player"
+  exit 1
+fi
+
+if [[ $(player status) != "Playing" ]]; then
+  player next
+fi
+
 if [[ -e "${res_dir}" ]];then
   compiled_index="$(ls "$res_dir" | grep compiled | sort --numeric-sort | tail -n 1 | cut -d '.' -f1 | cut -d '-' -f2)"
 else
@@ -25,22 +59,13 @@ index="0"
 
 pushd "$workdir"
 
-function cleanup_and_exit() {
-  rm -fv "current.mp3" "bg.png" "cover.jpg" "cover.png"
-  ffmpeg -f concat -i concat -c copy "compiled-$((compiled_index + 1)).webm"
-  rm -fv "concat"
-  popd
-  ls "$workdir" | grep compiled | xargs -I '{}' mv -v $workdir/'{}' "$res_dir"
-  rm -rfv "$workdir"
-}
+job_started=1
 
-trap "cleanup_and_exit" EXIT
-
-while [[ "$(playerctl status)" = "Playing" ]];do
-  title="$(playerctl metadata xesam:title)"
-  artist="$(playerctl metadata xesam:artist)"
-  artUrl="$(playerctl metadata mpris:artUrl)"
-  url="$(playerctl metadata xesam:url)"
+while [[ "$(player status)" = "Playing" ]];do
+  title="$(player metadata xesam:title)"
+  artist="$(player metadata xesam:artist)"
+  artUrl="$(player metadata mpris:artUrl)"
+  url="$(player metadata xesam:url)"
 
   # Downloading song and album cover
   wget "${url}" -O "current.mp3"
@@ -65,9 +90,11 @@ while [[ "$(playerctl status)" = "Playing" ]];do
     [img][ladv]overlay=enable='gt(mod(t, 40), 30)'[rv]" \
     -map "[rv]" \
     -map 1:a \
-    -framerate 30 -g 360 \
+    -framerate 30 -g 60 \
     -c:v vp8 -pix_fmt yuv420p -movflags +faststart \
-    -c:a libopus \
+    -vb 4000k \
+    -f webm \
+    -c:a libvorbis \
     -err_detect ignore_err \
     -shortest -strict -2 -y \
     "${index}.webm"
@@ -75,8 +102,9 @@ while [[ "$(playerctl status)" = "Playing" ]];do
   echo "file '${index}.webm'" >> concat
 
   index=$((index + 1))
-  if ! (playerctl next);then
+  if [[ "$(player next)" = "ERROR" ]];then
     break
   fi
 done
-playerctl stop
+
+player stop
diff --git a/assets/utils/nginx.conf b/assets/utils/nginx.conf
new file mode 100644
index 0000000..975dcf7
--- /dev/null
+++ b/assets/utils/nginx.conf
@@ -0,0 +1,41 @@
+upstream anime_stream {
+ server 192.168.1.101:8889;
+}
+
+upstream radio_stream {
+ server 192.168.1.101:8888;
+}
+
+server{
+  
+  listen 80;
+  server_name s3rius.ddns.net;
+
+  include /var/nginx/common/server.conf;
+  client_max_body_size            100m;
+
+  access_log  /var/log/nginx/s3rius.ddns.net_access.log combined;
+  error_log   /var/log/nginx/s3rius.ddns.net_error.log;
+
+  location /stats {
+    stub_status on;
+    access_log off;
+    allow all;
+  }
+
+  location /anime {
+    resolver 127.0.0.11 valid=30s;
+    proxy_http_version 1.1;
+    proxy_set_header Connection "";
+    proxy_pass  http://anime_stream/;
+  }
+
+  location / {
+    resolver 127.0.0.11 valid=30s;
+    proxy_http_version 1.1;
+    proxy_set_header Connection "";
+    default_type video/webm;
+    proxy_pass  http://radio_stream/;
+  }
+  
+}
diff --git a/assets/utils/stream_snapshot b/assets/utils/stream_snapshot
new file mode 100755
index 0000000..a522319
--- /dev/null
+++ b/assets/utils/stream_snapshot
@@ -0,0 +1,10 @@
+#!/bin/bash
+count="$(curl -X GET -s http://s3rius.ddns.net/stats | grep Active | cut -d ':' -f2 | xargs)"
+http_status="$(curl -sw '%{http_code}' -o /dev/null http://s3rius.ddns.net/stats)"
+if [[ "$http_status" -ne "200" ]];then
+  exit 0
+fi
+users="$((count - 1))"
+time="$(timedatectl | grep Local | sed 's/Local time: [[:alpha:]]\+//' | xargs)"
+touch ~/.stream_stats
+echo "[$time] $users" >> /home/$USER/.stream_stats
-- 
GitLab