使用 nginx 和 rtmp 插件搭建视频直播和点播服务器

使用 nginx 和 rtmp 插件搭建视频直播和点播服务器

Filed Under (技术) by waterlin

使用 nginx 和 rtmp 模块,可以很容易地搭建一个视频直播和点播服务器出来。

首先,看一下最经典的参考文献:How to set up your own private RTMP server using nginx

1. 安装 nginx 和 rtmp 模块

有关 nginx 的编译和安装比较简单,这里就不介绍了,看参考文献。这里提示以下几点:

(1) 安装好 nginx 后,配置文件在这里:

1

/usr/local/nginx/conf/nginx.conf

(2) 启动 nginx 的命令:

1

2

$ sudo /usr/local/nginx/sbin/nginx -s stop

$ sudo /usr/local/nginx/sbin/nginx

2. 配置 nginx 视频直播和点播服务

先看一下完整的 nginx 配置文件里有关视频点播和直播的配置:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

rtmp {

        server {

                listen 1935;

                chunk_size 4096;

                application live {

                        live on;

                        record off;

                }

                application live2 {

                        live on;

                        record off;

                }

                # video on demand

                application vod {

                        play /var/flvs;

                }

                application vod_http {

                        play http://192.168.31.185/vod;

                }

                application hls {

                        live on;

                        hls on;

                        hls_path /tmp/hls;

                }

        }

}

# HTTP can be used for accessing RTMP stats

http {

    server {

        listen      8080;

        # This URL provides RTMP statistics in XML

        location /stat {

            rtmp_stat all;

            # Use this stylesheet to view XML as web page

            # in browser

            rtmp_stat_stylesheet stat.xsl;

        }

        location /stat.xsl {

            # XML stylesheet to view RTMP stats.

            # Copy stat.xsl wherever you want

            # and put the full directory path here

            root /path/to/stat.xsl/;

        }

        location /hls {

            # Serve HLS fragments

            types {

                application/vnd.apple.mpegurl m3u8;

                video/mp2t ts;

            }

            root /tmp;

            add_header Cache-Control no-cache;

        }

        location /dash {

            # Serve DASH fragments

            root /tmp;

            add_header Cache-Control no-cache;

        }

    }

}

现在来解释一下里面各行代码的含义。对于视频直播服务,如果需要支持多路流输入的话,很简单,在 nginx 配置文件里多配几个 Application 就只可以了,像下面这样:

1

2

3

4

5

6

7

8

9

application live {

        live on;

        record off;

}

application live2 {

        live on;

        record off;

}

这样就可以通过下面的地址来推送直播流,其它观众端也可以通过下面的地址来访问直播流:

1

2

rtmp://192.168.31.185/live/test

rtmp://192.168.31.185/live2/test

后面紧跟的 test 关键字,可以随便更换,只要你的推送流和访问流的地址一样就可以了。

rtmp 模块也可以直接支持 VOD 这种视频点播服务,只需要在配置文件里添加如下内容即可:

1

2

3

4

5

6

7

8

# video on demand

application vod {

    play /var/flvs;

}

application vod_http {

    play http://myserver.com/vod;

}

然后把一个 mp4 或是 flv 文件扔到 /var/flvs 目录下,对于 /var/flvs/dir/file.flv 这个视频文件,就可以通过下面的网址来访问了:

1

rtmp://myserver.com/vod//dir/file.flv

