百度360必应搜狗淘宝本站头条
当前位置:网站首页 > 技术资源 > 正文

openresy+nginx-rtmp-module搭建点播直播服务器

off999 2025-02-10 13:53 23 浏览 0 评论

一般点播或者直播服务器都是使用nginx-rtmp-module作为服务器,然后使用ffmpeg或者obs来进行推流,客户端使用rtmp、http-flv、hls或者dash协议拉取转码后的数据,进行播放。

网上很多编译nginx+nginx-rtmp-module的方法,但是很少有可用的openresy+nginx-rtmp-module的编译方法。本文就从模块编译开始介绍下如何搭建点播、直播服务器。

首先下载openresty源码:
https://github.com/openresty/openresty;如果仅仅编译nginx-rtmp-module,可以下载
https://github.com/arut/nginx-rtmp-module,如果想额外支持http-flv协议,可以下载
https://github.com/winshining/nginx-http-flv-module.git,后者包含了前者。

接着我们编译openresty

cd openresty/openresty-1.25.3.1 
./configure  --add-module=../../nginx-http-flv-module

然后

make
sudo make install

就完成了编译,不过实践过程中遇到了下面几个问题,值得分享下:

openresty/openresty-1.25.3.1/../../nginx-http-flv-module/hls/ngx_rtmp_hls_module.c:2059:27: error: use of undeclared identifier 'NGX_RTMP_FRAME_IDR'
    frame.key = (ftype == NGX_RTMP_FRAME_IDR);
                          ^
10 errors generated.
make[2]: *** [objs/addon/hls/ngx_rtmp_hls_module.o] Error 1
make[1]: *** [build] Error 2
make: *** [all] Error 2

出现问题的原因是我们config错误了,nginx-http-flv-module 模块中包含nginx-rtmp-module模块的功能,只需安装nginx-http-flv-module 模块即可。也就是说,下面的配置方法是错误的。

./configure --add-module=../../nginx-rtmp-module --add-module=../../nginx-http-flv-module

然后是make过程中的warning

ld: warning: dylib (/usr/local/Cellar/gcc/11.2.0/lib/gcc/11/libstdc++.dylib) was built for newer macOS version (11.3) than being linked (11.1)

设置下环境变量即可

export MACOSX_DEPLOYMENT_TARGET=11.3

然后是make install的时候遇到的

 cp: /openresty/openresty-1.25.3.1/build/resty.index: No such file or directory
make: *** [install] Error 1
build/pod: No such file or directory
make: *** [install] Error 1

上述两个问题,在网上没有找到好的解决办法,目测这俩文件对安装没有大的影响,我的解决办法是在make install之前先创建它俩。

touch build/resty.index
touch build/pod

然后问题就解决了。正确安装后展示如下:

mkdir -p /usr/local/openresty/site/lualib /usr/local/openresty/site/pod /usr/local/openresty/site/manifest
ln -sf /usr/local/openresty/nginx/sbin/nginx /usr/local/openresty/bin/openresty

然后我们运行下

 /usr/local/openresty/bin/openresty
 ps aux |grep openresty
xiazemin         28175   0.0  0.0  4268424    732 s016  S+    4:27下午   0:00.00 grep openresty
root             25010   0.0  0.0  4306592    584   ??  Ss    4:27下午   0:00.00 nginx: master process /usr/local/openresty/bin/openresty

没有报错,说明编译成功。

接着我们安装ffmpeg来进行推流,直接brew install ffmpeg会慢到让你怀疑人生。果断放弃,直接采用docker 镜像安装。

docker pull jrottenberg/ffmpeg
Using default tag: latest
latest: Pulling from jrottenberg/ffmpeg
docker.io/jrottenberg/ffmpeg:latest

接着到网上下一个mp4格式的素材到本地。至此,我们的准备工作准备完毕。

然后开始配置我们的rtmp服务器,配置文件如下:



