Skip to content

Adaptive Bitrates on Livestream

Fiero edited this page Dec 10, 2019 · 9 revisions

For converting live streams into several streams resolution for an adaptive streaming purpose, we need to make sure the server has enough CPU for the workload. This is to prevent live streaming from continuous delays and even worst, the server becomes unresponsive and hang.

On this sample, I will show how to transcode the live stream into 3 different resolutions

Open your NGINX configuration file

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

Edit the rtmp section inside your nginx.conf, it will looks like this

    rtmp {
        server {
                listen 1935;
                chunk_size 4000;
                application live {
                        live on;
                        on_publish http://localhost/avideo/plugin/Live/on_publish.php;
                        on_play http://localhost/avideo/plugin/Live/on_play.php;
                        on_record_done http://localhost/avideo/plugin/Live/on_record_done.php;
                        exec ffmpeg -i rtmp://localhost/live/$name 
                            -c:a aac -strict -2 -b:a 128k -c:v libx264 -vf scale=-2:240 -g 48 -keyint_min 48 -sc_threshold 0 -bf 3 -b_strategy 2 -b:v 400k -maxrate 700k -bufsize 1200k -b:a 96k -r 20 -f hls -hls_time 6 -hls_list_size 0 -f flv rtmp://localhost/adaptive/$name_low  
                            -c:a aac -strict -2 -b:a 128k -c:v libx264 -vf scale=-2:480 -g 48 -keyint_min 48 -sc_threshold 0 -bf 3 -b_strategy 2 -b:v 1200k -maxrate 2100k -bufsize 2400k -b:a 128k -f hls -hls_time 6 -hls_list_size 0 -f flv rtmp://localhost/adaptive/$name_mid  
                            -c:a aac -strict -2 -b:a 128k -c:v libx264 -vf scale=-2:720 -g 48 -keyint_min 48 -sc_threshold 0 -bf 3 -b_strategy 2 -b:v 2400k -maxrate 4200k -bufsize 100000k -b:a 128k -f hls -hls_time 6 -hls_list_size 0 -f flv rtmp://localhost/adaptive/$name_hi;
                }

                application adaptive {
                    live on; 
                    hls on; 

                    hls_path /HLS/live;
                    hls_nested on;

                    allow play all;
                    allow publish 127.0.0.1;
                    deny publish all;

                    hls_variant _low BANDWIDTH=400000;
                    hls_variant _mid BANDWIDTH=2100000;
                    hls_variant _hi  BANDWIDTH=4200000;
                }
        }
    }

This code is optimized for AVideo Live, after restarting your NGINX with

sudo /usr/local/nginx/sbin/nginx -s stop && sudo /usr/local/nginx/sbin/nginx

you must check the Adaptive option on your Live plugin

Clone this wiki locally