CentOS上利用nginx-rtmp-module上搭建rtmp和hls协议流媒体服务器

一:Nginx 上进行配置

1. 查找 nginx的安装目录 which nginx

[root@localhost conf]# which nginx
/usr/local/nginx/sbin/nginx
[root@localhost conf]#

2. 进入 nginx的配置目录

[root@localhost conf]# cd /usr/local/nginx/
[root@localhost nginx]# ll
总用量 36
drwx------. 2 nobody root 4096 2月  24 18:38 client_body_temp
drwxr-xr-x. 2 root   root 4096 3月  18 10:44 conf
drwx------. 2 nobody root 4096 2月  24 18:38 fastcgi_temp
drwxr-xr-x. 2 root   root 4096 2月  24 18:33 html
drwxr-xr-x. 2 root   root 4096 3月  19 11:18 logs
drwx------. 2 nobody root 4096 2月  24 18:38 proxy_temp
drwxr-xr-x. 2 root   root 4096 2月  24 18:33 sbin
drwx------. 2 nobody root 4096 2月  24 18:38 scgi_temp
drwx------. 2 nobody root 4096 2月  24 18:38 uwsgi_temp
[root@localhost nginx]#

3. 进入conf目录,修改 nginx.conf 文件内容如下

worker_processes  1;

error_log  logs/error.log debug;

events {
    worker_connections  1024;
}

rtmp {
    server {
        listen 1935;
        # RTMP 直播配置
        application rtmplive {
            live on; # 开启直播
            #hls on;# 开启HLS直播
            #hls_path /home/jack/video/m3u8File; #配置HLS m3u8 文件存放地址
            #record keyframes;
            #record_path /tmp;
            #record_max_size 128K;
            #record_interval 30s;
            #record_suffix .this.is.flv;

            #on_publish http://localhost:8080/publish;
            #on_play http://localhost:8080/play;
            #on_record_done http://localhost:8080/record_done;
        }

        # HLS 直播流配置
        application hls{
            live on;
            hls on;
            hls_path /tmp/hls;
            hls_fragment 5s; # 一个ts文件的时长 5s
        }
    }
}

http {
    server {
        listen  80;
        location /hls {
            types {
               #m3u8 type配置
              application/vnd.apple.mpegurl m3u8;
               #ts 分片文件配置
               video/mp2t ts;
            }
            #指向访问m3u8文件目录
            #alias /home/jack/video/m3u8File;
            root /tmp;
            add_header Cache-Control no-cache;#禁止缓存
        }

        location /stat {
            rtmp_stat all;
            rtmp_stat_stylesheet stat.xsl;
        }

        location /stat.xsl {
            root /lib/nginx-rtmp-module-1.2.1;
        }

        location /control {
            rtmp_control all;
        }

        #location /publish {
        #    return 201;
        #}

        #location /play {
        #    return 202;
        #}

        #location /record_done {
        #    return 203;
        #}

        location /rtmp-publisher {
            root /lib/nginx-rtmp-module-1.2.1/test;
        }
         
        # 配置nginx访问首页
        location / {
            root html;
            index index.html index.htm;
        }
    }
}

二:HLS推流和拉流

HLS推流
zfz:测试视频 zhangfengzhou$ ffmpeg -re -i hanfu_xiaohun.mp4 -vcodec libx264 -acodec aac -f flv rtmp://192.168.0.88:1935/hls/test2
HLS拉流
zfz:~ zhangfengzhou$ ffplay -i http://192.168.0.88/hls/test2.m3u8

三:RTMP推流和拉流

RTMP推流
zfz:测试视频 zhangfengzhou$ ffmpeg -re -i hanfu_xiaohun.mp4 -vcodec libx264 -acodec aac -f flv rtmp://192.168.0.88/rtmplive/test1
RTMP拉流
zfz:~ zhangfengzhou$ ffplay -i rtmp://192.168.0.88/rtmplive/test1

四:查看推流的统计数据

在浏览器中输入 http://192.168.0.88/stat 就可以查看推流的情况

五:其他问题

在有些时候进行hls拉流的时候,会遇到 HTTP error 404 Not found, 一般问题出在 tmp/hls 目录下没有m3u8文件和ts文件,尝试多次拉流,就可以正常播放了。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值