ngxin-rtmp-module 搭建及rtmp & hls 配置簡單分享

安裝

nginx

#Nginx安裝網上一搜一堆,其實就是下載nginx後 tar zxvf解壓然後./configure 看看缺什麼,然後yum imstanll什麼。
最後省事一些註冊到系統服務(/etc/init.d/nginx 配置文件網上一堆)
#基礎組件
yum install -y pcre pcre-devel  
yum install -y zlib zlib-devel  
yum install -y openssl openssl-devel  
yum install -y wget
#下載與安裝
wget nginx.tar
tar zxvf nginx.tar
./configure
make & make install
#註冊到系統服務
# Source function library. 
. /etc/rc.d/init.d/functions 
# Source networking configuration. 
. /etc/sysconfig/network 
# Check that networking is up. 
[ "$NETWORKING" = "no" ] && exit 0 
nginx="/usr/local/nginx/sbin/nginx" 
prog=$(basename $nginx) 
NGINX_CONF_FILE="/usr/local/nginx/conf/nginxlive.conf" <----這個配置文件隨意改成你要的
[ -f /etc/sysconfig/nginx ] && . /etc/sysconfig/nginx 
lockfile=/var/lock/subsys/nginx 
start() { 
    [ -x $nginx ] || exit 5 
    [ -f $NGINX_CONF_FILE ] || exit 6 
    echo -n $"Starting $prog: " 
    daemon $nginx -c $NGINX_CONF_FILE 
    retval=$? 
    echo 
    [ $retval -eq 0 ] && touch $lockfile 
    return $retval 
} 
stop() { 
    echo -n $"Stopping $prog: " 
    killproc $prog -QUIT 
    retval=$? 
    echo 
    [ $retval -eq 0 ] && rm -f $lockfile 
    return $retval 
killall -9 nginx 
} 
restart() { 
    configtest || return $? 
    stop 
    sleep 1 
    start 
} 
reload() { 
    configtest || return $? 
    echo -n $"Reloading $prog: " 
    killproc $nginx -HUP 
RETVAL=$? 
    echo 
} 
force_reload() { 
    restart 
} 
configtest() { 
$nginx -t -c $NGINX_CONF_FILE 
} 
rh_status() { 
    status $prog 
} 
rh_status_q() { 
    rh_status >/dev/null 2>&1 
} 
case "$1" in 
    start) 
        rh_status_q && exit 0 
    $1 
        ;; 
    stop) 
        rh_status_q || exit 0 
        $1 
        ;; 
    restart|configtest) 
        $1 
        ;; 
    reload) 
        rh_status_q || exit 7 
        $1 
        ;; 
    force-reload) 
        force_reload 
        ;; 
    status) 
        rh_status 
        ;; 
    condrestart|try-restart) 
        rh_status_q || exit 0 
            ;; 
    *)    
      echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload|configtest}" 
        exit 2 
esac  

#賦予權限
chmod 755 /etc/init.d/nginx
#添加到服務
chkconfig --add nginx
#開機啓動
chkconfig --level 345 nginx on

nginx-rtmp-module

#安裝此組件到nginx 
nginx路徑下
./configure --add-module=你的路徑/nginx-rtmp-module --with-http_ssl_module --with-debug
make & make install

nginx-rtmp-module Config

#----參考資料
#----https://github.com/arut/nginx-rtmp-module
#----https://github.com/arut/nginx-rtmp-module/wiki/Directives

