nginx+nginx-rtmp-module+ffmpeg build streaming media server

Nginx itself is a very good HTTP server, and FFMPEG is a very good audio and video solution. These two things can be combined to build a relatively complete streaming media server through a nginx module nginx-rtmp-module. .

This streaming server can support RTMP and HLS (Live Http Stream)

Start with installation

The installation of Nginx refers to my previous one: http://blog.csdn.net/redstarofsleep/article/details/45092127

The difference is that you need to increase the support of nginx-rtmp-module when configuring. After downloading nginx-rtmp-module, unzip it, and then add this module (--add-module) when nginx is installed. The others are the same.

 

  1. ./configure --prefix=/usr/local/nginx --with-pcre=/home/user/pcre/pcre-8.32 --with-zlib=/home/user/zlib/zlib-1.2.8 --with-openssl=/home/user/openssl/openssl-1.0.1i  --add-module=/home/user/nginx-rtmp-module  

For the installation of FFMPEG, refer to the previous article: http://blog.csdn.net/redstarofsleep/article/details/45092145

 

The principle of nginx working with ffmpeg as a streaming media server is as follows: nginx provides rtmp services through the rtmp module, ffmpeg pushes an rtmp stream to nginx, and then the client accesses nginx to watch the real-time video stream. HLS is also the same principle, but the final client is It is accessed through the HTTP protocol, but the ffmpeg push stream is still rtmp.

 

After the installation is complete, open the Nginx configuration file nginx.conf for configuration

First add the rtmp configuration inside

  1. rtmp {  
  2.     server {  
  3.         listen 1935;  
  4.   
  5.         application myapp {  
  6.             live on;  
  7.         }  
  8.         application hls {  
  9.             live on;  
  10.             hls on;  
  11.             hls_path /tmp/hls;  
  12.         }  
  13.     }  
  14. }  

Then, for hls, you need to add a location configuration to http

  1. location /hls {  
  2.             types {  
  3.                 application/vnd.apple.mpegurl m3u8;  
  4.                 video/mp2t ts;  
  5.             }  
  6.             root /tmp;  
  7.             add_header Cache-Control no-cache;  
  8. }  

 

This is the simplest and most basic configuration, rtmp listens on port 1935, if it is hls, use hls on to open hls, and set a temporary file directory hls_path /tmp/hls for hls; other more advanced configurations can refer to nginx-rtmp -module's readme, which introduces other configurations in detail, and it also provides an example of playing on a web page through JWPlayer.

 

After saving the configuration file, start nginx, and you can see that a listener on port 1935 has been added through the netstat -ltn command. 8080 is the default http listening port of nginx.

  1. # netstat -ltn  
  2. Active Internet connections (only servers)  
  3. Proto Recv-Q Send-Q Local Address           Foreign Address         State        
  4. tcp        00127.0.1.1:530.0.0.0:*               LISTEN                          
  5. tcp        000.0.0.0:220.0.0.0:*               LISTEN                            
  6. tcp        00127.0.0.1:6310.0.0.0:*               LISTEN                         
  7. tcp        000.0.0.0:19350.0.0.0:*               LISTEN                          
  8. tcp        000.0.0.0:80800.0.0.0:*               LISTEN                          
  9. tcp6       00 :::22                   :::*                    LISTEN             
  10. tcp6       00 ::1:631                 :::*                    LISTEN          

Then push the stream to nginx with ffmpeg:

The first one is pushed to myapp configured above:

  1. ffmpeg -re -i "D:\download\film\aqgy\02.mp4" -vcodec libx264 -vprofile baseline -acodec aac  
  2.  -ar 44100 -strict -2 -ac 1 -f flv -s 1280x720 -q 10 rtmp://server:1935/  
  3. myapp/test1  

The second push to hls:

  1. ffmpeg -re -i "D:\download\film\aqgy\02.mp4" -vcodec libx264 -vprofile baseline -acodec aac  
  2.  -ar 44100 -strict -2 -ac 1 -f flv -s 1280x720 -q 10 rtmp://ip:1935/  
  3. hls/test2  

Now our streaming media server has two real-time streams, one is rtmp and the other is hls. Play it with a streaming media player. The streaming media player can use vlc or ffplay with ffmpeg. Mobile phones can also be used. played.

 

The addresses of the two streams above are:

The first is the push address: rtmp://serverIp:1935/myapp/test1

The second is the HTTP address: http://serverIp:8080/hls/test2.m3u8

 

Finally, paste an explanation for the special streaming media protocol of HLS:

       (This explanation comes from: http://www.cnblogs.com/haibindev/archive/2013/01/30/2880764.html)

  HTTP Live Streaming (HLS) is an HTTP-based streaming media transmission protocol implemented by Apple Inc., which can realize live and on-demand streaming media. Compared with common live streaming media protocols, such as RTMP protocol, RTSP protocol, MMS The biggest difference of HLS live broadcast is that what the live broadcast client obtains is not a complete data stream. The HLS protocol stores the live data stream as continuous, short-duration media files (MPEG-TS format) on the server side, and the client side continuously downloads and plays these small files, because the server side always sends the latest live broadcast. The data generates new small files, so that as long as the client continuously plays the files obtained from the server in sequence, the live broadcast is realized. It can be seen that, basically, it can be considered that HLS realizes live broadcast by means of on-demand technology. Since the data is transmitted through the HTTP protocol, there is no need to consider the problem of firewalls or proxies, and the duration of the segmented files is very short, and the client can quickly select and switch the bit rate to adapt to playback under different bandwidth conditions. However, this technical feature of HLS determines that its delay is generally higher than that of ordinary live streaming protocols.

 

 

http://blog.csdn.net/redstarofsleep/article/details/45092147

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326850049&siteId=291194637