1 Preparation
Install the dependency packages needed by nginx
- Install mercurial to support hg command
sudo add-apt-repository ppa:tortoisehg-ppa/releases
sudo add-apt-repository ppa:mercurial-ppa/releases
sudo apt-get update
sudo apt-get install mercurial python-nautilus tortoisehg
sudo apt install mercurial
- Solve the dependency package openssl installation
sudo apt-get install openssl libssl-dev
- Solve the installation of dependent package pcre
sudo apt-get install libpcre3 libpcre3-dev
- Resolve the dependency package zlib installation
sudo apt-get install zlib1g-dev
If the corresponding dependency package is not installed, nginx cannot be compiled normally
Alibaba Cloud Simple Application Server: Anti COVID-19 SME Enablement Program
$300 coupon package for all new SMEs and a $500 coupon for paying customers.
2 Compile Nginx
2.1 Nginx download
- Create rtmp in the user directory
mkdir ~/rtmp
cd ~/rtmp
Official website: http://nginx.org/
- Download nginx (need to install mercurial to support hg command)
hg clone http://hg.nginx.org/nginx
Download nginx-rtmp-module
Official website: nginx-rtmp-module
git clone https://github.com/arut/nginx-rtmp-module.git
at this time
lqf@ubuntu:~/rtmp$ ls
nginx nginx-rtmp-module
2.2 Compile
Enter the root directory of nginx source code:
cd nginx
Configuration
auto/configure --with-http_ssl_module --with-http_v2_module --with-http_flv_module --with-http_mp4_module --add-module=../nginx-rtmp-module
Compile and install
make -j4
sudo make install
Installed to /usr/local/nginx by default
Configuration file path: /usr/local/nginx/conf/nginx.conf
3 Configure nginx
3.1 On-demand configuration
In your configuration, you need to change the IP to your
server IP or domain name.
3.1.1 Create Media Folder
/mnt/hgfs/dn_class/vod
3.1.2 Prepare media files:
Copy the media file 35.mp4 to the /mnt/hgfs/dn_class/vod directory.
The media files can be copied by themselves, in accordance with the AAC+H264 format.
3.1.3 Configure http in nginx
After the nginx
server has been set up in the previous step, we can start a video-on-demand service. Open the configuration file nginx.conf (path /usr/local/nginx/conf/nginx.conf) and add the RTMP configuration.
#user nobody;
worker_processes 1;
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid;
events {
worker_connections 1024;
}
rtmp {#RTMP server
server {
listen 1935; #server port
chunk_size 4096; #chunk_size
application vod {
play /mnt/hgfs/dn_class/vod; #media file position
}
}
}
........
Don't care about other configurations
Modify the configuration file to support on-demand
The configuration directory /mnt/hgfs/qingfu/vod is the location for storing video files, so let's put a file in it. I put a 35.mp4 file.
After the file is placed, let us restart nginx
sudo /usr/local/nginx/sbin/nginx -s reload
If an error is reported
nginx: [error] open() "/usr/local/nginx/logs/nginx.pid" failed (2: No such file or directory)
It means that nginx is not started, so you need to start it first
sudo /usr/local/nginx/sbin/nginx
3.1.4 Play with ffplay on Windows
Note: The Ubuntu network connection installed on the virtual machine needs to select "Bridge Mode", so that windows and Ubuntu are IPs on the same network segment.
My current IP is: 192.168.100.33
ffplay rtmp://192.168.100.33/vod/35.mp4
It can be played under normal configuration. If it cannot play normally, you need to check whether the vod directory is correct.
3.2 Live broadcast configuration
Then we add the configuration of the live
server on the basis of the on-demand server configuration file.
3.2.1 Configuration
Adding an application to the RTMP service can be named arbitrarily or multiple names. Since it is a live broadcast, I call it live. If you plan to make multiple sequences of live broadcasts, you can use live_cctv.
#user nobody;
worker_processes 1;
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid;
events {
worker_connections 1024;
}
rtmp {#RTMP server
server {
listen 1935; #server port
chunk_size 4096; #chunk_size
# vod server
application vod {
play /mnt/hgfs/dn_class/vod; #media file position
}
# live server 1
application live{ #Darren live first add
live on;
}
# live server 2
application live_cctv{ #Darren live add
live on;
}
}
}
........
Don't care about other configurations
Comparison with original file after modification
3.2.2 Push Stream
Use ffmpeg to generate a simulated live source on Ubuntu and push it to the rtmp
server
ffmpeg -re -i /mnt/hgfs/dn_class/vod/35.mp4 -c copy -f flv rtmp://192.168.100.33/live/35
or
ffmpeg -re -i /mnt/hgfs/dn_class/vod/35.mp4 -c copy -f flv rtmp://192.168.100.33/live_cctv/35
Note that the source file must be H.264+AAC encoded.
3.2.3 Pull flow
Use ffplay to pull streaming on the windows side
ffplay rtmp://192.168.100.33/live/35
Or (need to correspond to push streaming)
ffplay rtmp://192.168.100.33/live_cctv/35