首页 小组 问答 话题 好文 素材 用户 唠叨 我的社区

[分享]CentOS使用Nginx集成rtmp搭建直播服务器

九秘Lv.1种子选手
2024-09-10 16:48:26
0
133

一、准备阶段

搞一个目录

mkdir nginx
cd nginx

安装编译工具

[root@localhost nginx]# yum install gcc-c++

安装编译依赖/工具

执行顺序不分先后

对于Ubuntu系统, pcre/zlib/openssl 这些需要单独下载源码, 在编译 nginx 的时候由 configure 命令指定源码路径, CentOS下的这种方式比较亲民

[root@localhost nginx]# yum install -y pcre pcre-devel
[root@localhost nginx]# yum install -y zlib zlib-devel
[root@localhost nginx]# yum install -y openssl openssl-devel
[root@localhost nginx]# yum install -y wget

下载 nginx源码包

先访问 http://nginx.org/download, 找到合适的版本, 然后拼接url, 使用 wget 下载

[root@localhost nginx]# wget http://nginx.org/download/nginx-1.9.9.tar.gz

下载 rtmp 模块

用于编译 nginx 源代码的时候编译进去

[root@localhost nginx]# wget https://github.com/arut/nginx-rtmp-module/archive/master.zip

二、执行阶段

解压 nginx-rtmp 包

[root@localhost nginx]# unzip master.zip

解压 nginx 源码包

[root@localhost nginx]# tar -zxvf nginx-1.9.9.tar.gz

开始编译

[root@localhost nginx]# cd nginx-1.9.9
[root@localhost nginx]# ./configure --add-module=../nginx-rtmp-module-master
[root@localhost nginx]# make
[root@localhost nginx]# make install

配置 nginx

这一步 cd 的目录由上一步的 ./configure 应用默认指定, 如果想指定其他目录, 使用 --prefix 参数 (./configure --prefix="$HOME/build_path")

[root@localhost nginx]# cd /usr/local/nginx/sbin
[root@localhost nginx]# ./nginx

修改 nginx.conf

[root@localhost nginx]# cd ../conf
[root@localhost nginx]# vi nginx.conf
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;
        application live {
            live on;
            allow publish all; # control access privilege
            allow play all; # control access privilege
        }

        application hls {
            live on;
            hls on;
            hls_path /tmp/hls;
            wait_key on;               #对视频切片进行保护,这样就不会产生马赛克了。 
            hls_fragment 10s;          #每个视频切片的时长。 
            hls_playlist_length 60s;   #总共可以回看的事件,这里设置的是1分钟。 
            hls_continuous on;         #连续模式。 
            hls_cleanup on;            #对多余的切片进行删除。 
            hls_nested on;             #嵌套模式。
        }

    application vod {
            play /tmp/video;
        }
    }
}

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

        location /hls {
            types {
                application/vnd.apple.mpegurl m3u8;
                video/mp2t ts;
            }
            root /tmp;
            add_header Cache-Control no-cache;
        }

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

}
reload nginx 配置
[root@localhost nginx]# /usr/local/nginx/sbin/nginx -s reload

查看监听

配置文件中, rtmp模块增加了对 1935 端口的监听, 用于 rtmp 协议的推流和拉流

[root@localhost nginx]# netstat -ltn
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State      
tcp        0      0 0.0.0.0:6379            0.0.0.0:*               LISTEN     
tcp        0      0 0.0.0.0:1935            0.0.0.0:*               LISTEN

至此服务端的工作完成

三、测试阶段

安装 FFmpeg 工具

这是一个格式转换工具, 也可以把转换后的内容推到某个 rtmp 地址

[root@localhost nginx]# yum install epel-release -y
[root@localhost nginx]# rpm --import http://li.nux.ro/download/nux/RPM-GPG-KEY-nux.ro

访问 http://li.nux.ro/download/nux/dextop/el7/x86_64, 我们要下载 nux-dextop-release, 选择一个合适的版本(我是用最高版本)

[root@localhost nginx]# rpm -Uvh http://li.nux.ro/download/nux/dextop/el7/x86_64/nux-dextop-release-0-5.el7.nux.noarch.rpm
[root@localhost nginx]# yum install ffmpeg ffmpeg-devel -y

推流

新建一个目录, 在里面放几个视频文件, 使用 ffmpeg 命令开始推, -i 参数指定了本地的视频文件, -q 参数指定了推送的地址

[root@localhost nginx]# ffmpeg -re -i /root/video/Yin_Yue_Tai.mp4 -vcodec libx264 -vprofile baseline -acodec aac -ar 44100 -strict -2 -ac 1 -f flv -s 1280x720 -q 10 rtmp://127.0.0.1:1935/live/test

此时视频文件就被推送到了 nginx.

拉流

地址: rtmp:{IP}:1935/live/test
测试的话, IOS 上可以下载 易推流, 这个应用支持拉流和推流;

Android 端没有去找拉流(播放)的工具, 只找到一个叫 杏林推流 的推流app;

测试的时候统一使用这个地址, Android 端使用推流工具录制, IOS端使用拉流工具观看。

或者使用上面的 ffmpeg 命令推流, 手机端拉流观看。

(完)

九秘
九秘

42 天前

签名 : 我常驻在>https://www.huaqu.club/ask   133       0
评论
站长交流