Categories
4k IT Servers Streaming

Another Nginx Re-Streamer With HLS Output

Recently I had a request to see if I would be able to provide some sort of a tutorial on how to make a re-streaming server for Facebook and YouTube as well as having an HLS output as well, preferably with various resolutions.

My server is based on the rtmp-module in nginx. We should install this on an Ubuntu server from Lightsail. I usually make a very small one for $3.50 per month, but then if I want to stream with HLS I need to use a snapshot and create a new instance that is much larger. Otherwise it won’t be able to handle all the transformations of the data to make the HLS files and it will fail.

This server in the stock configuration as shown will re-stream to any other RTMP locations that you need and also send out HLS to places in various resolutions. If you send it a 1080p 30fps input, it will send out a 1080, 720, and 360 HLS output as well. If you make some adjustments that I show in an optional section, then you can make it take in 4k and re-stream that to any RTMP places you wish, plus output on HLS 4k, 1080, 720, and 360.

Install nginx and update ubuntu

sudo apt update
sudo apt upgrade
sudo apt install -y nginx
sudo apt install -y libnginx-mod-rtmp

Now we have to install a bunch of needed packages to allow for the media streaming to properly occur.

sudo apt install -y software-properties-common
sudo dpkg --add-architecture i386
sudo apt update

The entire below code should be copied and pasted in its entirety at once.

sudo apt install wget nano python-certbot-nginx ufw unzip software-properties-common dpkg-dev git make gcc automake build-essential joe ntp ntpdate zlib1g-dev libpcre3 libpcre3-dev libssl-dev libxslt1-dev libxml2-dev libgd-dev libgeoip-dev libgoogle-perftools-dev libperl-dev pkg-config autotools-dev gpac ffmpeg sysstat nasm yasm mediainfo mencoder lame libvorbisenc2 libvorbisfile3 libx264-dev libvo-aacenc-dev libmp3lame-dev libopus-dev libfdk-aac-dev libavcodec-dev libavformat-dev libavutil-dev g++ libc6:i386 freeglut3-dev libx11-dev libxmu-dev libxi-dev libglu1-mesa libglu1-mesa-dev
sudo apt install mariadb-server mariadb-client phpmyadmin php php-cgi php-common php-pear php-mbstring php-fpm

These are to allow for the rtmp module to be installed and the statistics to be made.

cd /usr/src

sudo git clone https://github.com/arut/nginx-rtmp-module

sudo cp /usr/src/nginx-rtmp-module/stat.xsl /var/www/html/stat.xsl

This is the crossdomain file

sudo nano /var/www/html/crossdomain.xml

Paste the below into that file.

<?xml version="1.0"?>
<!DOCTYPE cross-domain-policy SYSTEM "http://www.adobe.com/xml/dtds/cross-domain-policy.dtd">
<cross-domain-policy>
<allow-access-from domain="*"/>
</cross-domain-policy>

This is the info.php file.

sudo nano /var/www/html/info.php

Paste the below into that file.

<?php
phpinfo();
?>

This is to create the locations of the hls streams.

sudo mkdir /var/livestream
sudo mkdir /var/livestream/hls
sudo mkdir /var/livestream/med
sudo mkdir /var/livestream/low

This is to give the proper permissions the livestream location can be used.

sudo chown -R www-data: /var/livestream

Now we have to create the nginx.conf file. This is where the majority of the server gets its programing.

sudo nano /etc/nginx/nginx.conf

The below file should be used as the nginx.conf file. I took off the .conf extension and made it a .txt so the site would allow me to post it.

In all the files that I have here, if you use them, please be sure to change the IP to your IP address. You will have to change them by using “sudo nano” and the file name such as nginx.conf. If you click on the link below that says “nginx”, it will open up a new browser window and you can copy and paste this into your nginx.conf file.


Skip this section if you don’t need 4K.

Below is the nginx.conf file that you should use if you want the streamer to make 4 types of HLS files. It can take 4k and create 4k, 1080, 720 and 360 outputs. But it needs a very large server to do it all. Also you’ll have to create the proper html files that allow you to show people the created outputs, or just use it to send to other services. I didn’t include the proper html files in this tutorial. You should name this “nginx.conf” if you use it.

Remember that you’ll also need to make directories where the “high” files will be stored.


sudo nginx -t
sudo systemctl restart nginx
sudo nano /etc/nginx/sites-available/default

The below default.txt is actually the file that is from /etc/nginx/sites-available/default. I added the .txt extension to it so it would allow me to post it properly. You should delete the .txt extension to make it work if you want to use it directly, or just copy and paste it into the default file when you are in the nano program.

sudo nginx -t
sudo systemctl restart nginx

So that we can create and use a website to manage and check on these streaming server functions we have to have a video player that will play our HLS files on a website. The first two lines below should be copied and pasted individually. The “sudo wget” sections should be done as sections. That will get the .zip file and the .js file that you will need for the video player.

