nginx + rtmp 推流

鲁成天
2023-12-01

环境

安装Homebrow

ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

安装nginx

下载nginx项目到本地

brew tap homebrew/nginx

执行安装

brew install nginx-full -with-rtmp-module

到这一步,nginx和rtmp模块已经安装好了。

查看nginx信息:

brew info nginx-full

终端信息:

homebrew/nginx/nginx-full: stable 1.12.2, devel 1.13.6, HEAD
HTTP(S) server, reverse proxy, IMAP/POP3 proxy server
https://nginx.org/
Conflicts with:
  nginx (because nginx-full symlink with the name for compatibility with nginx)
/usr/local/Cellar/nginx-full/1.12.2 (8 files, 1.2MB) *
  Built from source on 2017-11-09 at 11:42:29 with: --with-rtmp-module
From: https://github.com/Homebrew/homebrew-nginx/blob/master/Formula/nginx-full.rb
==> Dependencies
Required: pcre ✔, openssl ✔
Optional: passenger ✘, geoip ✘, gperftools ✘, gd ✘, imlib2 ✘
==> Options
--with-accept-language-module
    Build with Accept Language support
--with-accesskey-module
    Build with HTTP Access Key support
--with-addition
    Build with HTTP Addition support
--with-ajp-module
    Build with AJP-protocol support
--with-anti-ddos-module
    Build with Anti-DDoS support
--with-array-var-module
    Build with Array Var support
--with-auth-digest-module
    Build with Auth Digest support
--with-auth-ldap-module
    Build with Auth LDAP support
--with-auth-pam-module
    Build with Auth PAM support
--with-auth-req
    Build with HTTP Auth Request support
--with-auto-keepalive-module
    Build with Auto Disable KeepAlive support
--with-autols-module
    Build with Flexible Auto Index support
--with-cache-purge-module
    Build with Cache Purge support
--with-captcha-module
    Build with Captcha support
--with-counter-zone-module
    Build with Realtime Counter Zone support
--with-ctpp2-module
    Build with CT++ support
--with-dav-ext-module
    Build with HTTP WebDav Extended support
--with-debug
    Build with debug log
--with-degredation
    Build with HTTP Degredation support
--with-dosdetector-module
    Build with detecting DoS attacks support
--with-echo-module
    Build with Echo support
--with-eval-module
    Build with Eval support
--with-extended-status-module
    Build with Extended Status support
--with-fancyindex-module
    Build with Fancy Index support
--with-flv
    Build with FLV support
--with-gd
    Build with gd support
--with-geoip
    Build with geoip support
--with-geoip2-module
    Build with GeoIP2 support
--with-google-perftools
    Build with Google Performance Tools support
--with-gperftools
    Build with gperftools support
--with-gunzip
    Build with Gunzip support
--with-gzip-static
    Build with Gzip static support
--with-headers-more-module
    Build with Headers More support
--with-healthcheck-module
    Build with Healthcheck support
--with-homebrew-libressl
    Include LibreSSL instead of OpenSSL via Homebrew
--with-http-accounting-module
    Build with HTTP Accounting support
--with-http-flood-detector-module
    Build with Var Flood-Threshold support
--with-http-remote-passwd-module
    Build with Remote Basic Auth Password support
--with-http2
    Build with HTTP/2 support
--with-image-filter
    Build with Image Filter support
--with-imlib2
    Build with imlib2 support
--with-log-if-module
    Build with Log-if support
--with-lua-module
    Build with LUA support
--with-mail
    Build with Mail support
--with-mail-ssl
    Build with Mail SSL/TLS support
--with-mod-zip-module
    Build with HTTP Zip support
--with-mogilefs-module
    Build with HTTP MogileFS support
--with-mp4
    Build with MP4 support
--with-mp4-h264-module
    Build with HTTP MP4/H264 support
--with-mruby-module
    Build with MRuby support
--with-naxsi-module
    Build with Naxsi support
--with-nchan-module
    Build with Nchan support
--with-no-pool-nginx
    Build without nginx-pool (valgrind debug memory)
--with-notice-module
    Build with HTTP Notice support
--with-passenger
    Build with passenger support
--with-pcre-jit
    Build with JIT in PCRE
--with-perl
    Build with Perl support
--with-php-session-module
    Build with Parse PHP Sessions support
--with-push-stream-module
    Build with HTTP Push Stream support
--with-random-index
    Build with Random Index support
--with-realip
    Build with Real IP support
--with-realtime-req-module
    Build with Realtime Request support
--with-redis-module
    Build with Redis support
--with-redis2-module
    Build with Redis2 support
--with-rtmp-module
    Build with RTMP support
--with-secure-link
    Build with Secure Link support
--with-set-misc-module
    Build with Set Misc support
--with-slice
    Build with Slice support
--with-small-light-module
    Build with Small Light support
--with-status
    Build with Stub Status support
--with-stream
    Build with TCP/UDP proxy support
--with-stream-geoip
    Build with Stream GeoIP support
--with-stream-realip
    Build with Stream RealIP support
--with-stream-ssl
    Build with Stream SSL/TLS support
--with-stream-ssl-preread
    Build with Stream without terminating SSL/TLS support
--with-sub
    Build with HTTP Sub support
--with-subs-filter-module
    Build with Substitutions Filter support
--with-tarantool-module
    Build with Tarantool upstream support
--with-tcp-proxy-module
    Build with TCP Proxy support
--with-txid-module
    Build with Sortable Unique ID support
--with-unzip-module
    Build with UnZip support
--with-upload-module
    Build with Upload support
--with-upload-progress-module
    Build with Upload Progress support
