Stream Live HLS Video Ubuntu Nginx

The first step is to read and set up your server "Nginx-RTMP on Ubuntu 14.04". 

Instead of compiling with:

./configure --with-http_ssl_module  --add-module=../nginx-rtmp-module-master

Use this string in the procedure outlined in the prerequisite doc:

./configure --with-http_ssl_module --with-http_stub_status_module --add-module=../nginx-rtmp-module-master

The example in this tutorial will create both "live" and "mobile" (optimized) streams and will use ffmpeg (installed in the previous tutorial) to generate the bit-rate adjusted, mobile-optimized HLS stream. The example will also show how to cause the server to record your live streams automatically and to allow you to play the recordings back as a video on demand (VOD) replay service.

First, create the folder structures necessary to hold the live and mobile HLS manifests and video fragments:

sudo mkdir /HLS
sudo mkdir /HLS/live
sudo mkdir /HLS/mobile
sudo mkdir /video_recordings
sudo chmod -R 777 /video_recordings

It's probably a good idea to have your firewall turned on if you haven't done so already. If so, you must allow traffic into the ports used by Nginx and HLS. If you'd like to run without the firewall for now, ignore the ufw section below.

HLS streaming requires a significantly different Nginx configuration from the RTMP configuration in the first article. Edit your nginx.conf file to use the following, substituting "my-ip" and "my-stream-key" with your info. You can use anything you would like for "my-stream-key" it's just a word which is unique and helpful to you. You may wish to back up your original config file first, then paste my supplied configuration info into the editor, replacing everything that was there:

sudo cp /usr/local/nginx/conf/nginx.conf /usr/local/nginx/conf/nginx.conf.original
sudo nano /usr/local/nginx/conf/nginx.conf

New nginx.conf:

worker_processes  1;
error_log  logs/error.log debug;
events {
worker_connections  1024;
}
rtmp {
server {
listen 1935;
allow play all;

#creates our "live" full-resolution HLS videostream from our incoming encoder stream and tells where to put the HLS video manifest and video fragments
application live {
allow play all;
live on;
record all;
record_path /video_recordings;
record_unique on;
hls on;
hls_nested on;
hls_path /HLS/live;
hls_fragment 10s;

#creates the downsampled or "trans-rated" mobile video stream as a 400kbps, 480x360 sized video
exec ffmpeg -i rtmp://192.168.254.178:1935/$app/$name -acodec copy -c:v libx264 -preset veryfast -profile:v baseline -vsync cfr -s 480x360 -b:v 400k maxrate 400k -bufsize 400k -threads 0 -r 30 -f flv rtmp://192.168.254.178:1935/mobile/$;
}

#creates our "mobile" lower-resolution HLS videostream from the ffmpeg-created stream and tells where to put the HLS video manifest and video fragments
application mobile {
allow play all;
live on;
hls on;
hls_nested on;
hls_path /HLS/mobile;
hls_fragment 10s;
}

#allows you to play your recordings of your live streams using a URL like "rtmp://my-ip:1935/vod/filename.flv"
application vod {
play /video_recordings;
}
}
}


http {
include       mime.types;
default_type  application/octet-stream;

server {
listen 80;
server_name 192.168.254.178;

#creates the http-location for our full-resolution (desktop) HLS stream - "http://my-ip/live/my-stream-key/index.m3u8"      
location /live {
types {
application/vnd.apple.mpegurl m3u8;
}
alias /HLS/live;
add_header Cache-Control no-cache;
}

#creates the http-location for our mobile-device HLS stream - "http://my-ip/mobile/my-stream-key/index.m3u8"        
location /mobile {
types {
application/vnd.apple.mpegurl m3u8;
}
alias /HLS/mobile;
add_header Cache-Control no-cache;
}   

#allows us to see how stats on viewers on our Nginx site using a URL like: "http://my-ip/stats"     
location /stats {
stub_status;
}

#allows us to host some webpages which can show our videos: "http://my-ip/my-page.html"     
location / {
root   html;
index  index.html index.htm;
}   
}
}

Press Ctrl + X to exit. Say "yes" to save the changes.

You can find clear instructions and examples of the variables possible in this nginx.conf file if you query your favorite search engine for "nginx-rtmp directives". I've been using nginx-rtmp with HLS for some years now, without using the "allow publish" and "deny publish" directives and I've seen zero instances of people using/invading my video servers. So I did not include those directives here. Read about and add these directives if you would like.

After changing the nginx.conf file, you must restart Nginx to use the new configuration:

sudo service nginx restart

Watch closely for any Nginx error messages and address any errors which may have been caused by miss-spelling, folder ownership, or permissions issues. If you have no error messages, then you're ready to create your encode stream.

 

You must have a video encoder in order to create the stream. OBS (Open Broadcaster Software) - which is open-source and works well . There are other solutions to choose from, which are outside the scope of this tutorial. I will not cover everything about configuring an RTMP video encoder. They all require roughly the same input variables though. The key settings you'll need to input in order to use my exact nginx.conf configuration and to function well across most players/browsers/platforms are as follows:

  1. Encoder-x264
  2. Variable bitrate (not CBR or Constant Bit Rate), Quality highest
  3. Max bitrate-600kbps
  4. Audio-Codec-AAC
  5. Audio-Format-44.1khz
  6. Audio-bitrate-64kbps
  7. FMS URL-"rtmp://my-ip:1935/live"
  8. Stream Key-"my-stream-key"
  9. Resolution-640x480
  10. FPS (frames per second)-30
  11. CFR (Constant Frame Rate) - Yes
  12. Keyframe interval-2 seconds (one keyframe every 2 seconds)
  13. x264 Encoding Profile-baseline (may work with main—depends on player used)
  14. x264 CPU Present-veryfast

Once your encoder is set up, you can test it all. Start the encoder up with your webcam or some kind of test-fodder running on it. You can view your broadcast at this point with VLC player using URL's such as:

http://my-ip/live/my-stream-key/index.m3u8
http://my-ip/mobile/my-stream-key/index.m3u8

These are for your main and your mobile video streams, respectively. Substitute your IP and stream key accordingly.

After you've successfully broadcasted your first stream check (via ssh or ftp) that your live broadcast was recorded in the /video_recordings folder on your server. You can also try playing this recorded file in VLC with a URL like:

rtmp://my-ip/vod/filename.flv

Nginx stats are also available (with Nginx stub_status). To view visitor/viewer stats, access:

http://my-ip/stats
  • 0 Users Found This Useful
Was this answer helpful?

Related Articles

Run OPTIMIZE TABLE to defragment tables for better performance

For this suggestion:Run OPTIMIZE TABLE to defragment tables for better performanceRun this...

How to clear Error Log of a cPanel Account.

User the below code to clear Error Log:Replace User with Username:Code:for i in `find...

What is php.ini ?

The php.ini file and changing PHP Settings The php.ini file is a special file for PHP and...

How to fix ini_set() has been disabled for security reasons

1. Create php.ini file inside your public_html folder, OR the folder in which you have...

How to fix Fatal error: Uncaught exception 'Exception' with message 'DateTimeZone:

1: Create .htaccess file inside your public_html folder, OR the folder in which you have...