Nginx video live / on demand service dry goods sharing

Time:2020-2-26

In recent years, the internet live broadcasting business is very hot. I also studied it and found that the configuration of live video on demand on nginx is also very easy to implement, so I’d like to share it.
1、 Ubuntu 14.04 install nginx and nginx? RTMP? Module extension
Nginx can be installed and extended in three ways, depending on whether it is installed or not.
1. Newly install nginx and nginx? RTMP? Module extensions

#!/bin/sh
apt-get update
apt-get install -y gcc libpcre3 libpcre3-dev openssl libssl-dev make git libxml2 libxml2-dev libxslt-dev libgd2-xpm-dev geoip-database libgeoip-dev
cd /usr/local/src/
git clone https://github.com/arut/nginx-rtmp-module.git
wget -c  http://mirrors.sohu.com/nginx/nginx-1.9.14.tar.gz
tar zxvf nginx-1.9.14.tar.gz
cd nginx-1.9.14/
./configure --with-http_ssl_module --add-module=/usr/local/src/nginx-rtmp-module
make
make install
cd /usr/local/nginx/
ln -s `pwd`/sbin/nginx /usr/sbin/nginx
 
nginx -V
 
wget http://www.*****.com/editor/attached/file/20160322/20160322112243_43972.txt
mv 20160322112243_43972.txt /etc/init.d/nginx
chmod +x /etc/init.d/nginx
service nginx restart

2. If you have installed nginx in APT get, you need to recompile and install it and add the nginx? RTMP? Module extension

#!/bin/sh
apt-get -y install dpkg-dev libxml2 libxml2-dev libxslt-dev libgd2-xpm-dev geoip-database libgeoip-dev libpcre3 libpcre3-dev libssl-dev  openssl 
 
nginx -V 2>a 
nginx_config=`cat a |grep configure | cut -d ':' -f 2`
nginx_version=`cat a|grep 'nginx version'|cut -d '/' -f 2 | cut -d ' ' -f 1`
rm -f a
 
cd /usr/local/src/
 
# apt-get install -y git
git clone https://github.com/arut/nginx-rtmp-module.git
 
#Download the corresponding nginx version with Sohu open source image
# apt-get source nginx
wget -c "http://mirrors.sohu.com/nginx/nginx-${nginx_version}.tar.gz"
tar zxvf "nginx-${nginx_version}.tar.gz"
cd "nginx-$nginx_version"
echo "./configure ${nginx_config} --add-module=/usr/local/src/nginx-rtmp-module" | sh
make
 
#Force override
cp -rfp objs/nginx /usr/sbin/nginx
#Check for errors
/usr/sbin/nginx -t
Nginx restart
service nginx restart
nginx -V

3. If nginx has been compiled and installed, you can add nginx? RTMP? Module extension directly
Find the source root directory where nginx is installed. If not, download the new source code
My installation source root directory / usr / local / SRC / nginx-1.4.6

cd /usr/local/src/
# apt-get install -y git
git clone https://github.com/arut/nginx-rtmp-module.git
 
nginx -V 2>a 
nginx_config=`cat a |grep configure | cut -d ':' -f 2`
rm -f a
 
cd nginx-1.4.6
echo "./configure ${nginx_config} --add-module=/usr/local/src/nginx-rtmp-module" | sh
make
 
#Force override
cp -rfp objs/nginx /usr/sbin/nginx
#Check for errors
/usr/sbin/nginx -t
Nginx restart
service nginx restart
nginx -V

2、 Configure use
Edit the configuration file of nginx (/ etc / nginx / nginx.conf or / usr / local / nginx / conf / nginx. CONF)
1. Add an RTMP node to the outer layer of the HTTP node, as follows:

rtmp {
    server {
        listen 1935;
        chunk_size 4096;
        max_connections 100;
 
        #Audio and video stream upload and playback address are RTMP: // your IP / Live / streamname 
        #Streamname itself is custom.
        application live {
            live on;
            record off;
        }
 
        #This is the upload address RTMP: // your IP: / HLS / streamname2
        #This is also the address of the live broadcast. The on-demand broadcast address is below
        application hls {
            live on;
            hls on;
            #Please create the corresponding directory first (MKDIR - P / var / www / HLS / & & chown - R www data: www data / var / www)
            hls_path /var/www/hls/;
        }
    }
}

Explain:
Application represents an application
The streamname after the address can be defined by itself, and files similar to streamname.m3u8 and streamname.ts will be generated in the corresponding directory on demand.
Live on; means to turn on the live broadcast.
HLS on; indicates that when on-demand is turned on, temporary files will be generated on the server.
Record off; is to turn off the function of saving video.
The on-demand broadcast address is the data stream address of an HTTP protocol. For example, http: / / your IP: 8080 / HLS / streamname2.m3u8 configured below.
By default, on-demand files are generated one by one and deleted one by one automatically. The m3u8 file is dynamic. You can view it with vim.
2. Add a server node in the following HTTP node, as follows:

#This is the on-demand broadcast address http: // your IP: 8080 / HLS / streamname2.m3u8
server {
        listen      8080;
        index index.html;
        root  /var/www/hls/;
 
        location /hls {
                alias /var/www/hls/;
                types {
                        application/vnd.apple.mpegurl m3u8;
                        video/mp2t ts;
                }
                add_header Cache-Control no-cache;
        }
}

