1. 程式人生 > >基於Nginx+nginx-rtmp-module+ffmpeg搭建rtmp、hls流媒體伺服器

基於Nginx+nginx-rtmp-module+ffmpeg搭建rtmp、hls流媒體伺服器

Nginx及nginx-rtmp-module安裝

新建目錄
mkdir /usr/local/mginx
下載
cd /usr/local/nginx
wget http://nginx.org/download/nginx-1.12.2.tar.gz
wget https://codeload.github.com/arut/nginx-rtmp-module/zip/master
解壓
tar -zxvf nginx-1.12.2.tar.gz
unzip nginx-rtmp-module-master.zip 
安裝

nginx的一些模組依賴一些lib庫,在安裝nginx之前,須先安裝這些lib庫,

依賴庫主要有g++、gcc、openssl-devel、pcre-devel和zlib-devel 視情況執行如下命令安裝

yum install gcc-c++ 
yum install pcre pcre-devel  
yum install zlib zlib-devel 
yum install openssl openssl--devel 
nginx-rtmp-module模組安裝
cd /usr/local/nginx/nginx-1.12.2
#完成rtmp模組安裝
./configure --add-module=/usr/local/nginx/nginx-rtmp-module-master
make
make install

至此nginx及相關模組安裝完畢

配置及啟動服務支援rtmp

修改配置
cd /usr/local/nginx/conf
vim nginx.conf
#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;
}


rtmp {
    server {
	listen 1935;
	chunk_size 4096;

	application vod {
		play /usr/local/nginx/html/hls;
     	}

    	application live {
                live on; 
        }
    }
}


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       80;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            root   html;
            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;
        }
	
	#fastdfs 將 /group1/M00 對映到 /usr/local/fastdfs/file/data
	location /group1/M00 {
	    alias /usr/local/fastdfs/file/data;
	}


        # 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;
    #    }
    #}

}
服務管理
cd /usr/local/nginx/sbin
nginx #啟動服務
nginx -s stop #管理服務
直播


利用ffmpeg進行推流直播

ffmpeg -re -i "E:\ffmpeg-20180227-fa0c9d6-win64-static\bin\1234.mp4" -acodec copy -vcodec copy -f flv -an  rtmp://192.168.174.200:1935/live/a 

利用vlc測試,地址:rtmp://192.168.174.200:1935/live/a


點播

將本地mp4檔案複製到/usr/local/nginx/html/hls

利用vlc測試:rtmp://192.168.174.200:1935/vod/1234.mp4

配置支援hls

工作流程
點播:準備好視訊檔案MP4,使用ffmpeg將其轉成TS格式檔案,然後繼續使用ffmpeg將其切片並生成播放列表M3U8檔案,將所得TS分片與M3U8檔案放到nginx伺服器目錄下,開啟http伺服器後,同一區域網內用手機輸入網路地址可檢視點播視訊。 
直播:使用VLC軟體將視訊以rtsp流的形式發出,使用ffmpeg接收這個rtsp流並實時切割到http伺服器下,開啟伺服器後,同一區域網內用手機輸入網路地址可檢視直播視訊。
修改配置檔案
cd /usr/local/nginx/conf
vim mime.types

#在 application/zip zip; 這一行後面增加2行: 
application/x-mpegURL m3u8; 
application/vnd.apple.mpegurl m3u8;

vim video/x-msvideo
#avi;行後,增加1行:
video/MP2T ts;
點播

使用ffmpeg將mp4格式檔案轉成TS格式檔案

ffmpeg -y -i 1234.mp4 -vcodec copy -acodec copy -vbsf h264_mp4toannexb test.ts

使用ffmpeg將TS檔案切成ts分片併產生m3u8檔案

ffmpeg -i test.ts -c copy -map 0 -f segment -segment_list playlist.m3u8 -segment_time 2 test%03d.ts

將所得的ts分片和m3u8檔案放到nginx\html目錄下

vlc輸入[ http://192.168.174.200:80/playlist.m3u8 ]即可看到點播視訊 

注:192.168.174.200是本機的區域網ip,也即http伺服器的ip

直播
將視訊以rtsp流的形式從vlc上發出來(具體方法請自查) 

使用ffmpeg接收rtsp流並自動實時切片到http伺服器下

ffmpeg -i rtsp://admin:[email protected]/cam/realmonitor?channel=1&subtype=1 -c copy -vcodec h264 -f hls -hls_time 2.0 -hls_list_size 5 -hls_wrap 5 /usr/local/nginx/html/test.m3u8

vlc輸入[ http://172.19.12.240/test.m3u8 ]即可看到直播視訊

錄製

通過ffmpeg向伺服器推流,在伺服器進行視訊錄製

ffmpeg -re -i "E:\ffmpeg-20180227-fa0c9d6-win64-static\bin\1234.mp4" -acodec copy -vcodec copy -f flv -an  rtmp://192.168.174.200:1935/live/a
http://192.168.174.200/hls/a/index.m3u8

修改nginx.conf配置

rtmp {
    server {
	listen 1935;
	chunk_size 4096;

	application vod {
		play /usr/local/nginx/html/hls;
     	}

    	application live {
                live on;
	        hls on; #這個引數把直播伺服器改造成實時回放伺服器。
                wait_key on; #對視訊切片進行保護,這樣就不會產生馬賽克了。
                hls_path /usr/local/nginx/html/hls; #切片視訊檔案存放位置。
                hls_fragment 10s;     #每個視訊切片的時長。
                hls_playlist_length 60s;  #總共可以回看的事件,這裡設定的是1分鐘。
                hls_continuous on; #連續模式。
                hls_cleanup on;    #對多餘的切片進行刪除。
                hls_nested on;     #巢狀模式。
        }
    }
}

檢視已經產生切片視訊檔案了。其中還有一個index.m3u8。 

cd /usr/local/nginx/html/hls/a/
ls

vlc輸入[ http://192.168.174.200/hls/a/index.m3u8 ]即可看到視訊