前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Nginx 搭建RTMP视频点播 直播 HLS服务器

Nginx 搭建RTMP视频点播 直播 HLS服务器

作者头像
剧终
发布2020-12-15 11:54:58
5K0
发布2020-12-15 11:54:58
举报
文章被收录于专栏:Linux学习日志Linux学习日志

安装Nginx

代码语言:javascript
复制
--下载nginx-rtmp-module模块
git clone https://github.com/arut/nginx-rtmp-module.git

--安装依赖
yum install -y wget gcc gcc-c++ make pcre pcre-deve zilib zlib-devel openssl-devel

--下载Nginx源码包并解压
wget http://nginx.org/download/nginx-1.18.0.tar.gz
tar zxvf nginx-1.18.0.tar.gz
cd nginx-1.18.0

--编译安装
./configure --prefix=/usr/local/nginx --with-http_ssl_module --with-http_gzip_static_module --with-http_stub_status_module --add-module=/nginx-rtmp-module/
make && make install

点播视频服务的配置

代码语言:javascript
复制
--编辑主配置文件
vim /usr/local/nginx/conf/nginx.conf
worker_processes  1;
events {
    worker_connections  1024;
}
include /usr/local/nginx/conf.d/*.conf;
http {
    include       mime.types;
    default_type  application/octet-stream;
}

--编辑子配置文件
vim /usr/local/nginx/conf.d/default.conf
rtmp {
  server {
  listen 8080;  #设置端口号
  chunk_size 4096;  #设置数据传输块的大小

application live {  
  play /usr/local/nginx/html/live;  #设置视频文件的位置
}
}
}

--上传一个视频文件
[root@localhost nginx]# ls /usr/local/nginx/html/live/
ckwlq.mp4

--重载配置文件
/usr/local/nginx/sbin/nginx -s reload
打开VLC软件

直播服务的配置

代码语言:javascript
复制
--编辑配置文件
vim /usr/local/nginx/conf.d/default.conf   #添加直播服务配置
rtmp {
  server {
  listen 8080;
  chunk_size 4096;

application video {    #配置直播服务
   live on;
}
application live {
  play /usr/local/nginx/html/live;
}
}
}

--重载配置文件
/usr/local/nginx/sbin/nginx -s reload

安装OBS工具

代码语言:javascript
复制
由于我用的是deepin系统,所以这里只演示deepin系统下的操作,windows系统百度下载OBS软件即可

--安装ffmpeg
sudo apt install ffmpeg

--安装OBS
sudo add-apt-repository ppa:obsproject/obs-studio
sudo apt-get update
sudo apt install obs-studio

--运行OBS
root@linux:/# obs

使用OBS进行直播

代码语言:javascript
复制
点击右上角文件在点击设置
代码语言:javascript
复制
点击来源的 + 号选择 `屏幕捕抓(XSHM)`
代码语言:javascript
复制
点击开始推流
代码语言:javascript
复制
设置VLC播放的地址 rtmp://172.16.0.160:8080/video

实时回放视频服务

代码语言:javascript
复制
--编辑主配置文件
vim /usr/local/nginx/conf/nginx.conf
worker_processes  1;
events {
    worker_connections  1024;
}
include /usr/local/nginx/conf.d/*.conf;
http {
    include       mime.types;
    default_type  application/octet-stream;
server {
  listen 80;
  server_name localhost;

 location /live {   #这里也是需要添加的字段
   types {  
   application/vnd.apple.mpegurl m3u8;  
   video/mp2t ts;  
}
  alias /usr/local/nginx/html/live;   
  expires -1;
  add_header Cache-Control no-cache;  
}  
}
}


--编辑子配置文件
vim /usr/local/nginx/conf.d/default.conf   #添加直播服务配置
rtmp {
  server {
  listen 8080;
  chunk_size 4096;

application live {    #配置直播服务
   live on;
   hls on;   #设置回放视频服务
   hls_path  /usr/local/nginx/html/live;  #设置切片视频文件存放位置
   wait_key on;   #对视频切片进行保护,这样就不会产生马赛克了
   hls_fragment 10s;  #设置HLS 分段长度
   hls_playlist_length 60s;  #总共可以回看的事件,这里设置的是1分钟
   hls_continuous on; #连续模式
   hls_cleanup on;    #对多余的切片进行删除
   hls_nested on;     #嵌套模式
}
application video {
  play /usr/local/nginx/html/video;
}
}
}

--创建切片视频文件存放文件夹
[root@host1 /]# mkdir /usr/local/nginx/html/live/

--重启nginx
/usr/local/nginx/sbin/nginx -s reload
代码语言:javascript
复制
使用obs设置提流地址进行直播
验证
代码语言:javascript
复制
--查看是否产生切片视频文件
[root@host1 html]# ls live/
0.ts  1.ts  2.ts  3.ts  4.ts  5.ts  index.m3u8

数据统计模块

代码语言:javascript
复制
数据统计模块是http 模块,因此统计命令应该位于http 模块中
代码语言:javascript
复制
--编辑主配置文件,添加以下配置
[root@host1 /]# vim /usr/local/nginx/conf/nginx.conf
location /livestatus {
  rtmp_stat all;
  rtmp_stat_stylesheet stat.xsl; #将流媒体的状态全部记录到'stat.xsl'中
}
location /stat.xsl {
  root /nginx-rtmp-module/;
}

--重载配置文件
/usr/local/nginx/sbin/nginx -s reload
验证
代码语言:javascript
复制
浏览器访问'http://172.16.0.160/livestatus'
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 安装Nginx
    • 点播视频服务的配置
      • 打开VLC软件
    • 直播服务的配置
      • 安装OBS工具
        • 使用OBS进行直播
          • 实时回放视频服务
            • 验证
          • 数据统计模块
            • 验证
        相关产品与服务
        云直播
        云直播(Cloud Streaming Services,CSS)为您提供极速、稳定、专业的云端直播处理服务,根据业务的不同直播场景需求,云直播提供了标准直播、快直播、云导播台三种服务,分别针对大规模实时观看、超低延时直播、便捷云端导播的场景,配合腾讯云视立方·直播 SDK,为您提供一站式的音视频直播解决方案。
        领券
        问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档