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

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

off999 2025-02-10 13:53 20 浏览 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

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

相关推荐

python爬取电子课本,送给居家上课的孩子们

在这个全民抗疫的日子,中小学生们也开启了居家上网课的生活。很多没借到书的孩子,不得不在网上看电子课本,有的电子课本是老师发的网络链接,每次打开网页去看,既费流量,也不方便。今天我们就利用python的...

高效办公!Python 批量生成PDF文档是如何做到的?

前言:日常办公中,经常会使用PDF文档,难免需要对PDF文档进行编辑,有时候PDF文档中的大部分内容都是一样的,只是发送对象不同。这种模板套用的场景下,使用Python进行自动化就尤为方便,用最短的时...

如何用Python将PDF完整的转成Word?

PDF文件完整的转为Word,转换后格式排版不会乱,图片等信息完整显示不丢失。这个很简单,有很多方法都可以实现。方法一:Python利用Python将PDF文件转换为Word,有许多库可以帮你实现这一...

使用Python拆分、合并PDF(python合并多个pdf)

知识点使用Python操作PDF!主要内容有:1、PDF拆分;2、PDF合并。在工作中,难免会和PDF打交道,所以掌握一点处理PDF的技能非常有必要,本文将介绍几个常用的功能。PDF拆分很多时候,获取...

10分钟实现PDF转Word神器!看DeepSeek如何用Python解放打工人

开篇痛点每个被PDF折磨过的职场人都懂——领导发来的扫描件要修改,手动抄到Word需要2小时;网上下载的报告想复制数据,却变成乱码…今天我们用Python+DeepSeek,10分钟打造一个智能转换工...

《Python知识手册》,高清全彩pdf版开放下载

Python编程还不懂?今天我要把我参与编写的这套《Python知识手册》免费分享出来,看完文末有惊喜哦。...

利用python进行数据分析,PDF文档给你答案

本书详细介绍利用Python进行操作、处理、清洗和规整数据等方面的具体细节和基本要点。虽然本书的标题是“数据分析”,重点却是Python编程、库,以及用于数据分析的工具。兄弟,毫无套路!PDF版无偿获...

OCRmypdf:一款可以让扫描PDF文件变得可搜索、可复制!

简介在日常工作中,我们经常会接触到各种PDF文件,其中不少是扫描版文档。处理这些扫描PDF时,尽管内容看似完整,但往往无法直接复制或搜索其中的文本。尤其是在需要对大量文档进行文本分析、存档或后期编辑时...

高效的OCR处理工具!让扫描PDF文件变得可搜索、可复制!

在工作中,我们常常遇到各种各样的PDF文件,其中不乏一些扫描版的文档。而在处理扫描的PDF文件时,虽然文件内容看似完整,但你却无法复制、搜索其中的文本。特别是对大量文档需要进行文本分析、存档、或者...

三步教你用Elasticsearch+PyMuPDF实现PDF大文件秒搜!

面对100页以上的大型PDF文件时,阅读和搜索往往效率低下。传统关系型数据库在处理此类数据时容易遇到性能瓶颈,而Elasticsearch凭借其强大的全文检索和分布式架构,成为理想解决方案。通过...

用 Python 去除 PDF 水印,你学会吗?

今天介绍下用Python去除PDF(图片)的水印。思路很简单,代码也很简洁。首先来考虑Python如何去除图片的水印,然后再将思路复用到PDF上面。这张图片是前几天整理《数据结构和算法...

扫描PDF档案效率提升300%!OCRmyPDF:告别无法搜索的PDF噩梦,这款26K Star的开源神器让文本识别轻松上手!

要在PDF中搜索某个关键词,结果发现啥也找不到?这种情况大多数人都遇到过吧,特别是处理扫描文档或图片PDF时。就在前几天,我还在为这事抓狂呢!后来无意中发现了OCRmyPDF这个宝藏项目...简直就...

Python自动化办公之PDF版本发票识别并提取关键信息教程(上篇)

大家好,我是皮皮。一、前言前几天在Python白银交流群【上海新年人】问了一个Python自动化办公发票数据处理的问题,一起来看看吧。二、实现过程这个问题在实际工作中还是非常常见的,实用性和通用性都比...

PDF解锁神器:用PyMuPDF与pdfplumber告别手动提取

前言大家好,今天咱们来聊聊如何用Python中的PyMuPDF和pdfplumber库,轻松提取PDF文件里的文本和元数据。你是否曾经在处理一个复杂的PDF文件时,感到信息难以触及,提取过程让人抓狂?...

《Python知识手册》,高清pdf免费获取

今天我要把我参与编写的这套《Python知识手册》免费分享出来,真正弘扬Python开源精神!手册的部分页面如下:获取方式:...

取消回复欢迎 发表评论: