Skip to content

Adaptive Bitrates on Livestream

YouPHPTube edited this page Mar 9, 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; # Listen on standard RTMP port
            chunk_size 4000; 

            # This application is to accept incoming stream
            application live {
                live on; 

                on_publish http://localhost/YouPHPTube/plugin/Live/on_publish.php;
                on_play http://localhost/YouPHPTube/plugin/Live/on_play.php;
                on_record_done http://localhost/YouPHPTube/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:360 -g 48 -keyint_min 48  -sc_threshold 0 -bf 3 -b_strategy 2 -b:v 800k -maxrate 856k -bufsize 1200k -b:a 96k -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:540 -g 48 -keyint_min 48 -sc_threshold 0 -bf 3 -b_strategy 2 -b:v 1400k -maxrate 1498k -bufsize 2100k -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 2800k -maxrate 2996k -bufsize 4200k -b:a 128k -f hls -hls_time 6 -hls_list_size 0 -f flv rtmp://localhost/adaptive/$name_hi;
            }

            # This application is for splitting the stream into HLS fragments
            application adaptive {
                live on; 
                hls on; 

                # Pointing this to an SSD is better as this involves lots of IO
                hls_path /HLS/live;
                hls_nested on;

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

                hls_variant _low BANDWIDTH=800000;
                hls_variant _mid BANDWIDTH=1400000;
                hls_variant _hi  BANDWIDTH=2800000;
            }
        }
}

Clone this wiki locally