当前位置:首页 > Nginx
使用 nginx 搭建 rtmp 视频点播、直播服务器
来源:靑龍一笑的博客  作者:靑龍一笑  发布时间:2021-11-02 14:42:20  点击量:547  评论:0

一、安装 nginx

    首先,在安装 nginx 的时候,需要先将 nginx-rtmp-module-master 模块编译进去。
    有关 nginx 的安装和配置,详见我的另外两篇文章《在 Linux 系统下安装和配置 Nginx·中篇》、《在 Linux 系统下安装和配置 Nginx·下篇》。
    对于 CentOS 7 以上版本的操作系统,可以使用 systemctl 配置系统服务,详见我的另一篇文章《CentOS 7 使用 systemctl 配置系统服务》。

[root@RicenOS softwares]# cat /usr/lib/systemd/system/nginx.service
[Unit]
Description=The tengine HTTP and reverse proxy server
After=syslog.target network.target remote-fs.target nss-lookup.target

[Service]
Type=forking
PIDFile=/data/nginx/logs/nginx.pid
ExecStartPre=/data/nginx/sbin/nginx -t -c /data/nginx/conf/nginx.conf
ExecStart=/data/nginx/sbin/nginx -c /data/nginx/conf/nginx.conf
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s TERM $MAINPID
PrivateTmp=false

[Install]
WantedBy=multi-user.target

二、安装 yasm

[root@RicenOS softwares]# tar xvf yasm-1.3.0.tar.gz
[root@RicenOS softwares]# cd yasm-1.3.0
[root@RicenOS yasm-1.3.0]# ./configure
[root@RicenOS yasm-1.3.0]# make
[root@RicenOS yasm-1.3.0]# make install
[root@RicenOS yasm-1.3.0]# cd ..

三、安装 x264

[root@RicenOS softwares]# tar xvf x264-snapshot-20191217-2245.tar.bz2
[root@RicenOS softwares]# cd x264-snapshot-20191217-2245
[root@RicenOS x264-snapshot-20191217-2245]# ./configure --enable-shared --enable-static --disable-asm
[root@RicenOS x264-snapshot-20191217-2245]# make
[root@RicenOS x264-snapshot-20191217-2245]# make install
[root@RicenOS x264-snapshot-20191217-2245]# cd ..
[root@RicenOS softwares]# sed -i '1i/usr/local/lib' /etc/ld.so.conf
[root@RicenOS softwares]# ldconfig

四、安装 ffmpeg

[root@RicenOS softwares]# tar xvf ffmpeg-4.4.1.tar.gz
[root@RicenOS softwares]# cd ffmpeg-4.4.1
[root@RicenOS ffmpeg-4.4.1]# ./configure --prefix=/opt/ffmpeg \
> --enable-shared --enable-gpl --enable-libx264 --enable-pthreads
[root@RicenOS ffmpeg-4.4.1]# make
[root@RicenOS ffmpeg-4.4.1]# make install
[root@RicenOS ffmpeg-4.4.1]# cd ..
[root@RicenOS softwares]# sed -i '1i/opt/ffmpeg/lib' /etc/ld.so.conf
[root@RicenOS softwares]# ldconfig

    查看 ffmpeg 的版本信息:

[root@RicenOS softwares]# cd /opt/ffmpeg/bin/
[root@RicenOS bin]# ./ffmpeg -version

五、配置 nginx

    修改 nginx 的配置文件 nginx.conf,添加以下代码:

rtmp {
    server {
        listen 1935;
        chunk_size 4096;

        # 点播
        application vod {
            # 视频存放目录(必须是绝对路径)
            play /data/websites/vod;
        }

        # 直播
        application live {
            live on;
        }

        # 推流
        application push {
            live on;
            # 推流到 live 直播应用
            push rtmp://www.ricen.net/live;
        }

    }
}

    注意,上述代码在 nginx.conf 文件中与 http 是同级的。
    nginx 的 rtmp 模块有个统计功能,如果想要使用,需要在站点配置文件中添加,例如:

server {
    listen 80;
    server_name www.ricen.net;
    access_log logs/127.0.0.1_access.log main;
    error_log logs/127.0.0.1_error.log error;

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

    # 配置 rtmp 统计功能
    location /stat {
        rtmp_stat all;
        rtmp_stat_stylesheet stat.xsl;
    }

    location /stat.xsl {
        root /data/websites;
    }

}

    另外,还需要将 nginx-rtmp-module-master 模块中的 stat.xsl 拷贝到 /data/websites 目录下。当然,也可以直接将 stat.xsl 指向到 nginx-rtmp-module-master 模块的目录下。

六、点播与直播

    假设,在站点的视频目录 /data/websites/vod 下,存在 01.mp4 的视频文件。那么,点播的 rtmp 地址为:rtmp://www.ricen.net:1935/vod/01.mp4
    这里的域名 www.ricen.net 也可以用服务器 IP 来代替,由于 1935 端口是 rtmp 协议的默认端口,是可以省略的。
    如果是来自摄像头的直播,推流地址为:

rtmp://www.ricen.net/push

    如果是本地视频直播,需要使用如下命令转成 rtmp 直播流:

[root@RicenOS bin]# ./ffmpeg -re -i /data/websites/vod/01.mp4 -vcodec libx264 -profile:v baseline -acodec aac -ar 44100 -strict -2 -ac 1 -f flv -s 1280x720 -q 10 rtmp://www.ricen.net/live/01

    其中:

-re:将输入的读取速度降低到输入的本地帧速率,对于实时输出很有用
-i:指定输入的文件
-vcodec:指定视频编码
baseline:基本画质
-acodec:指定音频编码
-ar:指定音频采样率
-strict -2:为了使用 aac 音频编码
-ac:设置声道数
flv:rtmp的视频格式
-s:设置视频分辨率

    同样是 /data/websites/vod/01.mp4,作为直播流,其地址是:rtmp://www.ricen.net:1935/live/01

版权所有 © 2005-2023 靑龍一笑的博客  Powered by C.S.Ricen
Copyright © 2005-2023 by www.ricensoftwares.com.cn  All Rights Reserved.

欢迎光临本站,这里是靑龍一笑的博客。

因资金匮乏,本站已迁到国外的免费空间,可能导致本站的访问速度较慢,由此给您带来的不便,敬请谅解。

您可以通过下方的“支持本站建设”链接,给本站提供资金支持。

Free Web Hosting