Nginx+nginx-rtmp-module+ffmpeg setting up a streaming media server

Source: Internet
Author: User

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
    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

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
  1. rtmp {
  2. server {
  3. Listen 1935;
  4. Application MyApp {
  5. Live on;
  6. }
  7. Application HLS {
  8. Live on;
  9. HLS on;
  10. Hls_path/tmp/hls;
  11. }
  12. }
  13. }

Then, for HLS, you also need to add a location configuration in HTTP

Java code
    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, 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
  1. # NETSTAT-LTN
  2. Active Internet connections (only servers)
  3. Proto recv-q send-q Local address Foreign address state
  4. Tcp        0       0 127.0. 1.1:53             0.0.0.0:*                listen       
  5. Tcp        0       0 0.0. 0.0:22               0.0. 0.0:*               listen        
  6. Tcp        0       0 127.0. 0.1:631            0.0.0.0:*                listen       
  7. Tcp        0       0 0.0. 0.0:1935            0.0. 0.0:*                listen       
  8. Tcp        0       0 0.0. 0.0:8080            0.0. 0.0:*                listen       
  9. TCP6 0 0::: $: ::* LISTEN
  10. 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
    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 rtmp://server:1935/
    3. Myapp/test1

The second one pushes to HLS:

Java code
    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 rtmp://ip:1935/
    3. 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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.