#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 1985; #not default port 1935
        application myapp {
            live on;
                #为 rtmp 引擎设置最大连接数。默认为 off
            max_connections 1024;
             # 不开启录制
            record off;
        }


        application hls{
            live on;
            hls on;
            hls_path ./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;
        #}
        location /live {
            flv_live on;
        }


        location /hls {
           #server hls fragments
           types{
             application/vnd.apple.mpegurl m3u8;
             video/mp2t ts;
           }
           alias ./hls;
           expires -1;
           #跨域一定要放开
           add_header Access-Control-Allow-Origin *;
           add_header Access-Control-Allow-Headers X-Requested-With;
           add_header Access-Control-Allow-Methods GET,POST,OPTIONS;
        }


         # rtmp stat
        location /stat {
            rtmp_stat all;
            #rtmp_stat_format json;
            rtmp_stat_stylesheet stat.xsl;
        }
        location /stat.xsl {
            root ./html/rtmp;
        }


        # rtmp control
        location /control {
            rtmp_control all;
             add_header Access-Control-Allow-Origin *;
        }
    }


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


}

rtmp的配置和http的配置类似,包括rtmp、server、application三层。配置完毕后我们启动openresty

ps aux |grep -E "openresty|nginx" |grep -v grep |awk '{print $2}' |xargs sudo kill -9 && /usr/local/openresty/bin/openresty -p $PWD/ -c conf/nginx.conf 

接着我们启动ffmpeg进行推流

docker run -v $(pwd):$(pwd) jrottenberg/ffmpeg:latest -re -i $(pwd)/demo.mp4 -vcodec copy -f flv rtmp://host.docker.internal:1985/hls/stream

推流成功后展示如下:

[flv @ 0xae7e00] Failed to update header with correct filesize.
frame=  929 fps= 30 q=-1.0 Lsize=    7624kB time=00:00:30.93 bitrate=2018.7kbits/s speed=   1x
video:7101kB audio:484kB subtitle:0kB other streams:0kB global headers:0kB muxing overhead: 0.511305%

推流过程中如果遇到下面错误

    Metadata:
      handler_name    : Core Media Audio
rtmp://host.docker.internal:1985/zbcs/room: Input/output error

原因是推流路径不对,application hls 前缀匹配后,还需要加一层目录,对应着我们的推流密钥。也就是说下面的推流是不对的:

docker run -v $(pwd):$(pwd) jrottenberg/ffmpeg:latest -re -i $(pwd)/demo.mp4 -vcodec copy -f flv rtmp://host.docker.internal:1985/hls

推流成功后我们可以下载工具vlc来进行拉流,在vlc输入

rtmp://localhost:1985/hls/stream

就可以看到视频播放效果,为了防止推流早早结束了,影响我们测试,可以配置循环推流:

docker run -v $(pwd):$(pwd) jrottenberg/ffmpeg:latest -stream_loop -1 -re -i $(pwd)/demo.mp4 -vcodec copy -f flv rtmp://host.docker.internal:1985/hls/stream

回过头来看下我们的hls目录,可以看到下面的文件列表:

% ls ./hls 
stream-20.ts    stream-23.ts    stream-26.ts    stream-29.ts
stream-18.ts    stream-21.ts    stream-24.ts    stream-27.ts    stream.m3u8
stream-19.ts    stream-22.ts    stream-25.ts    stream-28.ts

一个stream.m3u8 和一批ts文件。转流成功后在我们之前配置的nginx rtmp模块的接收路径下(/hls)会生成m3u8索引文件,m3u8其实就是ts文件的索引,ffmpeg会把一个直播源的数据分割成很多个ts文件,访问m3u8可以获取ts文件的播放顺序,逐个播放,ts文件达到一定数量会自动删除前面无用的ts,并且如果ffmpeg停止转流,文件夹底下的文件也会自动清除。

至此我们完成了点播的推流和播放器拉流。如何进行直播的推流呢?其实很简单,我们把ffmpeg换成推流工具obs即可,和我们日常直播推流一样。obs设置推流服务器地址为

rtmp://localhost:1985/hls/stream

需要注意的是,服务器地址是

 rtmp://127.0.0.1:1985/hls/

密钥是

stream

否则会出现推流失败。至此,简单的点播和直播服务器搭建流程介绍完毕。后面详细介绍如何实现网页版的播放端推流、看端拉流、以及播放控制,敬请期待。

相关推荐

大文件传不动?WinRAR/7-Zip 入门到高手,这 5 个技巧让你效率翻倍

“这200张照片怎么传给女儿?微信发不了,邮箱附件又超限……”62岁的张阿姨对着电脑犯愁时,儿子只用了3分钟就把照片压缩成一个文件,还教她:“以后用压缩软件,比打包行李还方便!”职场人更懂这...

电脑解压缩软件推荐——7-Zip:免费、高效、简洁的文件管理神器

