Dry Goods Sharing of nginx Video Live/On Demand Service

In recent years, the Internet live broadcasting business is very hot. I also studied the next, found that the nginx configuration of live video on demand is also easy to achieve, I would like to share it.
1. Install nginx and nginx_rtmp_module extensions in ubuntu14.04
There are three ways to install and extend nginx, depending on whether it has been installed or not.
1. New installation of 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 apt-get has installed nginx, it is necessary to recompile the installation 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 using Sohu Open Source Mirror
# 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
 
#Compulsory coverage
cp -rfp objs/nginx /usr/sbin/nginx
#Check for errors
/usr/sbin/nginx -t
#Restart nginx
service nginx restart
nginx -V

3. If you have compiled and installed nginx, you can add the nginx_rtmp_module extension directly.
Find the source directory where nginx is installed, and download the new source if not
My installation source 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
 
#Compulsory coverage
cp -rfp objs/nginx /usr/sbin/nginx
#Check for errors
/usr/sbin/nginx -t
#Restart nginx
service nginx restart
nginx -V

II. Configuration and Use
Edit the configuration file for nginx (/etc/nginx/nginx.conf or/usr/local/nginx/conf/nginx.conf)
1. To add a rtmp node to the outer layer of http node, the details are as follows:

rtmp {
    server {
        listen 1935;
        chunk_size 4096;
        max_connections 100;
 
        #Both the upload and play addresses of audio and video streams 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 for live broadcasting. The address for VOD broadcasting is below.
        application hls {
            live on;
            hls on;
            #Please create the corresponding directory (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. When on demand, files like streamName.m3u8 and streamName.ts will be generated in the corresponding directory.
live on; means open live.
hls on; indicates that when VOD is opened, temporary files will be generated on the server.
record off; closes the function of saving video.
The address of VOD is a data stream address of http protocol. For example, the following configuration http:// Your IP:8080/hls/streamName2.m3u8.
By default, on-demand files are generated one by one and automatically deleted one by one. The m3u8 file is dynamic. You can view it with vim.
2. Add a server node inside the following http node. The details are as follows:

#This is the VOD playback 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 above VOD service.
StreaName2 in url is based on the name of the recorded push stream. There needs to be consistency.
By default, the address has a certain time-limit line, you need to confirm every time whether the m3u8 file exists on the server.
3. Test Use

a.Push video stream to server
    (1).Use linux upper ffmpeg Tools simulate push-to-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 local video file simulation to server

ffmpeg -re -i /data/localFile.mp4 -c copy -f flv rtmp://Your IP/live/streamName

This command pushes the local localFile.mp4 simulation into live live streaming, which can then be viewed 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 local File. MP4 emulation into the HLS application, and then watch the live broadcast through rtmp://your IP/hls/streamName address, or through the player.http:// Your IP:8080/hls/streamName.m3u8 to view the video on demand

    (2) Using some professional tools to push to the server on Windows

I use Sharp PC to record/broadcast SDK live.( http://www.rdsdk.com/contrast... Non-advertising] demo in the package to push the flow, this can be used free of charge and installation-free.
The common push-flow testing tools recommended on the Internet are OBS, XSplit, FMLE, etc.
The address of the push stream is rtmp://your IP/hls/streamName or rtmp://your IP/live/streamName.

    (3) Write the APP of mobile phone and other devices, record the head image captured by mobile camera and the sound captured by microphone, and push it to the server together.

There are many SDK s for Android or IOS. It can be developed by itself.

b. Playing Video on Demand with Player

Windows and linux Desktop versions can be turned on by VLC players http:// Your IP:8080/hls/streamName.m3u8 is the address to watch live broadcasting (does this player not support rtmp live broadcasting?)

c. Playing live videos on Web pages using Web browsers

Write HTML page index.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 src="http://Your IP/hls/streamName.m3u8 "type=" application/vnd.apple.mpegurl "/>
        <p class="warning">Your browser does not support HTML5 video.</p>
    </video>
    </body>
</html>

Modify IP and save the page to / var/www/hls/index.html.
Access with Apple Deviceshttp:// Your IP:8080/index.html can see the live broadcast.
Because of the H5 compatibility problem, it can only be accessed by browser on Apple devices.
Compatibility processing is required if other devices are to be accessible. In general, 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 file in the package to record and broadcast simultaneously

There are test files in the nginx_rtmp_module extension package. Copy the files in the folder to the site root directory (e.g. / 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
Modify the streamer of flash vars in record.html file to rtmp://your IP/live and change the file value of flash vars to streamName
Preservation.

Open two pages at the same time using the browser:

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, using the camera of flash recording computer [click allow])

III. OTHER
1. For configuration of nginx-rtmp-module, see Wiki https://github.com/arut/nginx...
2. The above environment is only for testing and learning. Privilege security, network bandwidth, video quality and so on must be considered in public networks.
3. There are many such products on the network, such as Aliyun Basic Service, which has the function of live video on demand.

Copyright belongs to the author. For commercial reprints, please contact the author for authorization. For non-commercial reprints, please indicate the source. Internet + era, always keep learning, hand in hand PHP,Dream It Possible.

Keywords: PHP Nginx git github OpenSSL

Added by paegn on Mon, 08 Jul 2019 04:14:05 +0300