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