Stream Status

From Starchive Wiki

The Stream Status tool is comprised of a command that checks for stream silence and a PHP page that reports this for the uptime monitor. The script is hosted at standrewsradio.com/stream-status.php.

Command

The command is as follows:

ffmpeg -i http://garfield.standrewsradio.com:8080/stream/1.mp3 -y -nostats -loglevel 0 -f mp3 -t 00:00.5 - | sox -t mp3 - -n stat 2>&1 | grep "Maximum amplitude" | cut -d' ' -f 7

This is split into four different parts, each operating on the output of the part before:

  1. ffmpeg -i http://garfield.standrewsradio.com:8080/stream/1.mp3 -y -nostats -loglevel 0 -f mp3 -t 00:00.5 -: silently records half a second of the stream as an mp3.
  2. sox -t mp3 - -n stat 2>&1: using sox, read the MP3 recording and get some statistics on it, piping stderr and stdout into stdout.
  3. grep "Maximum amplitude": gets the line of the output that contains information about the maximum amplitude.
  4. cut -d' ' -f 7: cuts the input into parts around spaces and returns the 7th part (this is the maximum amplitude in seconds).

PHP

The PHP script is as follows:

<?php

$output = exec("<the command>");
$val = floatval($output);

if ($val < 0.00025) {
    http_response_code(503);
} else {
    echo "all good :) max amplitude was $val!";
    http_response_code(200);
}

This script simply gets the output of the command that is run and checks the value. If it less than 0.00025, it returns a server error response code for the uptime robot to detect. This number has been chosen completely arbitrarily based on the fact that "silence" appears to fluctuate between 0.0001 and 0.0002.