那年那日那朵花

".......(o´ω`o)......"

搭建rtmp流媒体服务器

2017-02-11 13:50 linux

情况

昨天朋友问我是否会用nginx搭建rtmp流媒体服务器,当时我是不会的。正好有些时间就研究了下,现记录下来帮助自己学习。

开始操作了

上网查了下这个不是很难,搭建这个免费开源的流媒体服务器主要是用到了nginx和nginx-rtmp-module。

先下载并解包

wget https://github.com/arut/nginx-rtmp-module/archive/v1.1.10.tar.gz
wget http://nginx.org/download/nginx-1.10.3.tar.gz

如果下面编译安装的时候报错缺少模块的话 yum 安装对应模块,例如下面
yum install pcre-devel

编译安装

cd /path/to/nginxinstall
./configure --prefix=/opt/nginx/ --add-module=../nginx-rtmp-module-1.1.10
make
make install

安装完毕后,修改nginx配置文件

worker_processes  1;

events {
    worker_connections  1024;
}


http {
    include       mime.types;
    default_type  application/octet-stream;


    sendfile        on;

    keepalive_timeout  65;


    server {
        listen       80;
        server_name  localhost;


        location / {
            root   html;
            index  index.html index.htm;
        }

        #HLS
        location /hls {
            types {
                application/vnd.apple.mpegurl m3u8;
                video/mp2t ts;
            }
            root   /data;
            add_header Cache-Control no-cache;
        }


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

        location /stat.xsl {
            root html;

        }

        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }

}

rtmp { 
    server { 
        listen 1935; 
        # publish_time_fix off;

        #rtmp
        application livetest{
            live on;
            max_connections 1024;
        }

        #hls
        application live { 
            live on; #stream on live allow 
            record off; 
            hls on; 
            hls_path /data/hls; 
       } 
    }

}

把监控rtmp的stat.xsl复制到nginx的html目录中
cp ../nginx-rtmp-module-1.1.10/stat.xsl /opt/nginx/html/
然后启动nginx。

然后准备安装ffmpeg做推流拉流
我准备用yum安装,这样方便点,就是版本会比较低

cd /etc/yum.repos.d
vi dag.repo

[dag]
name=Dag RPM Repository for Red Hat Enterprise Linux
baseurl=http://archive.cs.uu.nl/mirror/dag.wieers/redhat/el$releasever/en/$basearch/dag
        http://apt.sw.be/redhat/el$releasever/en/$basearch/dag 
        http://dag.linux.iastate.edu/dag/redhat/el$releasever/en/$basearch/dag
        http://ftp.riken.jp/Linux/dag/redhat/el$releasever/en/$basearch/dag/
        http://ftp.heanet.ie/pub/freshrpms/pub/dag/redhat/el$releasever/en/$basearch/dag
        http://ftp-stud.fht-esslingen.de/dag/redhat/el$releasever/en/$basearch/dag
        http://mirror.cpsc.ucalgary.ca/mirror/dag/redhat/el$releasever/en/$basearch/dag
        http://mirrors.ircam.fr/pub/dag/redhat/el$releasever/en/$basearch/dag
        http://rh-mirror.linux.iastate.edu/pub/dag/redhat/el$releasever/en/$basearch/dag
        http://rpmfind.net/linux/dag/redhat/el$releasever/en/$basearch/dag
        http://wftp.tu-chemnitz.de/pub/linux/dag/redhat/el$releasever/en/$basearch/dag
        http://www.mirrorservice.org/sites/apt.sw.be/redhat/el$releasever/en/$basearch/dag
gpgcheck=0
enabled=1

yum install ffmpeg ffmpeg-devel

推流,我在网上找了个免费的源流rtmp://live.hkstv.hk.lxdns.com/live/hks
ffmpeg -i rtmp://live.hkstv.hk.lxdns.com/live/hks -vcodec copy -acodec copy -f flv rtmp://127.0.0.1:1935/live/test

测试

可以下载vlc播放器测试,做串流播放测试,也可以用其他能播放器。
可以直接用rtmp协议播放
rtmp
也可以用hls协议直接通过http请求播放
hls

查看本地分片

cd /data/hls/
[hu@servertest01 hls]$ tree
.
├── test-0.ts
├── test-10.ts
├── test-11.ts
├── test-12.ts
├── test-1.ts
├── test-2.ts
├── test-3.ts
├── test-4.ts
├── test-5.ts
├── test-6.ts
├── test-7.ts
├── test-8.ts
├── test-9.ts
└── test.m3u8

0 directories, 14 files

test.m3u8就是http访问时的文件
m3u8文件是动态加载的索引文件,而具体的视频数据是ts文件。

Cloudhu 个人随笔|built by django|

沪ICP备16019452号-1