Explain:
This node is a virtual host configured to play the video stream generated by the on-demand service above.
Streamname2 in the URL is based on the name of the recorded push stream. Consistency is needed.
By default, the address has a certain time limit line. You need to confirm whether the m3u8 file exists on the server every time.
3. Test use

a. Push video stream to server
    (1) Using ffmpeg on Linux to simulate streaming to the server
    Install ffmpeg tools
apt-get -y install build-essential git-core checkinstall yasm texi2html libvorbis-dev libx11-dev libvpx-dev libxfixes-dev zlib1g-dev pkg-config netcat libncurses5-dev libfaac-dev  libmp3lame-dev libx264-dev
 
FFMPEG_VERSION=2.3.3
 
cd /usr/local/src
if [ ! -d "/usr/local/src/ffmpeg-${FFMPEG_VERSION}" ]; then
  sudo wget "http://ffmpeg.org/releases/ffmpeg-${FFMPEG_VERSION}.tar.bz2"
  sudo tar -xjf "ffmpeg-${FFMPEG_VERSION}.tar.bz2"
fi
 
cd "ffmpeg-${FFMPEG_VERSION}"
./configure --enable-version3 --enable-postproc --enable-libvorbis --enable-libvpx --enable-gpl --enable-nonfree --enable-pthreads --enable-libfaac --enable-libmp3lame --enable-libx264
make
checkinstall --pkgname=ffmpeg --pkgversion="5:${FFMPEG_VERSION}" --backup=no --deldoc=yes --default

ffmpeg –version
Using ffmpeg tool to push the local video file simulation to the server

Ffmpeg - re - I / data / localfile.mp4 - C copy - f flv RTMP: // your IP / Live / streamname

This command will push the local localfile.mp4 simulation into live streaming, and then you can watch the live streaming through RTMP: / / your IP / Live / streamname address.

ffmpeg -re -i /data/localFile.mp4 -c copy -f flv /var/www/hls/streamName

This command will push the local localfile.mp4 simulation into the HLS application, and then you can watch the live broadcast through RTMP: / / your IP / HLS / streamname address, or you can open http: / / your IP: 8080 / HLS / streamname.m3u8 through the player to view the video on demand

(2) . use some professional tools to push the stream to the server on Windows

I use demo in the sharp motion PC recording / live SDK (http://www.rdsdk.com/contrast… Non advertising] package to push the stream. This can be used free of charge and installation.
The common streaming testing tools recommended on the Internet are OBS, XSplit, FMLE, etc.
The streaming address is RTMP: / / your IP / HLS / streamname or RTMP: / / your IP / Live / streamname

(3) . write the app of the mobile phone and other devices, record the head image captured by the camera and the voice captured by the microphone, and push them to the server together.

There are also many SDKs for this kind of Android or IOS. It can be developed by itself.

b. Play video on demand with player

Both windows and Linux Desktop versions can be VLC players. Open http://your IP address: 8080 / HLS / streamname.m3u8 to watch the live broadcast (this player does not support RTMP live broadcast?)

c. Using web browser to play live video on Web page

Write HTML pageindex.html:

<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="content-type" content="text/html; charset=utf-8">
        <title>HLS Player</title>
    </head>
    <body>
    <video poster="poster.png" height="720" width="1280" controls>
        <source type="application/vnd.apple.mpegurl" />
        <p class="warning">Your browser does not support HTML5 video.</p>
    </video>
    </body>
</html>

Modify the IP and save the page to / var / www / HLS / index.html.
Visit http://your IP: 8080/index.html on Apple device to see the live broadcast.
Due to H5 compatibility issues, it can only be accessed by browser on Apple devices.
If you want other devices to be accessible, you need to do compatibility processing. Generally, flash is embedded in the web page to play live (RTMP: / /…) or on-demand (http: / /… XXX. M3u8)

d. Using nginx? RTMP? Module to extend the in package files to record and live test at the same time

There are test files in the nginx? RTMP? Module extension package. Copy the files in the folder to the site root directory (such as / var / www / HTML)
cp -r /usr/local/src/nginx-rtmp-module/test/www/* /var/www/html;
Modify the address of RTMP in the index.html file to RTMP: / / your IP / Live / streamname
Change the streamer of flashvars in the record.html file to RTMP: / / your IP / live and change the file value of flashvars to streamname
Preservation.

Use the browser to open two pages at the same time:

Http: // your IP: 8080 / index.html (this is the address for live viewing)
Http: // your IP: 8080 / record.html (this is the address of live video recording, using the camera of flash recording computer [please click allow])

Three, others
1. For the configuration of nginx RTMP module, see wiki https://github.com/arut/nginx
2. The above environment is only for testing, learning and use. Authority security, network bandwidth and video quality must be considered in public network.
3. There are many such products on the network, for example, alicloud basic services have the function of live video on demand.

The copyright belongs to the author. For commercial reprint, please contact the author for authorization. For non-commercial reprint, please indicate the source. Internet plus era, always keep learning, join hands thousand PHP, Dream It Possible.