在日常工作中,我们经常需要处理压缩文件。无论是下载软件包、接收文件,还是存储大量数据,压缩和解压缩文件都成为了我们日常操作的一部分。而说到压缩解压软件,7-Zip绝对是一个不可忽视的名字。今天,我就来...

设置了加密密码zip文件要如何打开?这几个方法可以试试~

Zip是一种常见的压缩格式文件,文件还可以设置密码保护。那设置了密码的Zip文件要如何打开呢?不清楚的小伙伴一起来看看吧。当我们知道密码想要打开带密码的Zip文件,我们需要用到适用于Zip格式的解压缩...

大文件想要传输成功,怎么把ZIP文件分卷压缩

不知道各位小伙伴有没有这样的烦恼,发送很大很大的压缩包会受到限制,为此,想要在压缩过程中将文件拆分为几个压缩包并且同时为所有压缩包设置加密应该如何设置?方法一:使用7-Zip免费且强大的文件管理工具7...

高效处理 RAR 分卷压缩包:合并解压操作全攻略

在文件传输和存储过程中,当遇到大文件时,我们常常会使用分卷压缩的方式将其拆分成多个较小的压缩包,方便存储和传输。RAR作为一种常见的压缩格式,分卷压缩包的使用频率也很高。但很多人在拿到RAR分卷...

2个方法教你如何删除ZIP压缩包密码

zip压缩包设置了加密密码,每次解压文件都需要输入密码才能够顺利解压出文件,当压缩包文件不再需要加密的时候,大家肯定想删除压缩包密码,或是忘记了压缩包密码,想要通过删除操作将压缩包密码删除,就能够顺利...

速转!漏洞预警丨压缩软件Winrar目录穿越漏洞

WinRAR是一款功能强大的压缩包管理器,它是档案工具RAR在Windows环境下的图形界面。该软件可用于备份数据,缩减电子邮件附件的大小,解压缩从Internet上下载的RAR、ZIP及其它类...

文件解压方法和工具分享_文件解压工具下载

压缩文件减少文件大小,降低文件失效的概率,总得来说好处很多。所以很多文件我们下载下来都是压缩软件,很多小伙伴不知道怎么解压,或者不知道什么工具更好,所以今天做了文件解压方法和工具的分享给大家。一、解压...

[python]《Python编程快速上手:让繁琐工作自动化》学习笔记3

1.组织文件笔记(第9章)(代码下载)1.1文件与文件路径通过importshutil调用shutil模块操作目录,shutil模块能够在Python程序中实现文件复制、移动、改名和删除;同时...

Python内置tarfile模块:读写 tar 归档文件详解

一、学习目标1.1学习目标掌握Python内置模块tarfile的核心功能,包括:理解tar归档文件的原理与常见压缩格式(gzip/bz2/lzma)掌握tar文件的读写操作(创建、解压、查看、过滤...

使用python展开tar包_python拓展

类Unix的系统,打包文件经常使用的就是tar包,结合zip工具,可以方便的打包并解压。在python的标准库里面有tarfile库,可以方便实现生成了展开tar包。使用这个库最大的好处,可能就在于不...

银狐钓鱼再升级:白文件脚本化实现GO语言后门持久驻留

近期,火绒威胁情报中心监测到一批相对更为活跃的“银狐”系列变种木马。火绒安全工程师第一时间获取样本并进行分析。分析发现,该样本通过阿里云存储桶下发恶意文件,采用AppDomainManager进行白利...

ZIP文件怎么打开?2个简单方法教你轻松搞定!

在日常工作和生活中,我们经常会遇到各种压缩文件,其中最常见的格式之一就是ZIP。ZIP文件通过压缩数据来减少文件大小,方便我们进行存储和传输。然而,对于初学者来说,如何打开ZIP文件可能会成为一个小小...

Ubuntu—解压多个zip压缩文件.zip .z01 .z02

方法将所有zip文件放在同一目录中:zip_file.z01,zip_file.z02,zip_file.z03,...,zip_file.zip。在Zip3.0版本及以上,使用下列命令:将所有zi...

如何使用7-Zip对文件进行加密压缩

7-Zip是一款开源的文件归档工具,支持多种压缩格式,并提供了对压缩文件进行加密的功能。使用7-Zip可以轻松创建和解压.7z、.zip等格式的压缩文件,并且可以通过设置密码来保护压缩包中的...

取消回复欢迎 发表评论: