Nginx 推流 拉流 --- 点播直播

1. 准备环境

  1. 安装操作系统Cenos
  2. 配置yum源
    yum:https://developer.aliyun.com/mirror/
  3. Nginx依赖
    gcc-c++ zlib pcre openssl openssl-devel
  4. Nginx和扩展模块
    Nginx本身只是一个服务器,对流媒体并没有支持,所以我们要下载对应的模块来扩展其功能
    MP4模块:http://h264.code-shop.com/download/nginx_mod_h264_streaming-2.2.7.tar.gz
    FLV模块:http://sourceforge.net/projects/yamdi/files/yamdi/yamdi-1.9.tar.gz
    直播流模块:https://github.com/adwpc/nginx-rtmp-module
    FastDFS模块:https://github.com/happyfish100/fastdfs-nginx-module
  5. FFMPEG
    ffmpeg :https://ffmpeg.zeranoe.com/builds/
  6. OBS推流
  7. EV拉流

2. 安装环境

2.1 上传所有模块到系统中

mkdir /opt/nginx
cd /opt/nginx
rz nginx-1.8.1.tar.gz
rz nginx_mod_h264_streaming-2.2.7.tar.gz
rz yamdi-1.9.tar.gz
rz nginx-rtmp-module-master.zip

2.2 安装FLV模块

tar -zxvf yamdi-1.9.tar.gz
cd yamdi
./configure
mark && make install

2.3 解压MP4模块

tar -zxvf nginx_mod_h264_streaming-2.2.7.tar.gz
处理一个bug
cd /opt/nginx/nginx_mod_h264_streaming/src
注释157行-161行

     /* TODO: Win32 */
 // if (r->zero_in_uri)
 // {
 //   return NGX_DECLINED;
 // }

2.4 解压nginx

tar -zxvf nginx-1.8.1.tar.gz
tar -zxvf nginx_mod_h264_streaming.tar.gz
unzip nginx-rtmp-module-master.zip

2.5 安装nginx并添加模块

./configure --add-module=../nginx_mod_h264_streaming --add-module=../nginx-rtmp-module-master --with-http_ssl_module --prefix=/opt/software/nginx --with-http_flv_module --with-http_stub_status_module
注:如果编译报错, vim objs/Makefile (修改objs/Makefile文件, 去掉其中的"-Werror"), 然后就能够正常编译了.

2.6 播放配置

cd /opt/software/nginx/conf
vim nginx.conf

添加对FLV和MP4的支持

location ~ \.flv {
            flv;
}
location ~ \.mp4$ {
             mp4;
}

2.7 FFmpeg

FFmpeg是一套可以用来记录、转换数字音频、视频,并能将其转化为流的开源计算机程序
上传

rz ffmpeg-4.2.2.tar.bz2

安装

yum install bzip2 #因为后缀是bz2的 要安装 bzip2
tar -jxvf ffmpeg-4.2.2.tar.bz2 #使用 -jxvf 解压bz2文件
cd ffmpeg-4.2.2 #进入目录
./configure --prefix=/opt/software/ffmpeg #配置 设置安装路径
make && make install #安装

3. 点播

上传测试MP4

cd /opt/software/nginx/html
rz test.mp4

查看 http://192.168.3.9/test.mp4

4. 直播

4.1配置服务

cd /opt/software/nginx/conf
vim nginx.conf

rtmp {
    server {
        listen 1935;  #监听的端口
        chunk_size 4096;   
        application hls {  #rtmp推流请求路径  
            live on;    
            hls on;    
            hls_path /opt/software/nginx/html/hls;    
            hls_fragment 5s;    
        }    
    }    
}  

mkdir /opt/software/nginx/html/hls

4.2 直播推流

设置场景

场景增加视频捕获设备,显示器捕获或窗口捕获
注:窗口黑屏解决方案
设备管理器中-->显示适配器-->独立显卡-禁用

设置推流地址

rtmp服务器:rtmp://192.168.3.9:1935/hls/
密钥:12345

4.3 直播拉流

借用EV播放

输入拉流地址

rtmp地址:rtmp://192.168.3.9:1935/hls/12345

点击开始观看直播

猜你喜欢

转载自www.cnblogs.com/cjq10029/p/12469239.html