1. The background

Unconsciously I arrived streaming media sector has been more than a year and a half, accumulated a lot of knowledge of streaming media, but work is also busy at ordinary times, rarely to summary, ready to spend a few recent weekend time to write a series of streaming media practice, is to give oneself do summary to help developers in need at the same time;

In this section, we mainly build a vod service through the nginx+ RTMP module, that is, you have a lot of video files, you can refer to this article, and the author’s previous article using PHP and Ffmpeg to quickly build streaming media service practice, the application scenario is basically the same. However, there is no code involved in this article and it is relatively simple.

In this series of streaming media articles, the practice is the main. The author runs each command in the article and provides relevant screenshots, and explains the meaning of parameters, hoping to help readers better.

2. Operating environment

In order to deploy this service to the production environment in the later stage, the author adopts docker method to build this service. In addition, we usually use Linux system as the server in the production environment, so I choose Ubuntu system. The building process of other systems is basically similar.

2.1 the docker container

The default port for the RTMP service is 1935. In addition, after installing nginx, you need to open an HTTP port. In order to prevent conflicts with the host, port 8081 is used. We need to copy the video file to the container, so we also need to mount a directory, so the Docker container runs the command shown below

docker run --name video -v /Users/tangqingsong/files:/root/videos -d-i-p 8081:8081 -p 1935:1935 Ubuntu: 18.04&& Docker PSCopy the code

After the command is executed, docker returns the result shown below

docker exec -it video bash
Copy the code

After the command is executed, the following information is displayed

2.2 Domestic accelerators

Docker Ubuntu image APT software source uses the official domain name by default, which is very slow to access in China. In order to speed up subsequent installation, we change the apt software source to the address of Aliyunyuan, and execute the command as follows

echo 'deb http://mirrors.aliyun.com/ubuntu/ bionic main restricted universe multiverse deb http://mirrors.aliyun.com/ubuntu/ bionic-security main restricted universe multiverse deb http://mirrors.aliyun.com/ubuntu/ bionic-updates main restricted  universe multiverse deb http://mirrors.aliyun.com/ubuntu/ bionic-proposed main restricted universe multiverse deb http://mirrors.aliyun.com/ubuntu/ bionic-backports main restricted universe multiverse deb-src http://mirrors.aliyun.com/ubuntu/ bionic main restricted universe multiverse deb-src http://mirrors.aliyun.com/ubuntu/ bionic-security main restricted universe multiverse deb-src http://mirrors.aliyun.com/ubuntu/ bionic-updates main restricted universe multiverse deb-src http://mirrors.aliyun.com/ubuntu/ bionic-proposed main restricted universe multiverse deb-src http://mirrors.aliyun.com/ubuntu/ bionic-backports main restricted universe multiverse ' > /etc/apt/sources.list  && cat /etc/apt/sources.list
Copy the code

After the command is executed, the following information is displayed

2.3 Updating the Software Source List

Next we update the local software source information by executing the command shown below

apt update
Copy the code

After the command is executed, the following information is displayed

3 Nginx installation

After we get the system environment ready, we need to start installing Nginx. To install Nginx, we need to install its dependencies first, then download the relevant source code for compilation, and finally execute the compilation installation

3.1 Installing Nginx dependencies

After we update apt software source, we can install Nginx and other software dependent environment, execute the command as follows

apt-get install -y libpcre3 libpcre3-dev libssl-dev zlib1g-dev gcc  wget unzip vim make curl
Copy the code

There are a lot of dependent software installed, and the installation speed varies according to your network speed. After the command is executed, the information returned is as follows

3.2 Downloading Source Code

Nginx-rtmp-module, nginx-http-flV-module, nginx-rtmp-module, nginx-http-flV-module, nginx-rtmp-module First download it and unzip it, using the command shown below

wget https://github.com/winshining/nginx-http-flv-module/archive/master.zip ; unzip master.zip
Copy the code

After the command is executed, the following information is displayed

Wget http://nginx.org/download/nginx-1.17.6.tar.gz && tar - ZXVF nginx - 1.17.6. Tar. GzCopy the code

After the command is executed, the following information is displayed

3.3 Compilation and Installation

Then we go to compile and install, first go to the nginx directory, execute the command as follows

cdNginx - 1.17.6 && lsCopy the code

After the command is executed, the following information is displayed

./configure
nginx-http-flv-module

./configure --add-module=.. /nginx-http-flv-module-masterCopy the code

After the command is executed, the following information is displayed

vim objs/Makefile
Copy the code

There is an objs/Makefile in the current folder. We remove -werror from it and open the file with vim as shown below

After the command is executed, the following information is displayed

Once deleted, save and exit, Nginx is ready to compile, which takes a bit longer, using the following command

make 
Copy the code

After the command is executed, the following information is displayed

make install
Copy the code

After the installation command is executed, the following information is displayed

Configure the RTMP service

After the Nginx installation is complete, we need to configure Nginx and start the Nginx service

4.1 Adding the RTMP Service

We directly use the vim command to edit the Nginx configuration file, as shown below

vim /usr/local/nginx/conf/nginx.conf
Copy the code

After the vim command is executed, the following edit window opens

rtmp {                # RTMP service
   server {
       listen 1935;  #// Service port
        chunk_size 4096;   #// Data transfer block size


        application vod {
                play /opt/video/vod; #// Video file location.}}}Copy the code

4.2 Verifying the Configuration

After pasting and saving, run the nginx -t command on the terminal to test whether the configuration file is abnormal, as shown below

/usr/local/nginx/sbin/nginx -t
Copy the code

After the command is executed, the following information is displayed

/usr/local/ nginx/sbin/nginx && curl http://127.0.0.1Copy the code

After the command is executed, the following information is displayed

5 Video Playback

After all the above steps are completed, everything is basically normal. Now we can start to play the video, but we still need to put a video file under the video directory to play the video

5.1 Adding video Files

Then we create a folder to store the video, and set the permission setting to 777, in case the video cannot be played due to the permission problem, execute the command as follows

mkdir -p /opt/video/vod  && chmod -R 777 /opt/video/vod
Copy the code

After the command is executed, the following information is displayed

cp /root/videos/out.mp4 /opt/video/vod &&  ls /opt/video/vod
Copy the code

After the command is executed, the following information is displayed

out.mp4
rtmp

5.2 Installing the VLC Player

Generally used for debugging streaming media, we are used to using VLC player, let’s go to the official website to download it, the official website address is as follows

https://www.videolan.org/
Copy the code

After using the browser to open the official website, we can see the download button on the home page. It can also be seen that various platforms are supported, and the current system version is downloaded by default, as shown in the figure below

After downloading and installing the VLC player, we need to go to File->Open Network in the menu, as shown below

After clicking the Open Network option, a new window will Open. In this window, we can enter our playing address. The video playing address I copied in the past is shown below

rtmp://localhost/vod/out.mp4
Copy the code

In the figure above, you can see that there is an input box in the window. After copying the playing address into it, click the Open button in the lower right to start playing. The successful playing effect is shown in the figure below


Author: Tang Qingsong

Date: 2019-11-28

WeChat: songboy8888