RTMP is great for serving live content. When RTMP is paired with FFmpeg, streams can be converted into various qualities. Let's get started!
Installing Nginx and Nginx-RTMP
Install the tools required to compile Nginx and Nginx-RTMP from source.
sudo yum install pcre pcre-devel openssl openssl-devel zlib zlib-devel -y
Make a working directory and switch to it.
mkdir ~/working
cd ~/working
Download the Nginx and Nginx-RTMP sources.
wget http://nginx.org/download/nginx-1.9.7.tar.gz
wget https://github.com/arut/nginx-rtmp-module/archive/master.zip
Install the unzip package.
sudo yum install unzip
Extract the Nginx and Nginx-RTMP source.
tar -xvf nginx-1.9.7.tar.gz
unzip v1.2.1.zip
Switch to the Nginx directory.
cd nginx-1.9.7
Add modules to compile into Nginx. Nginx-RTMP is included.
./configure --add-module=../nginx-rtmp-module-1.2.1/
Compile and install Nginx with Nginx-RTMP.
make
sudo make install
Create a SystemD unit file for Nginx.
sudo nano /usr/lib/systemd/system/nginx.service
Populate the file with the following content.
[Unit]
Description=nginx - high performance web server
Documentation=https://nginx.org/en/docs/
After=network-online.target remote-fs.target nss-lookup.target
Wants=network-online.target
[Service]
Type=forking
PIDFile=/var/run/nginx.pid
ExecStartPre=/usr/sbin/nginx -t -c /etc/nginx/nginx.conf
ExecStart=/usr/sbin/nginx -c /etc/nginx/nginx.conf
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s TERM $MAINPID
[Install]
WantedBy=multi-user.target
Start and enable the Nginx service.
sudo systemctl start nginx.service
Tell SystemD to start Nginx services automatically at boot.
systemctl enable nginx.service
Installing FFmpeg
Install epel-release
and update the system.
sudo yum install epel-release -y
sudo yum update -y
sudo shutdown -r now
Install the Nux repository.
sudo rpm --import http://li.nux.ro/download/nux/RPM-GPG-KEY-nux.ro
sudo rpm -Uvh http://li.nux.ro/download/nux/dextop/el7/x86_64/nux-dextop-release-0-5.el7.nux.noarch.rpm
Install FFmpeg and its development packages.
sudo yum install ffmpeg ffmpeg-devel -y
Configuring Nginx-RTMP and FFmpeg
Open the Nginx configuration file.
sudo nano /usr/local/nginx/conf/nginx.conf
Append the following to the configuration.
rtmp {
server {
listen 1935;
chunk_size 4096;
application live {
live on;
record off;
exec ffmpeg -i rtmp://localhost/live/$name -threads 1 -c:v libx264 -profile:v baseline -b:v 350K -s 640x360 -f flv -c:a aac -ac 1 -strict -2 -b:a 56k rtmp://localhost/live360p/$name;
}
application live360p {
live on;
record off;
}
}
}
After you've added that configuration text, you can customize settings such a video bitrate, audio bitrate, and resolution. These changes will only be applied to the lower quality stream. To add more qualities, copy and paste the exec FFmpeg line and change the settings. You'll also need to create a new application. You can do this by copying and pasting the live360 example that has been included. Don't forget to update the exec FFmpeg line with the address of the new application. You can do this by changing the final RTMP address in the exec FFmpeg line.
Note: Changing the value after -b:v
will change the video bitrate. This is measured in kilobits per second. Changing the value after -b:a
will change the audio bitrate. This is measured in kilobits per second. Changing the value after -s
will change the resolution.
Save the file by pressing CTRL + X.
Restart Nginx.
sudo service nginx restart
Note: For best performance, each stream being converted should have its own CPU core. For example, if two qualities, 360P and 480P, are being created from a 720P stream, a instance with at least two CPU cores should be used.
Security Note
If you're using a firewall, you'll need to make sure TCP 1935
is allowed.
The current configuration allows anyone to stream to your server. We can fix this by only giving certain IP addresses the publish permission. Open the Nginx configuration.
sudo nano /usr/local/nginx/conf/nginx.conf
Look for the following lines.
live on;
record off;
Add the following to each set of the above lines. Change 0.0.0.0
to your actual IP address.
allow publish 127.0.0.1;
allow publish 0.0.0.0;
deny publish all;
The configuration will now look something like the following.
rtmp {
server {
listen 1935;
chunk_size 4096;
application live {
live on;
record off;
allow publish 127.0.0.1;
allow publish 0.0.0.0;
deny publish all;
exec ffmpeg -i rtmp://localhost/live/$name -threads 1 -c:v libx264 -profile:v baseline -b:v 350K -s 640x360 -f flv -c:a aac -ac 1 -strict -2 -b:a 56k rtmp://localhost/live360p/$name;
}
application live360p {
live on;
record off;
allow publish 127.0.0.1;
allow publish 0.0.0.0;
deny publish all;
}
}
}
Save the file by pressing CTRL + X.
Restart Nginx.
sudo service nginx restart
Configuring Software to Work with Nginx-RTMP
Streaming applications typically have two fields for connection information. The first field is usually for the server information and the second field is usually for the stream name or key. The information that you should place into each field is listed. The stream name or key can be set to anything.
Field 1: rtmp://your.ip/live/
Field 2: stream-key-you-set
To view the streams, open the following links in a player supporting RTMP.
rtmp://your.ip/live/stream-key-you-set
rtmp://your.ip/live360p/stream-key-you-set