worker_processes  1; 
events {
    worker_connections  1024;
}
#rtmp協議頭 監聽1935端口 暴露application作爲domain來接入
#有了這個纔可以通過 rtmp://ip:post/domain/streamKey 進行推拉
    rtmp { 
        server {     
            #========================
            #========RTMP配置========
            #========================
            #監聽端口,記得設置你的防火牆iptables
            listen 1935;   
            #網絡流塊大小,數字越大服務器壓力越小,MIN:128
            chunk_size 4096;  
            #超時默認60s
            timeout 60s;
            #保活ping可以理解爲心跳
            #ping爲主動發起心跳,ping_timeout爲對方30s無響應
            ping 3m;
            ping_timeout 30s;
            #緩衝區長度默認1000ms
            buflen 5000ms;
            application liveOnline {  
                #開啓RTMP直播
                live on;
                #最大直播連接數
                max_connections 1024;
                #========================
                #========HLS配置=========
                #======================== 
                #一定要開啓HLS否則無法使用m3u8拉流
                #開啓HLS直播
                hls on;
                #HLS的m3u8文件保存路徑
                hls_path /tmp/liveOnlineHLS;
                #每隔N秒保存1次  
                hls_fragment 1s; 
                #繼續播放位置
                hls_continuous on;
                #hls播放列表長度
                hls_playlist_length 3s;  
                #爲了防止音視頻不同步每隔n毫秒進行同步
                hls_sync 50ms;
                #開啓hls加密
                #hls_keys on;
                #加密文件路徑
                #hls_key_path /tmp/hlskeys;   
                #共用同一個key的視頻切片數
                #hls_fragments_per_key 10;
                #訪問地址
                #hls_key_url http://127.0.0.1/hlskeys/;
                #舊片段保存默認關
                #hls_continuous on;
                #將ts碎片文件根據不同str eamkey放到子目錄中
                #hls_nested on;
                #============================
                #========錄製配置低配========
                #============================
                #我們可以通過控制中心來開啓錄製和關閉錄製,可以有若干個錄製配置
                recorder base {  #啓用錄製
                    record all manual;  #手動控制錄製啓停
                    record_suffix _record.flv; # _record結尾
                    record_interval 10s; #每隔n秒寫一次 0是無間隔
                    record_path /tmp/recfile;  #錄製保存地址 
                    record_append on; #新流追加到老流中 
                    record_max_size 512000k; #錄製文件最大值
                }
                #============================
                #========錄製配置高配========
                #============================
                recorder vip {  #啓用錄製
                    record all manual;  #手動控制錄製啓停
                    record_suffix _record.mp4; # _record結尾.mp4格式
                    record_interval 1s; #每隔n秒寫一次 0是無間隔
                    record_path /tmp/recfile/vip;  #錄製保存地址 
                    record_append on; #新流追加到老流中 
                    record_max_size 3145728k; #錄製文件最大值
                }
        #========================
        #==錄製完畢文件轉爲MP4===
        #========================
        exec_record_done bash -c "ffmpeg -i $path -y -vcodec copy -acodec copy $dirname/$basename.mp4;rm -rf $path";
            #========================
            #========推拉認證========
            #========================
            #推
    	    on_publish http://xxx; 
	    #推完成
    	    on_publish_done http:/xxx;  
	    #拉
    	    on_play http://xxx;	  
    	    #拉完成
            on_play_done http://xxx;
            #推流白名單
    	    allow publish YouIp; 
    	    allow publish YouIp; 
    	    deny publish all; 
  	 }
        }    
    } 
http {
    include       mime.types;
    default_type  application/octet-stream;
  
    sendfile        on; 
    #保活timieout
    keepalive_timeout  65; 
    server {
        listen 80;
        server_name  localhost;  
                #HLS拉流其實是提供一個http來訪問*.ts文件目錄
		location /liveOnlineHLS {
                types {
                    #m3u8 type設置
                    application/vnd.apple.mpegurl m3u8;
                    #ts分片文件設置
                    video/mp2t ts;
                }  
		 #hls m3u8文件目錄
		 root /tmp;
		 add_header Cache-Control no-cache; 
        }  
        #通過這裏可以在推拉中進行監聽nginx-rtmp-module狀態
        location /liveStat { #監聽器
            #啓動流監聽/stat爲訪問domain
            #監聽保存到stat.xsl
            rtmp_stat all; 
            rtmp_stat_stylesheet stat.xsl;
        }
        location /stat.xsl { #狀態xsl 
            #指向nginx-rtmp-module目錄
            #該目錄中有stat.xsl及支持文件
            root /software/nginxrtmp/nginx-rtmp-module-master/;
        }
        #這個就是控制中心入口,基本上很多控制命令都要從這個control域來發出,比如錄製等
        location /control { #控制器
            rtmp_control all;
        }  
        #播放我們錄製的文件,錄製文件我保存到mp4了,flv ios是拉不了的
        location /vod/{  #hls點播地址
            alias /tmp/recfile/;
        }
    }
}
上面的配置我這個版本的project一直在用,後期還有很多要調整優化的,以後有空再分享
HLS拉流是要做訪問控制的恩,nginx-rtmp-module是沒有提供對HTTP HLS拉流的on_{event}事件的!
這裏要自己解決嘍



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章