nginx rtmp

rtmp 是协议名称
server 说明内部中是服务器的相关配置
listen 监听的端口,rtmp协议的默认端口为1935
application 访问的应用路径是hls
live on 启用rtmp直播
record off 不记录数据
hls on 启用hls直播
hls_path 切片保存位置
hls_fragment 每个切片的长度

nginx+rtmp
下载nginx稳定版本
cd /tmp
# http://nginx.org/en/download.html
wget http://nginx.org/download/nginx-1.19.1.tar.gz
# https://www.openssl.org/source/
wget https://www.openssl.org/source/openssl-1.1.1g.tar.gz
tar zxvf openssl-1.1.1g.tar.gz
git clone https://github.com/arut/nginx-rtmp-module.git
tar zxvf nginx-1.19.1.tar.gz
cd nginx-1.19.1
./configure --add-module=/tmp/nginx-rtmp-module --with-debug --with-openssl=/tmp/opensslg
make
sudo make install

#全量编辑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 1935; # rtmp 端口  1935为nginx 默认端口
        chunk_size 4096; # 流整合的最大的块大小。默认值为 4096。这个值设置的越大 CPU 负载就越小。这个值不能低于 128。
        application hls {
            live on;  # 直播开启
            hls on;  # hls 开启
            hls_path /Users/kendo/Desktop/program/rtmp/streaming; #hls 的m3u8索引文件存储  这个地方需要给到nginx的目录操作权限
            hls_fragment 5s; # hls 每片长度为5秒
            hls_playlist_length 55s;
            allow play all;
            #边播边存配置
            recorder rec{
                record all;   #存储所有,包括视频及音频
                record_unique on;  #是否添加时间戳到录制文件。否则的话同样的文件在每一次新的录制发生时将被重写。默认为 off。
                record_path /Users/kendo/Desktop/program/rtmp/record;   #录制文件存储地址配置  这个地方需要给到nginx的目录操作权限
                record_suffix -%Y-%m-%d-%H_%M_%S.flv;   #录制存储文件名字配置
            }
        }
    }
}

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       81;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            root   /Users/kendo/Desktop/program/rtmp/streaming;
            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;
        #}
        location /stat{
            rtmp_stat all;
            rtmp_stat_stylesheet stat.xsl;
        }
        location /stat.xsl{
            root /tmp/nginx-rtmp-module/;
        }
        location /test{
            alias /tmp/nginx-rtmp-module/test/rtmp-publisher/;
        }
        location /hls{
            #server hls fragments
            types{
                application/vnd.apple.mpegurl m3u8;
                video/mp2t ts;
            }
            alias /Users/kendo/Desktop/program/rtmp/streaming;
            expires -1;
            add_header Cache-Control no-cache;
        }
    }


    # 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;
    #    }
    #}

}


#启动nginx
sudo /usr/local/nginx/sbin/nginx


# mac推送桌面
ffmpeg -f avfoundation -pixel_format uyvy422 -i "1" -f flv rtmp://localhost:1935/hls/room1
# mac推送摄像头
ffmpeg -f avfoundation -pixel_format uyvy422 -i "0" -f flv rtmp://localhost:1935/hls/room2
# mac推送多个,假设3时外设录音
ffmpeg -f avfoundation -pixel_format uyvy422 -i "0:3" -f flv rtmp://localhost:1935/hls/room2

打开VLC->File->Open Network
粘贴地址:rtmp://localhost:1935/hls/room1 ,确定添加
添加成功后,对着记录右击,选择play

查看有哪些设备

ffmpeg -f avfoundation -list_devices true
-i ""
[AVFoundation indev @ 0x7ff122c00a00] AVFoundation video devices: [AVFoundation indev @ 0x7ff122c00a00] [0] FaceTime HD Camera (Built-in) [AVFoundation indev @ 0x7ff122c00a00] [1] Capture screen 0 [AVFoundation indev @ 0x7ff122c00a00] [2] Capture screen 1 [AVFoundation indev @ 0x7ff122c00a00] AVFoundation audio devices: [AVFoundation indev @ 0x7ff122c00a00] [0] Built-in Microphone