--with-upstream-order-module
    Build with Order Upstream support
--with-ustats-module
    Build with Upstream Statistics (HAProxy style) support
--with-var-req-speed-module
    Build with Var Request-Speed support
--with-vod-module
    Build with VOD on-the-fly MP4 Repackager support
--with-webdav
    Build with WebDAV support
--with-websockify-module
    Build with Websockify support
--with-xslt
    Build with XSLT support
--with-xsltproc-module
    Build with XSLT Transformations support
--devel
    Install development version 1.13.6
--HEAD
    Install HEAD version
==> Caveats
Docroot is: /usr/local/var/www

The default port has been set in /usr/local/etc/nginx/nginx.conf to 8080 so that
nginx can run without sudo.

nginx will load all files in /usr/local/etc/nginx/servers/.

- Tips -
Run port 80:
 $ sudo chown root:wheel /usr/local/opt/nginx-full/bin/nginx
 $ sudo chmod u+s /usr/local/opt/nginx-full/bin/nginx
Reload config:
 $ nginx -s reload
Reopen Logfile:
 $ nginx -s reopen
Stop process:
 $ nginx -s stop
Waiting on exit process
 $ nginx -s quit

To have launchd start homebrew/nginx/nginx-full now and restart at login:
  brew services start homebrew/nginx/nginx-full
Or, if you don't want/need a background service you can just run:
  nginx

nginx安装所在位置:

/usr/local/Cellar/nginx-full/1.12.2/bin/nginx

nginx配置文件所在位置:

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

运行nginx

nginx

在浏览器输入:http://localhost:8080

内容如下:

Welcome to nginx!

If you see this page, the nginx web server is successfully installed and working. Further configuration is required.

For online documentation and support please refer to nginx.org.
Commercial support is available at nginx.com.

Thank you for using nginx.

代表nginx安装成功

如果终端提示:

nginx: [emerg] bind() to 0.0.0.0:8080 failed (48: Address already in use)

则表示8080端口被占用了,查看端口的pid

lsof -i tcp:8080

结果如下:
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
xxx 7056 ygdx_lk 7u IPv4 0xf49473a52270b0b 0t0 TCP *:http-alt (LISTEN)

kill掉占用8080端口的pid

kill 7056(占用8080端口的pid)

然后重新执行nginx

nginx常用方法:

命令含义
nginx -s reload重新加载配置文件
nginx -s reopen重新加载日志
nginx -s stop停止nginx
nginx -s quit退出nigix

配置rtmp

修改nginx.conf这个配置文件(/usr/local/etc/nginx/nginx.conf)
修改后如下:


#user  nobody;
worker_processes  1;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;


events {
    worker_connections  1024;
}

rtmp {
    server{
        listen 1066;
        #直播流配置
        application rtmplive{
            live on;
            #为rtmp设置最大链接数。默认为off
            max_connections 1024;
        }
        application hls{
            live on;
            hls on;
            hls_path /usr/local/var/www/hls;
            hls_fragment 1s;
        }
    }
}


http {
    include       mime.types;
    default_type  application/octet-stream;

    #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
    #                  '$status $body_bytes_sent "$http_referer" '
    #                  '"$http_user_agent" "$http_x_forwarded_for"';

    #access_log  logs/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;

    server {
        listen       8080;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            root   html;
            index  index.html index.htm;
        }

        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        #location ~ \.php$ {
        #    proxy_pass   http://127.0.0.1;
        #}

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        #location ~ \.php$ {
        #    root           html;
        #    fastcgi_pass   127.0.0.1:9000;
        #    fastcgi_index  index.php;
        #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
        #    include        fastcgi_params;
        #}

        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\.ht {
        #    deny  all;
        #}
    }


    # another virtual host using mix of IP-, name-, and port-based configuration
    #
    #server {
    #    listen       8000;
    #    listen       somename:8080;
    #    server_name  somename  alias  another.alias;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}


    # HTTPS server
    #
    #server {
    #    listen       443 ssl;
    #    server_name  localhost;

    #    ssl_certificate      cert.pem;
    #    ssl_certificate_key  cert.key;

    #    ssl_session_cache    shared:SSL:1m;
    #    ssl_session_timeout  5m;

    #    ssl_ciphers  HIGH:!aNULL:!MD5;
    #    ssl_prefer_server_ciphers  on;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}
    include servers/*;
}

主要添加了这段配置:

rtmp {
    server{
        listen 1066;
        #直播流配置
        application rtmplive{
            live on;
            #为rtmp设置最大链接数。默认为off
            max_connections 1024;
        }
        application hls{
            live on;
            hls on;
            hls_path /usr/local/var/www/hls;
            hls_fragment 1s;
        }
    }
}

安装ffmpeg工具

brew install ffmpeg

推流

视频文件地址:/Users/xu/Desktop/input.mp4
推流拉流地址:rtmp://localhost:1935/rtmplive/home
acc:RTMP的音频格式
flv: RTMP的视频格式

ffmpeg -re -i /Users/xxx/Desktop/input.mp4 -vcodec libx264 -acodec aac -f flv rtmp://localhost:1066/rtmplive/home

拉流

打开VLC播放器
File->Open Network…
设置拉流地址:rtmp://localhost:1066/rtmplive/home

推流以后,点击open开始播放。

注意点:
打开网络偏好设置->状态->“Wi-Fi”已连接至“XXX”,其 IP 地址为 192.168.2.85

rtmp://192.168.2.85:1066/rtmplive/home则与上面的localhost是等价的。后续用手机进行推流,就不能用localhost推流了,只能改成对应的ip推流。

 类似资料: