天天看點

使用nginx+nginx-rtmp-module+ffmpeg搭建流媒體伺服器筆記(一)

第一部分

主要步驟及指令記錄:

1、下載下傳nginx,位址http://nginx.org/en/download.html,選擇最新版本下載下傳、解壓。目前最新版為:nginx-1.7.9。

2、為了增加對rtmp的支援,下載下傳nginx-rtmp-module,位址:https://github.com/arut/nginx-rtmp-module#example-nginxconf,這個是個開源項目。解壓後,為了和我在網上看到的教程同步,我改了檔案夾名字,将其改成了nginx-rtmp-module。然後将其放到/home/user/目錄下(今天才知道h和user非常像的目錄usr是Unix System Resource的意思)。

cp -rf 123/nginx-rtmp-module/ /home/user/
           

3、進入到nginx-1.7.9檔案夾目錄下,執行如下指令:

./configure --prefix=/usr/local/nginx  --add-module=/home/user/nginx-rtmp-module  --with-http_ssl_module
           

4、待上述指令執行完成之後,執行如下指令安裝:

make
           

5、make完成之後,執行:

make install
           

6、希望能順利完成以上步驟。安裝完成後,打開nginx的配置檔案nginx.conf進行配置:在root前提下執行下面指令打開nginx.conf

gedit /usr/local/nginx/conf/nginx.conf
           

7、nginx.conf中增加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;  
      
            application myapp {  
                live on;  
            }  
            application hls {  
                live on;  
                hls on;  
                hls_path /tmp/hls;  
            }  
        }  
    }  


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

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

}
           

8、儲存完配置檔案之後,啟動nginx服務

/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
           

9、啟動時可能會遇到端口占用的問題,

nginx: [emerg] bind() to 0.0.0.0:1935 failed (98: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:1935 failed (98: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:1935 failed (98: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:1935 failed (98: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:1935 failed (98: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] still could not bind()
           

上述的顯示是因為我的nginx服務已經在運作了,是以再啟動時1935和80端口會占用,可以使用下面指令來終止服務:

pkill -9 nginx
           

當遇到不是因為服務已經啟動造成的端口占用時可以通過下面指令來檢視是哪個線程占用的端口,然後殺死他(例如端口号1935):

<pre name="code" class="plain">netstat -tlnp|grep 1935
           

然後系統會顯示:

tcp        0      0 0.0.0.0:1935            0.0.0.0:*               LISTEN      20211/nginx.conf
           

注意到這個20211就是線程号,後面跟着的是服務名字,也就是說nginx占用着1935端口,可以使用下面指令殺死他:

kill -9 20211
           

10、然後再啟動服務,當服務啟動成功的話,在本機浏覽器輸入:http://localhost/

Welcome to nginx!

If you see this page, the nginx web server is successfully installed and working. Further configuration is required.

For online documentation and support please refer to nginx.org.
Commercial support is available at nginx.com.

Thank you for using nginx.
           

如果出現上述内容,則服務成功啟動。

11、可以通過netstat -ltn 指令可以看到端口的監聽情況.

激活Internet連接配接 (僅伺服器)
Proto Recv-Q Send-Q Local Address           Foreign Address         State      
tcp        0      0 0.0.0.0:1935            0.0.0.0:*               LISTEN     
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN     
tcp        0      0 127.0.1.1:53            0.0.0.0:*               LISTEN     
tcp        0      0 127.0.0.1:631           0.0.0.0:*               LISTEN     
tcp6       0      0 ::1:631                 :::*                    LISTEN     
           

80是nginx預設的http監聽端口。

12、用ffmpeg推流到nginx,myapp上,我推送的是網絡錄影機的rtsp直播視訊流。用如下指令:

ffmpeg -i rtsp://admin:[email protected] -acodec aac -strict experimental -ar 44100 -ac 2 -b:a 96k -r 25 -b:v 500k -s 640*480 -f flv rtmp://172.27.35.2:1935/myapp/test1
           

上述指令參照 http://www.tuicool.com/articles/eAfIVv具體參數還有待修改。

在VLC上打開網絡串流 ,填寫 rtmp://172.27.35.2:1935/myapp/test1可以看到監控畫面,有大約2~3秒的延時。

使用nginx+nginx-rtmp-module+ffmpeg搭建流媒體伺服器筆記(一)

明天繼續,将流推送到hls上……

繼續閱讀