1. 程式人生 > >Nginx+ffmpeg+ckplayer海康監控攝像頭在web頁面播放RTSP轉RTMP

Nginx+ffmpeg+ckplayer海康監控攝像頭在web頁面播放RTSP轉RTMP

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

        # TV mode: one publisher, many subscribers
        application mylive {

            # enable live streaming
            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       8080;
        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;
        }

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

}

第四步啟動伺服器,測試

1、啟動:

C:\nginx-1.10.2>start nginx-rtmp

或

C:\nginx-1.10.2>nginx-rtmp.exe

注:建議使用第一種,第二種會使你的cmd視窗一直處於執行中,不能進行其他命令操作。

2、停止:

C:\nginx-1.10.2>nginx-rtmp.exe -s stop

或

C:\nginx-1.10.2>nginx-rtmp.exe -s quit

注:stop是快速停止nginx,可能並不儲存相關資訊;quit是完整有序的停止nginx,並儲存相關資訊。

3、重新載入Nginx:

C:\nginx-1.10.2>nginx-rtmp.exe -s reload

當配置資訊修改,需要重新載入這些配置時使用此命令。
1:執行cmd命令,啟動nginx-rtmp.exe服務

(start nginx-rtmp.exe:啟動nginx服務,然後看程序中是否有nginx-rtmp.exe)

 2.開啟網頁輸入:localhost或者127.0.0.1本機ip加埠8080

(顯示一下介面說明啟動成功)


第五步FFMpeg推流

1:海康的攝像頭提供rtsp流地址,格式如下 rtsp://admin:[email protected]:554/Streaming/channels/101,想了解這個rtsp的格式是怎麼拼的,網上有很多資料,不過得找最新的格式,我就被舊的格式給整了半天。3:進入cmd,如圖所示進入到你解壓對應的位置。4:執行命令:
ffmpeg -re  -rtsp_transport tcp -i "rtsp://admin:[email protected]:554/Streaming/channels/101" -f flv -vcodec libx264 -vprofile baseline -acodec aac -ar 44100 -strict -2 -ac 1 -f flv -s 1280x720 -q 10 "rtmp://192.168.1.84:1935/mylive/test2"解釋:-re  -rtsp_transport tcp 這個引數必須要加不然會推流失敗,就是這個引數害得我多弄了半天。
rtmp://192.168.1.84:1935/mylive/test2 這個就是剛才部署的nginx了 ,mylive是在conf配置檔案裡面配置的,可以修改,test2可以隨便修改。執行成功如圖:這個時候就可以訪問rtmp流了。5:常用引數說明:主要引數:i 設定輸入流 f 設定輸出格式 ss 開始時間 視訊引數: 

b 設定視訊流量,預設為200Kbit/s-r 設定幀速率,預設為25 
s 設定畫面的寬與高-aspect 設定畫面的比例 
vn 不處理視訊-vcodec 設定視訊編解碼器,未設定時則使用與輸入流相同的編解碼器 
音訊引數: 
ar 設定取樣率 
ac 設定聲音的Channel數 
acodec 設定聲音編解碼器,未設定時則使用與輸入流相同的編解碼器an 不處理音訊

第六步用ckplayer播放

這個可以去官網下載一個最新版的ckplayer程式,按照他們提供的方式來做就行了,這裡我提供一個我做測試用的程式包,下載之後解壓,修改index.html中的rtmp地址就行。如果是用的iis部署,部署成功之後在瀏覽器上輸入:http://localhost:埠/Index1.html就可以播放了。

總結

總算是弄懂這套流程了,在這個過程找了各種資料也越到各種麻煩所幸都解決。最後還有一個遺留問題就是網路問題,你的攝像頭是在區域網內的話是很順利,如果是訪問公網攝像頭的rtsp在用ffmpeg推的時候會莫名的停掉,又只能繼續執行命令進行推流,這個問題還有待解決。