Page 1 of 1

Fetch and prepare ö1 Metadata for streamripper

Posted: Fri Nov 11, 2011 9:19 pm
by ^rooker
[PROBLEM]
The Austrian radio channel "Ö1" sends confusing title metadata within their live streams.

[SOLUTION]
After reading the source code of their streaming website, it was clear that the URL http://oe1.orf.at/programm/konsole/live contains the currently running title (and a few of the subsequent ones), encoded in JSON format.

One can now simply use plain-vanilla PHP, and transform the JSON data into something that, for example, streamripper can read:

Code: Select all

#!/usr/bin/php
<?php
$PAUSE=3;           // Wait n seconds before refreshing the metadata
$METADATA_URL="http://oe1.orf.at/programm/konsole/live";

while(true)
{
    // Fetch stream metadata in JSON format from OE1 website:
    $json_string = file_get_contents($METADATA_URL);

    // Parse JSON data into PHP object:
    $json_data = json_decode($json_string);
    $current_track = $json_data->list[0];

    printf("TITLE=(%s) %s\nARTIST=OE1 [ID:%d]\n.\n",
        trim($current_track->time),
        $current_track->title,
        $current_track->id
    );

    sleep($PAUSE);
}
?>
NOTE: It's important that the script continuously refreshes the metadata itself - therefore the infinite "while" loop, and the $PAUSE sleep.

If you save the PHP code above in a file called "metadata-oe1.php", you can call streamripper like this:

Code: Select all

streamripper http://mp3stream3.apasf.apa.at:8000/listen.pls -d /srv/streamripper/OE1 -E /path/to/metadata-oe1.php

Clean script for stream recording

Posted: Sun Jun 03, 2012 2:09 pm
by ^rooker
Here is a short BASH script used for recording different streams:

Code: Select all

#!/bin/bash

# ---------------
STREAM_OE1="http://mp3stream3.apasf.apa.at:8000/listen.pls"
METADATA_OE1="/path/to/metadata-oe1.php"

STREAM_BLA="http://wherever.bla/stream.m3u"
# ---------------

DIR_OUTPUT="/srv/streamripper"

function start_rip
{
    local SOURCE="$1"
    local METADATA=$(eval "echo \$METADATA_$SOURCE")
    local STREAM_URL=$(eval "echo \$STREAM_$SOURCE")
    local DIR_OUTPUT="$DIR_OUTPUT/$SOURCE"

    if [ -z "$STREAM_URL" ]; then
        echo ""
        echo "No URL defined for source '$SOURCE'. Exiting."
        echo ""
        return
    fi

    echo ""
    echo "Recording '$SOURCE'..."
    echo " - URL: $STREAM_URL"

    local CMD="streamripper $STREAM_URL -s -d \"$DIR_OUTPUT\""
    if [ -n "$METADATA" ]; then
        echo " - Metadata: $METADATA"
        CMD="$CMD -E $METADATA"
    fi
    echo "$CMD"

    read -p "Press any key to continue..."
    eval "$CMD"
}

start_rip "$1"
This script can be called with different "callsigns" of streams (example: "OE1", "BLA") and will create subfolders with these callsigns in "/srv/streamripper". Metadata handling scripts can optionally be provided per-stream.

Connection timed out...

Posted: Wed Jan 08, 2014 5:08 pm
by ^rooker
After upgrading to Ubuntu 12.04.3 LTS, I had weird problems with my metadata-fetching code:
PHP Warning: file_get_contents(http://oe1.orf.at/programm/konsole/live): failed to open stream: Connection timed out in metadata-oe1.php on line 9
PHP Notice: Trying to get property of non-object in metadata-oe1.php on line 15
Seems like something (either on my side or theirs) changed which causes this connection timeout.

I've now added checks to avoid accessing the $json_data variable unless it's been set:

Code: Select all

#!/usr/bin/php
<?php
$PAUSE=3;           // Wait n seconds before refreshing the metadata
$METADATA_URL="http://oe1.orf.at/programm/konsole/live";

while(true)
{
    // Fetch stream metadata in JSON format from OE1 website:
    $json_string = file_get_contents($METADATA_URL);

    if (isset($json_string) && !empty($json_string))
    {
echo "JSON string: ".$json_string; //DELME

        // Parse JSON data into PHP object:
        $json_data = json_decode($json_string);
        if (!empty($json_data))
        {  
            $current_track = $json_data->list[0];

            printf("TITLE=(%s) %s\nARTIST=OE1 [ID:%d]\n.\n",
                trim($current_track->time),
                $current_track->title,
                $current_track->id
            );
        }
    }

    sleep($PAUSE);
}
?>

Metadata encoding

Posted: Wed Jan 08, 2014 5:11 pm
by ^rooker
I've also encountered character encoding issues in the files produced by streamripper.
Luckily, streamripper has commandline arguments which allow to define the encoding to be used for metadata:
  • --codeset-filesys: Tells streamripper what codeset to use for the file names when it writes to your hard drive.
  • --codeset-id3: Tells streamripper what codeset to use for the id3 information.
  • --codeset-metadata: Tells streamripper what codeset is being used for metadata in the stream coming from the network.
I've set all of them now to "UTF-8" and now my filenames with umlauts are perfectly fine :)
Thanks to a thread about streamripper encoding on ubuntuusers.de