(原为:http://myserver.com/vod//dir/file.flv

这样直接在浏览器里就可以通过网页观看视频。对于 mp4 文件,也可以实现 VOD 服务,不过需要的是采用 H.264 和 AAC 格式编码的 mp4 文件。

3. HLS 直播流的配置

如果需要使用 HLS 来视频直播,可以直接像配置文件那样,写上下面这一段:

1

2

3

4

5

application hls {

        live on;

        hls on;

        hls_path /tmp/hls;

}

同时把后面有关 http 访问的内容写上:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

# HTTP can be used for accessing RTMP stats

http {

    server {

        listen      8080;

        # This URL provides RTMP statistics in XML

        location /stat {

            rtmp_stat all;

            # Use this stylesheet to view XML as web page

            # in browser

            rtmp_stat_stylesheet stat.xsl;

        }

        location /stat.xsl {

            # XML stylesheet to view RTMP stats.

            # Copy stat.xsl wherever you want

            # and put the full directory path here

            root /path/to/stat.xsl/;

        }

        location /hls {

            # Serve HLS fragments

            types {

                application/vnd.apple.mpegurl m3u8;

                video/mp2t ts;

            }

            root /tmp;

            add_header Cache-Control no-cache;

        }

        location /dash {

            # Serve DASH fragments

            root /tmp;

            add_header Cache-Control no-cache;

        }

    }

}

配好以后,推流可以使用下面的地址:

1

rtmp://192.168.31.185/hls/movie

movie 关键字可以任何替换。对于观众端来说,可以有几种播放方式:

(1) 用 rtmp:

1

rtmp://192.168.31.185/hls/movie

(2) 用 hls 播放:

1

http://192.168.31.185:8080/hls/movie.m3u8

这样就可以看到主播端推出来的流。注意,如果使用 http 方式,则是监听的 8080 端口,这个是在配置文件里写的。

4. 网页播放器插件

在第二步里,除了可以直接在浏览器里打开网址来观看视频,还可以写一个网页,实现像优酷那样的视频点播业务。通过使用第三方的播放器,在网页里植入该播放器来实现这个功能,比如说使用 JWPlayer 播放器。

下载 JWPlayer 播放器,放到 nginx 网页服务的根目录,比如说,在我这儿是 /usr/local/nginx/html 这个目录,把 JWPlayer 解压后放到这个目录里,然后在这个目录里新建一个 play.html 的文本文件,再写入以下内容:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

&lt;<span style="color: #cae682">html</span>&gt;

  &lt;<span style="color: #cae682">head</span>&gt;

    &lt;<span style="color: #cae682">script</span> <span style="color: #cae682">src</span>=<span style="color: #95e454">"/jwplayer/jwplayer.js"</span>&gt;&lt;/<span style="color: #cae682">script</span>&gt;

  &lt;/<span style="color: #cae682">head</span>&gt;

&lt;<span style="color: #cae682">body</span>&gt;

  &lt;<span style="color: #cae682">div</span> <span style="color: #cae682">id</span>='my-video'&gt;&lt;/<span style="color: #cae682">div</span>&gt;

  &lt;<span style="color: #cae682">script</span> <span style="color: #cae682">type</span>='text/javascript'&gt;

    jwplayer('my-video').setup({

      file:'http://192.168.31.185/vod/North.mp4',

      fallback:'false'

    });

  &lt;/<span style="color: #cae682">script</span>&gt;

&lt;/<span style="color: #cae682">body</span>&gt;

&lt;/<span style="color: #cae682">html</span>&gt;

对于 flv 文件,可以采用如下的形式来实现网页播放:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

&lt;<span style="color: #cae682">html</span>&gt;

  &lt;<span style="color: #cae682">head</span>&gt;

    &lt;<span style="color: #cae682">script</span> <span style="color: #cae682">src</span>=<span style="color: #95e454">"/jwplayer/jwplayer.js"</span>&gt;&lt;/<span style="color: #cae682">script</span>&gt;

  &lt;/<span style="color: #cae682">head</span>&gt;

&lt;<span style="color: #cae682">body</span>&gt;

  &lt;<span style="color: #cae682">div</span> <span style="color: #cae682">id</span>='my-video'&gt;&lt;/<span style="color: #cae682">div</span>&gt;

  &lt;<span style="color: #cae682">script</span> <span style="color: #cae682">type</span>='text/javascript'&gt;

    jwplayer('my-video').setup({

      file:'http://192.168.31.185/vod/1.flv',

      width:'50%',

      aspectratio:'3:2',

      fallback:'false',

      primary:'flash'

    });

  &lt;/<span style="color: #cae682">script</span>&gt;

&lt;/<span style="color: #cae682">body</span>&gt;

&lt;/<span style="color: #cae682">html</span>&gt;

这样,当你打开 http://192.168.31.185/play.html 这个网页时,就能看到 html 里指定的视频文件。

另外,如果有一些 flv 文件没有办法拖动播放,那是因为没有给 flv 文件添加“关键帧”,可以用工具“yamdi” 和“flvtool”来给 flv 生成关键帧,以 yamdi 为例,下载解压后从 shell 执行:

1

yamdi &#8211;i src.flv &#8211;o dst.flv

意思是将 src.flv 添加关键帧,并输出为 dst.flv 。这样 flv 就有关键帧了,即可进行拖动播放操作。

发布了123 篇原创文章 · 获赞 62 · 访问量 6万+

猜你喜欢

转载自blog.csdn.net/Archger/article/details/100188842