nginx搭建rtmp服务器实现直播

1.nginx 安装rtmp模块

1.1.下载对应版本的tgz包

https://codeload.github.com/arut/nginx-rtmp-module/tar.gz/refs/tags/v1.2.2

1.2.下载依赖

此处依赖可能不全,因为每个个操作系统环境依赖可能都不一样,如果依赖不够在编译时会有相应的错误提示,再根据提示安装相应的依赖即可。

yum -y install perl-ExtUtils-Embed readline-devel zlib-devel pam-devel libxml2-devel libxslt-devel openldap-devel python-devel gcc-c++   openssl-devel cmakepcre-develnanowget  gcc gcc-c++ ncurses-devel per redhat-rpm-config gd gd-devel
yum install redhat-rpm-config 
yum install gd gd-devel
yum install gperftools

1.3 编译配置

解压并进入rtmp模块下

tar -zxvf nginx-rtmp-module-1.2.2.tar.gz

首先查看nginx原有配置

nginx -V

再将原有配置加上

--add-module=nginx-rtmp-module-1.2.2

得到如下配置:

./configure --prefix=/usr/share/nginx --sbin-path=/usr/sbin/nginx --modules-path=/usr/lib64/nginx/modules --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --http-client-body-temp-path=/var/lib/nginx/tmp/client_body --http-proxy-temp-path=/var/lib/nginx/tmp/proxy --http-fastcgi-temp-path=/var/lib/nginx/tmp/fastcgi --http-uwsgi-temp-path=/var/lib/nginx/tmp/uwsgi --http-scgi-temp-path=/var/lib/nginx/tmp/scgi --pid-path=/run/nginx.pid --lock-path=/run/lock/subsys/nginx --user=nginx --group=nginx --with-compat --with-debug --with-file-aio --with-google_perftools_module --with-http_addition_module --with-http_auth_request_module --with-http_dav_module --with-http_degradation_module --with-http_flv_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_image_filter_module=dynamic --with-http_mp4_module --with-http_perl_module=dynamic --with-http_random_index_module --with-http_realip_module --with-http_secure_link_module --with-http_slice_module --with-http_ssl_module --with-http_stub_status_module --with-http_sub_module --with-http_v2_module --with-http_xslt_module=dynamic --with-mail=dynamic --with-mail_ssl_module --with-pcre --with-pcre-jit --with-stream=dynamic --with-stream_ssl_module --with-stream_ssl_preread_module --with-threads --with-cc-opt='-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches -specs=/usr/lib/rpm/redhat/redhat-hardened-cc1 -m64 -mtune=generic' --with-ld-opt='-Wl,-z,relro -specs=/usr/lib/rpm/redhat/redhat-hardened-ld -Wl,-E' --add-module=nginx-rtmp-module-1.2.2

开始编译

# 4指线程数
make -j4

以上完成后,会在objs目录下生成一个nginx文件,先验证:

./nginx -t
./nginx -V

文件替换,并重启

#命令仅供参考,以实际为准
cp objs/nginx /usr/sbin/

2.nginx rtmp 配置

此处要注意,rtmp是个单独的模块,和http是同级的,在nginx.conf中的结构是这样的

#rtmp模块,配置视频推流和数据转存
rtmp{
    server{

    }
}
#http模块,配置代理和访问相关
http{
    server{

    }
}

2.1.直播配置

rtmp{

    server{
        listen 1999;
        chunk_size 8192;

        # rtmp直播(推荐使用hls,rmtp想要在浏览器播放需要安装flash插件,浏览器默认都不支持flash了)
        application live{
            live on;
            max_connections 1024;
            allow play all;

            record_path /mnt/rtmp/live;

            recorder audio{
                record audio;
                record_suffix -%d-%b-%y-%T.flv;
            }

            recorder chunked{
                record all;
                #record_max_size 5120K;
                record_interval 15s;
                record_path /mnt/rtmp/live/chunked;
            }
        }

        # hls直播(rmtp转换成了m3u8,直接支持浏览器播放)
        application hls{
            live on;
            hls on;
            hls_path /mnt/rtmp/hls/live;
            hls_fragment 1s;
            hls_playlist_length 3s;
            hls_sync 10ms;
            hls_nested on;
            hls_cleanup on;
            meta on;
            recorder chunked{
                record all;
                record_interval 15s;
                record_path /mnt/rtmp/hls/chunked;
            }
        }

    }
}

2.2.http server 配置

server {
    listen       8888;
    listen       [::]:8888;
    server_name  _;
    root         /usr/share/nginx/html;

    # hls播放地址
    location /hls {
        types{
            application/vnd.apple.mpegurl m3u8;
            video/mp2t ts;
        }
        alias /mnt/rtmp/hls/live;
        index index.html;
        add_header Cache-Control no-cache;
        add_header Access-Control-Allow-Origin *;
    }


    # web播放器index.html配置
    location /index {
        alias /mnt/rtmp/hls;
        index index.html;
        add_header Cache-Control no-cache;
        add_header Access-Control-Allow-Origin *;
    }

}

2.3.html播放器demo

以下为index.html内容,这里需要注意将index.html放在http配置的对应目录

<html>
    <head>
        <meta charset="utf-8">
        <meta name="viewport" content="width=device-width,initial-scale=1.0">
        <style type="text/css">
            html,body{margin:0px;height:100%;width:100%; display: flex; align-items: center}
            #video{
                width:100%;
                height:100%;
            
            }
        </style>
        <title>index</title>
    </head>
    <body style="margin:0;overflow:hidden">
        <video id="video" muted></video>
        <script src="https://cdn.jsdelivr.net/npm/hls.js@latest"></script>
        <script>
            function initVideo () {
                let video = document.getElementById('video')
                if(Hls.isSupported()) {
                    let hls = new Hls()
                    //该hls地址为nginx配置中的地址
                    hls.loadSource('http://xxx.xxx.xxx.xxx:8888/hls/room123.m3u8')
                    hls.attachMedia(video)
                    hls.on(Hls.Events.MANIFEST_PARSED,()=>{
                        video.play()
                    });
                }else if (video.canPlayType('application/vnd.apple.mpegurl')) {
                    video.src = 'http://xxx.xxx.xxx.xxx:8888/hls/room123.m3u8'
                    video.addEventListener('loadedmetadata',function() {
                            video.play()
                    })
                }
            }
        initVideo()
        </script>
    </body>
</html>

3.直播和在线播放

#直播推流地址
rtmp://xxx.xxx.xxx.xxx:1999/hls/room123

#rtmp播放地址,浏览器需要支持flash插件,不推荐使用
rtmp://xxx.xxx.xxx.xxx:1999/hls/room123
rtmp://xxx.xxx.xxx.xxx:1999/hls/room123.flv

#m3u8视频流url
http://xxx.xxx.xxx.xxx:8888/hls/room123.m3u8

#demo播放地址
http://xxx.xxx.xxx.xxx:8888/index/

4.直播工具

本次功能用过的直播工具如下:
1、大疆无人机
2、V导播录屏
3、OBS Studio
使用方法大同小异,推流地址填对就可以了。

posted @   BugMakerLOL  阅读(891)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)
点击右上角即可分享
微信分享提示