February 26, 2021
nginx-video-streaming

Streaming Live Video and Storing Videos with NGINX Server

In this tutorial, I will be guiding how you can stream a live and stored video from your NGINX server. We will be going to install all these packages in Ubuntu 20.04 Server for these installations. In this solution, I will be taking advantage of the Real‑Time Messaging Protocol (RTMP) module for NGINX.

Before compiling the NGINX, you need to install some basic tools installed on your Ubuntu server like autoconfgccgit, and make. To download and install these packages you can run the command as follows:-

sudo apt update
sudo apt-get install build-essential

Installing Dependencies

For installing NGINX you will need to install several dependencies like: Perl Compatible Regular Expressions (PCRE), OpenSSL, and Zlib for compression.

The easiest way to install these dependencies is with a package manager, run the following command to install the dependencies:

 sudo apt install libpcre3-dev libssl-dev zlib1g-dev

Compiling NGINX with the RTMP Module

In order to install these packages, you need to clone the GitHub repositories for RTMP and NGINX, run the NGINX configure command, and then compile NGINX.

 cd /path/to/build/dir
 git clone https://github.com/arut/nginx-rtmp-module.git
 git clone https://github.com/nginx/nginx.git
 cd nginx
 ./auto/configure --add-module=../nginx-rtmp-module
 make
 sudo make install

Configuring NGINX

You can configure NGINX to stream video using one or both of the HTTP Live Streaming (HLS) and Dynamic Adaptive Streaming over HTTP (DASH) protocols. The configuration is as follows:

If you compiled the NGINX from the source then the by default NGINX directory is /usr/local/nginx

rtmp{
	server{
	listen 1935;
	application live{
	live on;
	interleave on;
	hls on;
	hls_path /tmp/hls;
	hls_fragment 15s;
	dash on;
	dash_path /tmp/dash;
	dash_fragment 15s;
	
	}
	}
}


http{
	default_type application/octet-stream;
	server{
	listen 80;
	location / {
		root /tmp;
	}
	type{
		application/vnd.apple.mpegurl m3u8;
		video/mp2t ts;
		text/html html;
		application/das+xml mpd;
	}
	}
}

Checking the NGINX Configuration before restarting

It is always a good practice to validate your NGINX configuration to make sure there are no syntactic errors.

/usr/local/nginx/sbin/nginx -t
nginx: the configuration file filename syntax is ok
nginx: configuration file filename test is successful

Run the following command to restart your NGINX Server

/usr/local/nginx/sbin/nginx -s restart

Installing ffmpeg

FFmpeg is a free and open-source software project consisting of a large suite of libraries and programs for handling video, audio, and other multimedia files and streams. At its core is the FFmpeg program itself, designed for command-line-based processing of video and audio files. Wikipedia

In order to install the ffmpeg just run the below command as follows:

sudo apt-get install ffmpeg

You have secessfully installed ffmpeg on you ubuntu server.

Testing the playback method

In order to test the environment, just upload a video in any directory you wish and the following command as follows:-

ffmpeg -re -i Butterfly_1080p.mp4 -vcodec copy -loop -1 -c:a aac -b:a 160k -ar 44100 -strict -2 -f flv rtmp:192.168.1.138/live/vedant

Now the video will start streaming. You can the check this on you VLC media player on your pc, Just copy the link on your network stream.

In the URLs, NGINX_server is the IP address or hostname of his NGINX server:

  • RTMP – rtmp://NGINX_server/live/vedant
  • HLS – http://NGINX_server/live/vedant.m3u8
  • DASH – http://NGINX_server/live/vedant.mpd

Hope you have enjoyed the tutorial. If you have any question feel free to comment.

Vedant Singh

Linux System Administrator | Entrepreneur | Founder Techmion. I have started my career as a Linux system Administrator after that quit my job to start something new that excites me and started a Tech Company in order to provide the best technology to my clients. My Company website link: https://www.techmion.com/ For Bulk, SMS-Marketing follows the link : https://www.pingsms.in/

View all posts by Vedant Singh →

Leave a Reply

Your email address will not be published. Required fields are marked *