it编程 > 数据库 > Redis

nginx+rtmp实现直播完整流程

31人参与 2026-01-11 Redis

一,环境准备

1.下载nginx-rtmp-module:

cd /www/server/
git clone https://github.com/arut/nginx-rtmp-module.git

2.nginx安装:

这是用了宝塔哈。

软件商店 > 应用搜索:nginx > 安装 > 编译安装 > 添加自定义模块
模块名称:nginx_rtmp_module
描述: nginx rtmp
参数:–add-module=/www/server/nginx-rtmp-module

3. 编辑conf配置:

​ 编辑nginx.conf引入配置: include /www/server/nginx/conf/rtmp/nginx-rtmp.conf;

​ 引入配置:include /www/server/nginx/conf/rtmp/nginx-rtmp-play.conf;

4. 重启nginx:

注意放行端口:1935和1000:

5. 使用obs推流:

live推流:

推流地址:rtmp://ip:1935/live,串流密钥:test123

浏览器访问:http://ip:1000/stat可以看到推流的情况:

使用potplayer拉流播放:

点击右上角的potplayer,选择“打开>打开链接”,输入rtmp://ip:1935/live/test123后,点击”确定“。

hls推流:

推流地址:rtmp://ip:1935/hls,串流密钥:test123

使用potplayer拉流播放:

点击右上角的potplayer,选择“打开>打开链接”,输入http://ip:1000/hls/test123.m3u8后,点击”确定“。

6. 扩展:浏览器拉流播放:

  1. html播放器代码index.html(使用video.js):
<!doctype html>
<html lang="zh-cn">

<head>
  <meta charset="utf-8">
  <title>html5 直播</title>
  <link href="https://vjs.zencdn.net/7.0.3/video-js.css" rel="external nofollow"  rel="stylesheet">
  <script src="https://vjs.zencdn.net/7.0.3/video.js"></script>
  <script src="https://cdn.jsdelivr.net/npm/videojs-flash@2/dist/videojs-flash.min.js"></script>
</head>

<body style="margin: auto; width: 1080px;">
  <!-- rtmp直播拉流地址 -->
  <video id="rtmp-live" class="video-js vjs-default-skin vjs-big-play-centered" controls preload="auto" width="1080" height="608" data-setup='{}'>
    <source src="rtmp://【ip】/live/test123" type="rtmp/flv">
  </video>
  <hr />
  <!-- http直播hls拉流地址 -->
  <video id="hls-live" class="video-js vjs-default-skin vjs-big-play-centered" controls preload="auto" width="1080"height="608" data-setup='{}'>
    <source src="http://【ip】:1000/hls/test123.m3u8" type="application/x-mpegurl">
  </video>
</body>

</html>
  1. 因为本例的html需要在服务中打开,本例采用node, 所以在同一目录下建立server.js:
    (提示:不使用node启动服务可以在idea中直接将index.html以服务run运行)
var http = require('http');
// 导入文件读写的js
var fs = require('fs');
var server = http.createserver(function (request, response) {
    console.log('someone has visited my first node server !');
    //根据访问的路径来选择响应的文件
    let filepath;
    if (request.url === '/') {
        filepath='index.html';
    } else {
        filepath='notfound.html';
    }
    //读取文件并写入响应内容中去
    fs.readfile(filepath,function(err,data ){
        response.write(''+data);
        //不能直接写data 是16进制的数,需要转成字符串
        //我写data.tostring() 会报错
        response.end();
    }) 
})
server.listen(8000, function () {
    console.log('server started at http://localhost:8000/  ......')
});

提示:使用chrome播放rtmp流的时候需要允许flash执行,而使用hls播放m3u8则不用

命令安装rtmp模块

下载并解压 rtmp 模块

cd /www/server/nginx/src/
git clone https://github.com/arut/nginx-rtmp-module.git

安装 pcre

sudo apt update
sudo apt install libpcre3 libpcre3-dev
cd /www/server/nginx/src/
wget https://ftp.pcre.org/pub/pcre/pcre-8.43.tar.gz
tar -xzvf pcre-8.43.tar.gz
cd pcre-8.43
./configure
make
sudo make install

确保 nginx 配置正确

cd ~/nginx-1.24.0/
./configure --user=www --group=www --prefix=/www/server/nginx \
--add-module=/www/server/nginx/src/ngx_devel_kit \
--add-module=/www/server/nginx/src/lua_nginx_module \
--add-module=/www/server/nginx/src/ngx_cache_purge \
--with-openssl=/www/server/nginx/src/openssl \
--with-pcre=/www/server/nginx/src/pcre-8.43 \
--with-http_v2_module \
--with-stream \
--with-stream_ssl_module \
--with-stream_ssl_preread_module \
--with-http_stub_status_module \
--with-http_ssl_module \
--with-http_image_filter_module \
--with-http_gzip_static_module \
--with-http_gunzip_module \
--with-ipv6 \
--with-http_sub_module \
--with-http_flv_module \
--with-http_addition_module \
--with-http_realip_module \
--with-http_mp4_module \
--add-module=/www/server/nginx/src/ngx_http_substitutions_filter_module-master \
--with-ld-opt=-wl,-e \
--with-cc-opt=-wno-error \
--with-http_dav_module \
--add-module=/www/server/nginx/src/nginx-dav-ext-module \
--add-module=/www/server/nginx/src/nginx-rtmp-module \
--with-ld-opt="-wl,-rpath,/usr/local/lib/"

安装

make
make install

检查是否成功,在输入下方命令后查找是否安装完成trmp模块

nginx -v

如果提示没有找到luajit查看是否安装

luajit -v

如果已经安装查找安装目录

whereis luajit

根据目录设置环境变量

export luajit_lib=/usr/local/lib
export luajit_inc=/usr/local/include/luajit-2.1

到此这篇关于nginx+rtmp实现直播完整流程的文章就介绍到这了,更多相关nginx rtmp 直播内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!

(0)

您想发表意见!!点此发布评论

推荐阅读

Redis的主从同步问题的一些解决方法

01-11

Redis实现分布式限流的几种方法

01-11

基于Redis实现登录功能思路详解(手机号+验证码)

01-08

Redis中ziplist与quicklist解析与对比小结

01-07

Redis热点Key独立集群实现方案(核心思路)

01-07

Redis安全高效删除包含特定模式的所有键值对的完整方案

01-13

猜你喜欢

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论