Nginx itself is a very excellent HTTP server, FFmpeg is a very good audio and video solutions. These two things can be set up by a nginx module Nginx-rtmp-module, a relatively perfect streaming media server.
This streaming media server can support RTMP and HLS (Live Http Stream)
Starting from Installation
Nginx installation refer to my previous one: http://redstarofsleep.iteye.com/blog/2010391
The difference is in the configure need to increase the support of nginx-rtmp-module, download good nginx-rtmp-module after decompression, and then add this module Nginx installation (--add-module), the others are the same.
Java code
- ./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
FFmpeg Installation Reference previous article: http://redstarofsleep.iteye.com/blog/2122612
Nginx with FFmpeg to do streaming media server principle is: Nginx through the rtmp module to provide rtmp service, FFmpeg push a rtmp stream to Nginx, and then the client through the access to Nginx to watch real-time video streaming. HLS is the same principle, but the final client is accessed via the HTTP protocol, but the ffmpeg push stream is still rtmp.
After the installation is complete, open nginx configuration file nginx.conf to configure
First add the rtmp configuration to the inside
Java code
- rtmp {
- server {
- Listen 1935;
- Application MyApp {
- Live on;
- }
- Application HLS {
- Live on;
- HLS on;
- Hls_path/tmp/hls;
- }
- }
- }
Then, for HLS, you also need to add a location configuration in HTTP
Java code
- Location/hls {
- Types {
- Application/vnd.apple.mpegurl m3u8;
- VIDEO/MP2T ts;
- }
- root/tmp;
- Add_header Cache-control No-cache;
- }
This is the simplest, most basic configuration, rtmp Monitor 1935 port, if it is HLS, use HLS on to open HLS, and set up a temporary file directory for HLS Hls_path/tmp/hls; Other more advanced configurations can be found in Nginx-rtmp-module's readme, which provides a more detailed introduction to other configurations, and it also offers an example of how to play through Jwplayer on a Web page.
After saving the configuration file, start Nginx, through the NETSTAT-LTN command can see the increase of a 1935 port listening. 8080 is the Nginx default HTTP listening port.
Java code
- # NETSTAT-LTN
- Active Internet connections (only servers)
- Proto recv-q send-q Local address Foreign address state
- Tcp 0 0 127.0. 1.1:53 0.0.0.0:* listen
- Tcp 0 0 0.0. 0.0:22 0.0. 0.0:* listen
- Tcp 0 0 127.0. 0.1:631 0.0.0.0:* listen
- Tcp 0 0 0.0. 0.0:1935 0.0. 0.0:* listen
- Tcp 0 0 0.0. 0.0:8080 0.0. 0.0:* listen
- TCP6 0 0::: $: ::* LISTEN
- TCP6 0 0::1:631:::* LISTEN
Then use FFmpeg to push the flow to Nginx:
The first one is pushed onto the MyApp configured above:
Java code
- Ffmpeg-re-i "D:\download\film\aqgy\02.mp4"-vcodec libx264-vprofile baseline-acodec AAC
- -ar 44100-strict-2-ac 1-f flv-s 1280x720-q rtmp://server:1935/
- Myapp/test1
The second one pushes to HLS:
Java code
- Ffmpeg-re-i "D:\download\film\aqgy\02.mp4"-vcodec libx264-vprofile baseline-acodec AAC
- -ar 44100-strict-2-ac 1-f flv-s 1280x720-q rtmp://ip:1935/
- Hls/test2
Now our streaming media server has two real-time streaming, one is rtmp, the other is HLS, with a streaming media player, streaming media player can use VLC can also use FFmpeg belt ffplay. The phone is also playable.
The addresses of the above two streams are:
The first one is the push address: rtmp://serverip:1935/myapp/test1
The second one is HTTP address: http://serverIp:8080/hls/test2.m3u8
Finally, an explanation for the more specific streaming protocol for HLS:
(This explanation is from: http://www.cnblogs.com/haibindev/archive/2013/01/30/2880764.html)
HTTP Live Streaming (HLS) is Apple Inc. (Apple Inc) The implementation of the HTTP-based streaming media transmission protocol, can achieve streaming media live and on-demand, relative to the common streaming media live protocol, such as RTMP protocol, RTSP protocol, MMS protocol, HLS broadcast the biggest difference is that the live client gets, not a complete data stream. The HLS protocol stores the live stream as a continuous, short-length media file (mpeg-ts format) on the server side, while the client constantly downloads and plays these small files because the server side always generates new small files with the latest live data. So that the client as long as the sequential playback of the files obtained from the server, the implementation of the live broadcast. It can be seen, basically, that HLS is on-demand technical way to achieve live. Because the data through the HTTP protocol transmission, so completely do not consider the firewall or proxy problems, and the length of the fragmented file is very short, the client can quickly select and switch the bitrate to adapt to different bandwidth conditions of playback. However, this technical feature of HLS determines that its delay will always be higher than the normal streaming live protocol.
Nginx+nginx-rtmp-module+ffmpeg setting up a streaming media server