EZSTREAM for streaming

EZSTREAM for streaming

ezstream is a command line source client software that can stream audio with/without reconding to an icecast or shoutcast server.

In its basic mode of operation, it streams media files or data from standard input without reencoding and thus requires only very little CPU resources. It can also use various external decoders and encoders to reencode from one format to another, and then stream the result to an Icecast server. I’m using it to stream a list of mp3 files to a webradio server. My use case is:

I want to stream the podcasts that I host in a folder, with some auto-update mechanism, so that If a new podcast is added, then it’s played (sooner or later).

ezstream can be used for streaming with a configuration file and it can be launched with:

ezstream -c mystream.xml

where mystream.xml is the configuration file

EZSTREAM configuration

ezstream use an xml file for configuration. Mine is very simple and I’ve used a modified version of the example that ezstream itself provides. Here’s the configuration I’m using:

<format>MP3</format>
<filename>/myfolder/playlist.txt</filename>
<stream_once>0</stream_once>

format: is the file format

filename: it’s the name and the path of a text file that contains a path of every single podcast, one per line

stream_once: zero (0) stream and then restart the list, one (1) stream the list once.

In the file there are other configurations: <url>, <sourcepassword>, <svrinfobitrate> but I think these are trivial.

The playlist

ezstream provide a very usefull feature to shuffle a list of file, it’s documented so you can check it with -h flag, and you can run it with:

ezstream -s playlist.txt

this command gets the playlist.txt input file, shuffles it, and sends it to the stdoutput, so it writes the shuffled list to the console. You can redirect the stdoutput to a file so that the list is shuffled. But, to be honest, I need something similar… but different. I want a shuffled version of the list but every X podcast, I want to insert two sounds, two musical cuts. So I’ve used a scripting language to generate the playlist. Unfortunately for you, I’ve used php, but let’s just analyze the logic:

<?php
$baseFolder = "/music/";
$cutArray = array("jingle.mp3","spot.mp3");
$playlistFile = "/myfolder/playlist.txt";
$everySong =3;

if (file_exists($playlistFile)){
    unlink($playlistFile);
}

foreach (glob($baseFolder . '*.mp3', GLOB_NOSORT) as $file) {
     $tmp = basename($file);
     if (strpos($tmp, 'news-automatiche') === false) {
         $allfile[] = $baseFolder . basename($tmp);
     }
}
shuffle($allfile);

$f = fopen($playlistFile, "w+");
foreach ($allfile as $index => $file) {
    if ($index % $everySong == 0) {
        foreach ($cutArray as $sng) {
            fwrite($f, $sng . PHP_EOL);
        }
     }
     fwrite($f, $file . PHP_EOL);
}
fclose($f);

I check if the file exists and, if yes, I delete it. Then I scan the folder $baseFolder for mp3 files and store them in an Array (with full path).

The array is shuffled with shuffle($allfile)

Then I write the playlist file line by line, and with % (mod mathematic operation), every 3 podcast files (defined with the variable $everySong), I write the two musical cut sounds.

Shuffle and update playlist

How can I reload the playlist with the new added podcasts? I’ve written a bash script that I run with cron:

Every 12 hours the cronjob runs the php script to write a new playlist file. This generated playlist file includes all the podcasts, including the added ones. Then, with the usage of sig -HUP signal mechanism of ezstream, I can reload this new playlist.

#!/bin/bash

SERVICE="ezstream"
COMMAND="pidof $SERVICE"
EZSTREAMPID=$(eval $COMMAND)

# Randomize playlist and insert sounds
php createPlaylist.php

 # Rereads the playlist file
kill -HUP $EZSTREAMPID

The main point here is kill -HUP $EZSTREAMPID: this command sends signal -HUP to the running ezstream. This signal:

  1. Rereads the playlist file
  2. Checks, in the new playlist file, where is the current playing podcast file
  3. If the current playing podcast file exists, the next song will be the one below it. Otherwise the next song will be the first of the playlist file

Conclusion

The need for an icecast client player that can stream podcast files with an update functionality is solved with ezstream and some lines of code, easy to read and to configure for your needs.

MiroAdmin