How to add the nginx-rtmp-module module to the nginx installed by brew, and set up the rtmp stream processing server
给brew安装的 nginx 添加 nginx-rtmp-module 模块,并搭建rtmp流处理服务器.
环境基础信息如下:
- macOS Mojave 版本 10.14
- nginx 版本 1.15.5
- nginx-rtmp-module 版本 1.2.1
首先给 brew 安装的 nginx 添加 nginx-rtmp-module 模块
1 2 3 4 5 | $ mkdir -p /usr/local/Cellar/nginx-modules $ cd /usr/local/Cellar/nginx-modules $ wget https://github.com/arut/nginx-rtmp-module/archive/v1.2.1.zip $ unzip v1.2.1.zip $ brew edit nginx # 在编译参数后面添加如下内容 |
添加编译参数 --add-module=/usr/local/Cellar/nginx-modules/nginx-rtmp-module-1.2.1
1 | $ brew reinstall --build-from-source nginx # 重新编译安装nginx |
编译的时候会有多个源码文件爆如下错误(还不知道是什么原因导致?).
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | $ brew reinstall --build-from-source nginx -o objs/addon/nginx-rtmp-module-1.2.1/ngx_rtmp_stat_module.o \ /usr/local/Cellar/nginx-modules/nginx-rtmp-module-1.2.1/ngx_rtmp_stat_module.c clang -c -pipe -O -Wall -Wextra -Wpointer-arith -Wconditional-uninitialized -Wno-unused-parameter -Wno-deprecated-declarations -Werror -g -I/usr/local/opt/pcre/include -I/usr/local/opt/openssl/include -I src/core -I src/event -I src/event/modules -I src/os/unix -I /usr/local/Cellar/nginx-modules/nginx-rtmp-module-1.2.1 -I objs -I src/http -I src/http/modules -I src/http/v2 -I src/mail -I src/stream \ -o objs/addon/nginx-rtmp-module-1.2.1/ngx_rtmp_control_module.o \ /usr/local/Cellar/nginx-modules/nginx-rtmp-module-1.2.1/ngx_rtmp_control_module.c clang -c -pipe -O -Wall -Wextra -Wpointer-arith -Wconditional-uninitialized -Wno-unused-parameter -Wno-deprecated-declarations -Werror -g -I/usr/local/opt/pcre/include -I/usr/local/opt/openssl/include -I src/core -I src/event -I src/event/modules -I src/os/unix -I /usr/local/Cellar/nginx-modules/nginx-rtmp-module-1.2.1 -I objs \ -o objs/ngx_modules.o \ objs/ngx_modules.c /usr/local/Cellar/nginx-modules/nginx-rtmp-module-1.2.1/dash/ngx_rtmp_dash_module.c:7:10: fatal error: 'ngx_rtmp_live_module.h' file not found #include "ngx_rtmp_live_module.h" ^~~~~~~~~~~~~~~~~~~~~~~~ 1 error generated. make[1]: *** [objs/addon/dash/ngx_rtmp_dash_module.o] Error 1 make[1]: *** Waiting for unfinished jobs.... make: *** [install] Error 2 READ THIS: https://docs.brew.sh/Troubleshooting These open issues may also help: nginx-unit 1.1 (new formula) https://github.com/Homebrew/homebrew-core/pull/27260 |
解决办法是修改报错的 c 源码文件的 include 文件路径(修改成后如下图),需要根据报错的文件进行修改,实测修改编译后的程序无问题:
1 2 3 4 5 | $ brew reinstall --build-from-source nginx # 直至修改源码文件无误后,重新编译安装nginx $ sudo chown root:wheel /usr/local/opt/nginx/bin/nginx $ sudo chmod u+s /usr/local/opt/nginx/bin/nginx $ cp /usr/local/Cellar/nginx-modules/nginx-rtmp-module-1.2.1/stat.xsl /usr/local/etc/nginx/ $ mkdir -p /data/development/tmp/hls # 创建 hls 文件临时目录,根据需要进行修改 |
然后编辑 nginx.conf 添加如下配置(我本机的配置):
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 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 | #user _www; worker_processes 4; error_log /usr/local/var/log/nginx/localhost.error.log; pid /usr/local/var/log/nginx/nginx.pid; events { worker_connections 65535; } http { server_tokens off; fastcgi_intercept_errors on; include servers/mime.types; include servers/main.conf; include servers/gzip.conf; include servers/proxy.conf; upstream php { #server unix:/private/tmp/php-fpm.sock; server 127.0.0.1:9500; } log_format main '$remote_addr - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" "$http_x_forwarded_for"'; log_format access '$remote_addr - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" "$http_x_forwarded_for"'; include servers/vhost/kernelstudio/*.conf; # rtmp http 服务端配置 server { listen 80; server_name rtmp.kernelstudio.com; # 此处可设置成自定义的域名 # application的配置 location / { root /data/development/tmp; index index.html index.htm; } #配置rtmp stat location /stat { rtmp_stat all; rtmp_stat_stylesheet stat.xsl; } location /stat.xsl { root /usr/local/etc/nginx/; } location /control { rtmp_control all; } #HLS配置开始,这个配置为了`客户端`能够以http协议获取HLS的拉流 location /hls { # Serve HLS fragments types { application/vnd.apple.mpegurl m3u8; video/mp2t ts; } root /data/development/tmp; add_header Cache-Control no-cache; } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } } } # 单独在 http 段外配置 rtmp 服务 rtmp { server { listen 1935; publish_time_fix on; application live { live on; allow publish all; allow play all; } application hls { live on; hls on; #把直播服务器改造成实时回放服务器。 hls_path /data/development/tmp/hls; #切片视频文件存放位置。 wait_key on; #对视频切片进行保护,以免产生马赛克 hls_fragment 10s; #每个视频切片的时长 hls_playlist_length 60s; #总共可以回看的事件,这里设置的是1分钟 hls_continuous on; #连续模式 hls_cleanup on; #对多余的切片进行删除 hls_nested on; #嵌套模式 } } } |
如果nginx之前是启动状态则重启nginx服务.
1 2 | $ nginx -s reload $ sudo vim /etc/hosts # 添加 127.0.0.1 rtmp.kernelstudio.com |
打开浏览器访问 http://rtmp.kernelstudio.com/stat 看到如下图就说明安装成功:
如果未安装ffmpeg 则使用 brew 进行安装:
1 | $ brew install ffmpeg |
使用 ffmpeg 进行流推测试(下面的命令是推的直播形式的流):
1 | ffmpeg -re -i /Users/zlin/Downloads/2.mp4 -vcodec copy -f flv rtmp://rtmp.kernelstudio.com:1935/live/2 |
下载 VLC 播放器进行测试观看 ( File -> Open Network ):
软件点击 play 播放,出现如下视频则表示安装配置没问题:
进行web端的hls播放测试.
1 2 3 | $ ffmpeg -re -i /Users/zlin/Downloads/2.mp4 -vcodec copy -f flv rtmp://rtmp.kernelstudio.com:1935/hls/2 $ cd /data/development/tmp $ vim index.html # 添加如下内容 |
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 | <!DOCTYPE html> <html lang="zh-hans"> <head> <meta charset="UTF-8"> <title>video demo</title> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/> <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1"> <meta name="viewport" content="initial-scale=1.0, minimum-scale=1.0, maximum-scale=2.0, user-scalable=no, width=device-width"> <link href="https://cdnjs.cloudflare.com/ajax/libs/video.js/6.6.2/video-js.min.css" rel="stylesheet"> <script src="https://cdn.bootcss.com/jquery/1.12.1/jquery.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/video.js/6.6.2/video.min.js"></script> </head> <body id="page-body" class="page-body "> <video id="videoPlayer" class="video-js vjs-default-skin vjs-big-play-centered" controls preload="auto" width="640" height="360" data-setup='{ "html5" : { "nativeTextTracks" : false } }'> <source src="http://rtmp.kernelstudio.com/hls/2/index.m3u8" type="application/x-mpegURL"/> </video> <script> jQuery(function($){ var player = videojs('videoPlayer'); }); </script> </body> </html> |
打开浏览器访问 http://rtmp.kernelstudio.com 查看推送的视频是否成功.