Skip to main content

Poor Mans Stream Switcher Server

So you need to switch from a constant stream into a live event automatically.



Let us say you have a channel stream coming from one location and want to insert live events into the stream without to much manual intervention, this should do it but still needs some testing.

I'm using HLS because seems it is the easiest to get showing on a page and mobile. Use video.js and the documentation is good. For test playback I'm simply going to use VLC desktop app.

I also figured out that CVLC removes the *.m3u8 file if it cannot find an input, but does not die if set in loop.

I will be using what we can get out of the Linux distros to keep setup easy and quick.

We are going to use:
  • crtmpserver
  • cvlc
  • apache
First get everything installed.




I do suggest changing the default crtmpserver applications so you do not end up being an open relay server for video streams, but for this example I'm going to use the default applications.

We are also going to assume some default paths which should be clear in the commands.

Stream 1:

cvlc rtmp://continues.stream.some.server.com:1935/application/stream --loop --sout '#transcode{width=320,height=240,fps=25,vcodec=h264,vb=256,venc=x264{aud,profile=baseline,level=30,keyint=30,ref=1},acodec=mp3,ab=96}:std{access=livehttp{seglen=10,delsegs=true,numsegs=5,index=/var/www/html/stream1/stream.m3u8,index-url=http://some.server.com/stream1/stream-########.ts},mux=ts{use-key-frames},dst=/var/www/html/stream1/stream-########.ts}’
Stream 2:

cvlc rtmp://127.0.0.1:1935/flvplayback/livestream --loop --sout '#transcode{width=320,height=240,fps=25,vcodec=h264,vb=256,venc=x264{aud,profile=baseline,level=30,keyint=30,ref=1},acodec=mp3,ab=96}:std{access=livehttp{seglen=10,delsegs=true,numsegs=5,index=/var/www/html/stream2/stream.m3u8,index-url=http://some.server.com/stream2/stream-########.ts},mux=ts{use-key-frames},dst=/var/www/html/stream2/stream-########.ts}’

stream.m3u8.php file:

<?php
if (file_exists('/var/www/html/stream2/stream.m3u8')) {
$m3u8file = file_get_contents('/var/www/html/stream2/stream.m3u8', true);
print $m3u8file;
} else {
        $m3u8file = file_get_contents('/var/www/html/stream1/stream.m3u8', true);
        print $m3u8file;
}
?>

.htaccess file:

RewriteEngine on
RewriteRule stream\.m3u8$ stream.m3u8.php [NC]


Now if you open http://some.server.com/stream.m3u8 you should see the continues stream.

We are sending the stream1/stream.m3u8 file contents created by the 1st CVLC to the player.

If you send another stream to rtmp://some.server.com:1935/flvplayback/livestream the 2nd CVLC will start creating the stream2/stream.m3u8 file and the player will pick that up the next time it requests a new stream.m3u8 file.

Popular posts from this blog

Double Bunk in Caravan

With our family expanding, we faced a challenge of adding another berth in our caravan, but I did not want to make it permanent.
A couple of options are available, of which one is simply to add a bunk to the single berth. We however did not want to cause any permanent markings on the interior of the caravan .

Netdata + SNMP + Mikrotik

Always wanted to see my Internet line usage as a gauge without having to log into the router.
So today I configured SNMP in Netdata to collect from my Mikrotik router.
/etc/netdata/node.d/snmp.conf:
{
"enable_autodetect": false,
"update_every": 5,
"max_request_size": 100,
"servers": [
{
"hostname": "10.1.1.1",
"community": "public",
"update_every": 5,
"max_request_size": 50,
"options": { "timeout": 20 },
"charts": {
"mikrotik1.cpu": {
"title": "CPU ",
"units": "percentage",
"type": "line",
"family": "cpu",
"dimensions": {
"used": {
&qu…

CasparCG Simple Playlist

It is possible to build a simple playlist as a rundown in the CasparCG client. It will very simply play each item after each other that are on the same layer. In this very simple post I show how to build such a playlist including the very important step of activating the OSC.

The Open Sound Control (OSC) implementation is how the client knows what the server is doing and then being able to send new command back to the server when a piece of media has ended to trigger playback of the next piece. For more details on the OCS, please see http://casparcg.com/wiki/CasparCG_OSC_Protocol