sudo mkdir /var/www/html/videojs
cd /var/www/html/videojs

sudo wget https://github.com/videojs/video.js/releases/download/v7.7.6/video-js-7.7.6.zip

sudo wget https://github.com/videojs/http-streaming/releases/download/v1.13.1/videojs-http-streaming.js

The following lines should be done individually and they will all the video player to be unzipped as well as make the proper allowance for it to be used with the directories we created.

sudo unzip /var/www/html/videojs/video-js-7.7.6.zip
sudo chown -R www-data: /var/www/html
sudo ls -la /var/www/html/videojs

Now we have to make the page that will show the options for the server.

We have to make the index.html page as well as the pages for the videos, low.html, med.html, live.html.

Here are some copies of my files. You’ll have to modify them slightly and add in your IP address on each of them to make them operate. Just replace the IP in some of the lines with your IP.

Note that these are very poor HTML code. I just made them quickly so that I could get a working page for myself. They aren’t neat and they aren’t good. They just work. Maybe sometime in the future I might fix them a bit, but for now I have other things that are more pressing than making them nice and pretty.

sudo nano /var/www/html/index.html
sudo nano /var/www/html/live.html
sudo nano /var/www/html/med.html
sudo nano /var/www/html/low.html

The index file below is set up for making 3 outputs (Low, Med, Live). The Live file output is the same stream output resolution that the input comes into the server. The below is a rough index.html file. It should make a workable webpage where you can choose to see some of your stats or a feed from each of the 3 above resolutions.

<h2 style="text-align: center;"><strong>Streaming Status Pages</strong>&nbsp;</h2> <p style="text-align: 
center;">&nbsp;</p> <p style="text-align: center;"><a href="https://(YOUR IP.Address)/nginx_status" target="_blank" 
rel="noopener">Status of Connections</a></p> <p style="text-align: center;">&nbsp;</p> <p style="text-align: 
center;">&nbsp;</p> <p style="text-align: center;"><a href="https://(YOUR IP.Address)/stat.html" target="_blank" 
rel="noopener">Status of the Stream</a></p> <p>&nbsp;</p> <p style="text-align: center;">&nbsp;</p> <p 
style="text-align: center;">Feed for Computers? <a href="https://(YOUR IP.Address)/live.html" target="_blank" 
rel="noopener">Live Stream "Full 1080 Resolution"</a></p> <p style="text-align: center;">&nbsp;</p> <p 
style="text-align: center;">&nbsp;</p> <p style="text-align: center;">Feed for the App: <a 
href="https://(YOUR IP.Address)/low.html" target="_blank" rel="noopener">Live Stream "Low Resolution"</a></p> <p 
style="text-align: center;">&nbsp;</p> <p style="text-align: center;">&nbsp;</p>

<p style="text-align: center;">Feed for Computers?: <a href="https://(YOUR IP.Address)/med.html" target="_blank" 
rel="noopener">Live Stream "720 Resolution"</a></p> <p style="text-align: center;">&nbsp;</p> <p 
style="text-align: center;">&nbsp;</p> <p style="text-align: center;">&nbsp;</p> <p style="text-align: 
center;">&nbsp;</p> <p style="text-align: center;">&nbsp;</p>
<p>&nbsp;</p>

Now you should test it all by streaming to your RTMP location at rtmp://IP/live/stream. If it takes hold and your streamer attaches then you can click on the websites links to check the feed.

After we have that all running. We have to get stunnel so that facebook will work. We’ll go back to the command prompt from your server. Issue the following commands to get stunnel.

sudo apt-get install stunnel4 -y

Now we’ll have to change stunnel’s boot configuration, issue the following command:

sudo nano /etc/default/stunnel4

Change Enabled from 0 to 1. It should look like the following:

ENABLED=1

Next we have to edit the stunnel configuration file.

sudo nano /etc/stunnel/stunnel.conf

You’ll have to cut and paste this in its entirety. It should look like this:

pid = /var/run/stunnel4/stunnel.pid
output = /var/log/stunnel4/stunnel.log
setuid = stunnel4
setgid = stunnel4
# https://www.stunnel.org/faq.html
socket = r:TCP_NODELAY=1
socket = l:TCP_NODELAY=1
debug = 4
[fb-live]
client = yes
accept = 1936
connect = live-api-s.facebook.com:443
verifyChain = no

Then of course you’ll have to use ctrl-x to exit, and Y to save it as the original named file.

Next we have make it enabled after boot by doing the following:

sudo systemctl enable stunnel4.service

Now we have to restart stunnel because we changed the configuration files.

sudo systemctl restart stunnel4.service

Now since we changed the stunnel configuration and all we should restart NGINX for good measure.

sudo systemctl restart nginx