Category Archive Technology

Second implementation podcast player

Podcast player for web radio site

As a hobby, I’m the “software handyman” in a web radio (also a speaker to be honest). In my spare time, I try to find solutions and cool stuff for all the digital problems related to this activity. It could be something that needs to be automated or some cool stuff on the website. This time, despite the fact that I’m not a good UI/UX, I’ve tried to improve the podcast player on the website. The default web player of WordPress is not that bad, but experimenting is something that I like to do and, after all, none will be killed by a bad player experiment on a web radio site self-financed by its speaker. So I’ve seen some cool players with a waveform on it and I’ve spent time to figure out how these can be included.

 

default worpdress player

Default worpdress player

First Implementation of a new podcast player

After a first implementation, I found version 1 of this new player not-so-dynamic as I’m expecting. And more it also required some additional computation and files (a static file representing the waveform peak).

 

First Implementation podcast player

First Implementation podcast player

Second iteration of a podcast player

So I’ve found a new super dynamic player, in javascript. But it gave me some problems related to the “touch event” needed by the iPhone users to start the sound. After some months of figuring out how to solve it, I finally found a way of making it work.

 

Second implementation podcast player

Second implementation podcast player

It’s not perfect, still can be done better and maybe there are bugs around it but what I want to highlight is the fact that I’ve spent a lot of my free time finding a better solution (better means “better to me”) and solving issues, problems, edge cases, code limitations. See this recap video:

 

 

I’ve had the chance to do this because there is no money, neither human life involved and, basically, because this is a hobby. The possibility of experimenting in a safe environment made the difference and gave me also a vision of what are the other different skills and people involved in the development of a software solution: what could be the problem for a UX, what could push to add a new solution based also on time needed to do it.

My advice is to find space to explore and make mistakes in your workplace or outside your workplace. If you have time take a look at the player, at the website and, if you have a lot of time and you want to listen to an Italian web radio show about technology, space and similar stuff with a lot of music, my show it’s called “Katzenjammer” every Monday, starting at 20:00 Europe/Rome time zone.

There are podcasts as well 😀

https://www.radiocittaperta.it/podcast/utenti-in-pericolo-su-facebook-meta-e-il-pianeta-rosso-katzenjammer-del-20-dicembre/

Maybe I’ll share some tech details and code in a future post, anyway feel free to contact me for details or questions 

Managing a web radio streaming provider

If you manage a Web Radio, if you are an IT consultant, or you’re just the “one of the tech guys” behind the scenes of such a radio, keep in mind the following suggestions. There could be some part of the web integration with your site that may result in future problems or a lot of work. Most of these problems are related to third-party services that you may or may not use, so let’s analyze some of them.

Streaming services

If you manage a Web Radio, you already know that you need a streaming web service in order to make people listen to your streaming audio. Unless you select an “all-in-house” solution, where you have a server with your website, your streaming server, and also podcast spaces, you probably rely on some third-party streaming services. This is also convenient from an economical point of view, because streaming servers can be very cheap, and some of them also come with an una-tantum solution (you paid once and only once). This basically means that your website has an url

https://www.myradiowebsite.com

While your streaming service has a different url, maybe something like this

https://stream.thirdparty.org:8021/stream

