搭建rtmp服務器,實現拉rtmp流;hls流;

1、首先需要裝nginx和nginx-rtmp-module;不會可以Google。

2、實現rtmp拉流

2-1、需要一個查看rtmp服務器的推拉流狀態的stat;

這個下載的nginx-rtmp-module裏面不一定有stat.xsl文件,沒有的話可以在nginx.conf設置到你指定的文件夾下,

例如:

//http{ }內
location /stat {   
            rtmp_stat all;
            rtmp_stat_stylesheet stat.xsl;
        }
        location /stat.xsl { 
            root /Users/leeli/Desktop/serverFile/; #一定要填對
        }

這樣訪問http://localhost:8080/stat,纔有界面

2-2、 設置nginx.conf文件,配置rtmp推流

// rtmp{ }內
application vod {
       play /Users/leeli/Desktop/serverFile/; #//視頻文件存放位置,自定義
   }
   application live { #第一處添加的直播字段
       live on;
       #allow publish 127.0.0.1;
       allow play all;
   }

那麼 在obs就有一個live的推流分支,推流:rtmp://192.168.31.12/live; (live名字可以隨意修改; qe爲obs上設置的串流祕鑰)

用VLC就可以直接拉rtmp://192.168.31.12/live/qe拉下來,不過要加一個qe,

 

3、配置nginx.conf文件,實現hls拉流;

參考 https://www.cnblogs.com/ebay/p/9968239.html

https://blog.csdn.net/superyu1992/article/details/81204539

// rtmp{}內
application hls {
            live on;
            hls on;
            hls_path /Users/leeli/Desktop/serverFile/;#視頻流存放地址
            hls_fragment 5s;
            hls_playlist_length 15s;
            hls_continuous on; #連續模式。
            hls_cleanup on;    #對多餘的切片進行刪除。
            hls_nested on;     #嵌套模式。
        }


//http{}內
location /hls {  #添加視頻流存放地址。
        types {
            application/vnd.apple.mpegurl m3u8;
            video/mp2t ts;
        }
        #訪問權限開啓,否則訪問這個地址會報403
        autoindex on;
        alias /Users/leeli/Desktop/serverFile/;#視頻流存放地址,與上面的hls_path相對應,這裏root和alias的區別可自行百度
        expires -1;
        add_header Cache-Control no-cache;
        #防止跨域問題
        add_header 'Access-Control-Allow-Origin' '*';
        add_header 'Access-Control-Allow-Credentials' 'true';
        add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
        add_header 'Access-Control-Allow-Headers' 'DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type';  
    }

推流還是用obs,還是rtmp,地址爲:rtmp://192.168.31.12:1935/hls; 記得串流祕鑰,拉流用得着。

拉流可以是Safari,直接輸入http://192.168.31.12:8080/hls/qe/index.m3u8;端口8080,區別1935;

下圖爲http://192.168.31.12/hls/qe,nginx會自動生成index.m3u8和ts,放在

server在local的文件:

 

4、最後貼上nginx.conf文件;

查找nginx.conf文件位置,cmd終端輸入:nginx -t;會顯示地址在local位置


nginx: the configuration file /usr/local/etc/nginx/nginx.conf syntax is ok
nginx: configuration file /usr/local/etc/nginx/nginx.conf test is successful

#user  nobody;
worker_processes  1;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;


events {
    worker_connections  1024;
}
######################ADD RTMP################
rtmp {                #RTMP服務
   server {
       listen 1935;  #//服務端口
   chunk_size 4096;   #//數據傳輸塊的大小


   application vod {
       play /Users/leeli/Desktop/serverFile/; #//視頻文件存放位置,自定義
   }
   application live { #第一處添加的直播字段
       live on;
       #allow publish 127.0.0.1;
       allow play all;
   }
   application push{
        live on; #開啓直播
        push rtmp://192.168.31.74:1935/live;
#//<自己公網ip,沒有可以填localhost本地玩一下>/live; #推流到上面的直播應用
    }

   application hls {
            live on;
            hls on;
            hls_path /Users/leeli/Desktop/serverFile/;#視頻流存放地址
            hls_fragment 5s;
            hls_playlist_length 15s;
            hls_continuous on; #連續模式。
            hls_cleanup on;    #對多餘的切片進行刪除。
            hls_nested on;     #嵌套模式。
        }

   }
}

#####################ADD RTMP################




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

    #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
    #                  '$status $body_bytes_sent "$http_referer" '
    #                  '"$http_user_agent" "$http_x_forwarded_for"';

    #access_log  logs/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;

    server {
        listen       8080;
        server_name  localhost;
#################################################
        location /stat {   
            rtmp_stat all;
            rtmp_stat_stylesheet stat.xsl;
        }
        location /stat.xsl { #第二處添加的location字段。
            root /Users/leeli/Desktop/serverFile/; #一定要填對
        }

	location /hls {  #添加視頻流存放地址。
        types {
            application/vnd.apple.mpegurl m3u8;
            video/mp2t ts;
        }
        #訪問權限開啓,否則訪問這個地址會報403
        autoindex on;
        alias /Users/leeli/Desktop/serverFile/;#視頻流存放地址,與上面的hls_path相對應,這裏root和alias的區別可自行百度
        expires -1;
        add_header Cache-Control no-cache;
        #防止跨域問題
        add_header 'Access-Control-Allow-Origin' '*';
        add_header 'Access-Control-Allow-Credentials' 'true';
        add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
        add_header 'Access-Control-Allow-Headers' 'DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type';  
    }

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            root   /Users/leeli/Desktop/serverFile;
	    autoindex_exact_size off;
	    autoindex_localtime on;
            index  index.html index.htm;
        }
##################################################
        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        #location ~ \.php$ {
        #    proxy_pass   http://127.0.0.1;
        #}

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        #location ~ \.php$ {
        #    root           html;
        #    fastcgi_pass   127.0.0.1:9000;
        #    fastcgi_index  index.php;
        #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
        #    include        fastcgi_params;
        #}

        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\.ht {
        #    deny  all;
        #}
    }


    # another virtual host using mix of IP-, name-, and port-based configuration
    #
    #server {
    #    listen       8000;
    #    listen       somename:8080;
    #    server_name  somename  alias  another.alias;

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


    # HTTPS server
    #
    #server {
    #    listen       443 ssl;
    #    server_name  localhost;

    #    ssl_certificate      cert.pem;
    #    ssl_certificate_key  cert.key;

    #    ssl_session_cache    shared:SSL:1m;
    #    ssl_session_timeout  5m;

    #    ssl_ciphers  HIGH:!aNULL:!MD5;
    #    ssl_prefer_server_ciphers  on;

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

}

 

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