Monthly Archives: November 2018

Ubuntu 18.04 compile Nginx + RTMP + VOD + HLS

This tutorial will explain how to setup your own streaming server on ubuntu 18.04 with RTMP, HLS and VOD add on module.
Firtsly, we prepare nginx source, download from http://nginx.org/en/download.html

#wget http://nginx.org/download/nginx-1.14.1.tar.gz
#tar -zxvf nginx-1.14.1.tar.gz

Clone source nginx-rtmp-module and nginx-vod-module

#git clone https://github.com/sergey-dryabzhinsky/nginx-rtmp-module.git
#git clone https://github.com/kaltura/nginx-vod-module.git

Install nginx dependencies

#sudo apt-get install build-essential libpcre3 libpcre3-dev libssl-dev

Download zlib from http://zlib.net/

#wget http://zlib.net/zlib-1.2.11.tar.gz
#tar -zxxvf zlib-1.2.11.tar.gz

compile nginx

#cd nginx
#./configure --add-module=../nginx-rtmp-module/ --add-module=../nginx-vod-module/ --with-file-aio --with-threads --with-zlib=../zlib-1.2.11
#make
#make install

move to nginx installation folder and edit nginx.conf

#cd /usr/local/nginx/
#nano conf/nginx.conf

add rtmp server on nginx configuration

rtmp {
    server {
        listen 1935;
        chunk_size 4000;
        allow play all;
        application live {
            allow play all;
            live on;
            record all;
            record_path /film;
        }
        #allows you to play your recordings of your live streams using a URL like "rtmp://my-ip:1935/vod/filename.flv"
        application vod {
            play /film;
        }
        application hls {
            live on;
            # Turn on HLS
            hls on;
            hls_path /mnt/hls/;
            hls_fragment 3;
            hls_playlist_length 60;
            # record stream to folder /film/
            record all;
            record_path /film;
            # disable consuming the stream from nginx as rtmp
			deny play all;
        }
    }
}

Create folder /mnt/hls and /film
Give nginx permission to access that folder

add hls streaming config on htto section at nginx configuration

server {
    listen 8080;

    location /hls {
        # Disable cache
        add_header Cache-Control no-cache;

        # CORS setup
        add_header 'Access-Control-Allow-Origin' '*' always;
        add_header 'Access-Control-Expose-Headers' 'Content-Length';

        # allow CORS preflight requests
        if ($request_method = 'OPTIONS') {
            add_header 'Access-Control-Allow-Origin' '*';
            add_header 'Access-Control-Max-Age' 1728000;
            add_header 'Content-Type' 'text/plain charset=UTF-8';
            add_header 'Content-Length' 0;
            return 204;
        }

        types {
            application/vnd.apple.mpegurl m3u8;
            video/mp2t ts;
        }

        root /mnt/;
    }
}
}

add vod server on http section at nginx configuration

    server {
        listen 8384;

        # vod caches
        vod_metadata_cache metadata_cache 256m;
        vod_response_cache response_cache 128m;

        # vod settings
        vod_mode local;
        vod_segment_duration 2000; # 2s
        vod_align_segments_to_key_frames on;

        #file handle caching / aio
        open_file_cache max=1000 inactive=5m;
        open_file_cache_valid 2m;
        open_file_cache_min_uses 1;
        open_file_cache_errors on;
        aio on;

        location /filmku/ {
            alias /film/;
            vod hls;
            add_header Access-Control-Allow-Headers '*';
            add_header Access-Control-Expose-Headers 'Server,r$
            add_header Access-Control-Allow-Methods 'GET, HEAD$
            add_header Access-Control-Allow-Origin '*';
            expires 100d;
        }
    }

check configuration and run nginx

# /usr/local/nginx/sbin/nginx -
# /usr/local/nginx/sbin/nginx

If you stream video using OBS to rtmp://your_ip/hls/streamname
so you can access your streaming via hls streaming http://your_ip:8080/hls/streamname.m3u8
or with rtmp vod rtmp://yourip/vod/streamname.flv
of with hls vod http://your_ip:8384/filmku/streamname.flv/index.m3u8

RTMP server + HLS Ubuntu 18.04.1 LTS

Install Nginx

sudo apt install libnginx-mod-rtmp

Install rtmp-module


sudo apt install libnginx-mod-rtmp
service nginx restart

Setting nginx conf
nano /etc/nginx/nginx.conf


rtmp {
    server {
        listen 1935;
        chunk_size 4000;
        allow play all;
        application live {
            allow play all;
            live on;
        }
        application hls {
            live on;
            # Turn on HLS
            hls on;
            hls_path /mnt/hls/;
            hls_fragment 3;
            hls_playlist_length 60;
            # disable consuming the stream from nginx as rtmp
            deny play all;
        }
    }
}

Create new nginx config file
nano /etc/nginx/conf.d/hls.conf


server {
    listen 8080;

    location /hls {
        # Disable cache
        add_header Cache-Control no-cache;

        # CORS setup
        add_header 'Access-Control-Allow-Origin' '*' always;
        add_header 'Access-Control-Expose-Headers' 'Content-Length';

        # allow CORS preflight requests
        if ($request_method = 'OPTIONS') {
            add_header 'Access-Control-Allow-Origin' '*';
            add_header 'Access-Control-Max-Age' 1728000;
            add_header 'Content-Type' 'text/plain charset=UTF-8';
            add_header 'Content-Length' 0;
            return 204;
        }

        types {
            application/vnd.apple.mpegurl m3u8;
            video/mp2t ts;
        }

        root /mnt;
        add_header Cache-Control no-cache;
    }
}

Restart Nginx…Done

Basic uri syntax: rtmp://your_ip/hls/stream_name
video will be available as http://your_ip:8080/hls/stream_name.m3u8

Raspberry – Compile Nginx with RTMP Module

Install utils and libraries needed for compilation and some extra Nginx modules

sudo apt-get install -y curl build-essential make gcc libpcre3 libpcre3-dev libpcre++-dev zlib1g-dev libbz2-dev libxslt1-dev libxml2-dev libgd2-xpm-dev libgeoip-dev libgoogle-perftools-dev libperl-dev libssl-dev libcurl4-openssl-dev

Download NGINX Source and RTMP Module


cd /tmp
wget https://github.com/arut/nginx-rtmp-module/archive/master.zip
wget http://nginx.org/download/nginx-1.7.9.tar.gz
tar -zxvf nginx-1.7.9.tar.gz
unzip master.zip

cd nginx-1.7.9
./configure --add-module=/tmp/nginx-rtmp-module-master
make
sudo make install

Mikrotik script get Public IP

/tool fetch url="http://ifconfig.co/ip" mode=http dst-path=mypublicip.txt
:local ipku [file get mypublicip.txt contents]
:global ipnow "";
:global ipold;
:for i from=0 to=([:len $ipku]-1) do={ :local tmp [:pick $ipku $i];
:if ($tmp !="\n") do={ :set ipnow "$ipnow$tmp" }
}

:if ($ipnow != $ipold) do={ 
:log info "ip diupdate"; 
:set ipold "$ipnow";
/tool fetch url="http://xxx.xxx/update.php\3Fip=$ipnow" keep-result=no;
} else={ :log info "Tidak diupdate"}

:log info $ipnow