While this is a good solution and it works nicely without any problem, please consider that your streaming url (https://stream.thirdparty.org:8021/stream) may be used in multiple places: your website is one of them, but also your mobile app, your other integration with other websites, other webradio aggregator websites and so on.

The problem: streaming url 

When you use a streaming server provider, this basically means you are coupled to a streaming url and if you want to change your streaming server provider, you need to update everything with your new streaming url: your website, your mobile app, the webradio aggregator website (eg. Tunein). More than this, if you need more listeners, the only way of doing this is to pay your streaming server provider for an upgraded plan.

The solution: a redirect proxy

The solution is to set up a simple redirect that can be easily manageable in-house. The redirect should add a streaming URL owned in your website, so something like this:

https://www.myradiowebsite.com/mystreaming --redirect to--> https://stream.thirdparty.org:8021/stream

This can be achieved in different ways: the simplest could probably be adding a rule in your .htaccess file (if you have an apache hosting) or a directive in ngnix configuration, if your hosting supports nginx. The redirect syntax is not covered in this post, you could probably find the right ones just by looking on the internet. I want to cover a slightly different solution that uses a programmatic approach. It’s a PHP redirect solution, but I know that this can be done with any other server-side language. More than having just a redirect, I’ve added the possibility of a random weighted redirect. The idea is that you can set up a redirect that is randomly distributed, so if you have 2 streaming service providers…

server1 = https://my.webradioserver.com:8201/stream 
server2 = https://another.server.com/8020/stream

… you can then weigh for example set 50-50 so that a user could listen to server1 or to server2 with a 50% of probability. This gives you the chance to move to another server with a ramp up, or you can add another server to increase the possible listeners, and assign the user randomly . This is my solution: 

<?php header("Expires: on, 01 Jan 1970 00:00:00 GMT"); header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT"); header("Cache-Control: no-store, no-cache, must-revalidate"); header("Cache-Control: post-check=0, pre-check=0", false); header("Pragma: no-cache"); function getRandomWeightedElement(array $weightedValues) { $rand = mt_rand(1, (int) array_sum($weightedValues)); foreach ($weightedValues as $key => $value) { $rand -= $value; if ($rand <= 0) { return $key; } } } $serverSTreaming1 = 'https://my.webradioserver.com:8201/stream'; $serverSTreaming2 = 'https://another.server.com/8020/stream'; $wheights = array($serverSTreaming1=>0, $serverSTreaming2=>100); header("location: ".getRandomWeightedElement($wheigths)); die();

The section
$wheights = array($serverSTreaming1=>0, $serverSTreaming2=>100);

does all the magic: this setup moves all the traffic to streaming $serverSTreaming2, but you can of course configure it at 50-50 as well, for example with

$wheights = array($serverSTreaming1=>50, $serverSTreaming2=>50);

This solution could easily be extended with more streaming providers and different weights.

Giveaways

With a redirect solution, you can easily change your streaming provider to all your clients like your website, your mobile application, third party web site aggregators etc. because you use the source url and then only change the redirect destination.

You can add multiple sources and weigh them, as per my example.

 

Liked this article? Consider a donation!

How to convert video for Playstation 3 / PS3

I know that downloading and converting video is an old-style procedure that, at the moment, it seems useless and time-consuming. But my PlayStation 3 still works like a charm and I can watch a movie while seating on the couch using the controller as a remote, so quite better than a computer on my legs connected to a streaming website.

A non-confortable position for watching movies

The problem is that the PS3, PlayStation 3, only plays some type of files and very often you need to convert downloaded files to a suitable format. After googling and looking around, I’ve finally found my perfect settings. First of all, you need to use ffmpeg to convert the file. ffmpeg is a super powerful tool that can do a lot of stuff with video and audio. We’ll use it to convert the input file into a format that can be played by PS3. Suppose you have a file named 

INPUT_FILE_NAME.mkv

(.mkv is a format that PS3 can not play) Then, to convert it just use this command:

ffmpeg -y -i "INPUT_FILE_NAME.mkv" -vf scale=1024:-1 -c:v libx264 -pix_fmt nv12 -acodec aac -b:a 192k -ac 2 -ar 44100 -af "aresample=async=1:min_hard_comp=0.100000:first_pts=0" -f mp4 "OUTPUT_FILE_NAME.mp4"

If you hate dubbing (I know that are a lot of movie lovers that hate dubbing, I’m not one of them, to be honest) you can also put the subtitle that is (often) in the .mkv downloaded file, directly into the mp4 in output. Unfortunately, you can not disable it, but it’s a good trade-off.

In this case, use this command

ffmpeg -y -i "INPUT_FILE_NAME.mkv" -vf scale=1024:-1 -c:v libx264 -pix_fmt nv12 -vf subtitles="INPUT_FILE_NAME.mkv":si=0 -acodec aac -b:a 192k -ac 2 -ar 44100 -af "aresample=async=1:min_hard_comp=0.100000:first_pts=0" -f mp4 "OUTPUT_FILE_NAME.mp4" > /dev/null 2>&1 < /dev/null &

Where I’ve just added the part

-vf subtitles="INPUT_FILE_NAME.mkv":si=0

that basically tells ffmpeg to “get the subtitle from the input file, get the first subtitle track, number 0, and render them into the output file”. You can change the file name and, as long as ffmpeg can read the input file, you’ll have a file usable by your PS3. Another option I’ve added it’s the > /dev/null 2>&1 < /dev/null & at the end of the command so that it’ll run in background.

The first part > /dev/null 2>&1 basically tells ffmpeg to put any output to nowhere.

The second part < /dev/null tells ffmpeg that there is no interactive input (yes ffmpeg is an interactive tool!)…

… and the last part & means “run in background“!

 

Liked this article? Consider a donation!

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.

Liked this article? Consider a donation!