1. 程式人生 > >直播系列4-使用nginx+nginx-rtmp-module+ffmpeg搭建流媒體伺服器筆記(一)

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

第一部分

主要步驟及命令記錄:

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的意思)。

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

3、進入到nginx-1.7.9資料夾目錄下,執行如下命令:

  1. ./configure --prefix=/usr/local/nginx  --add-module=/home/user/nginx-rtmp-module  --with-http_ssl_module  
4、待上述命令執行完成之後,執行如下命令安裝:
  1. make  
5、make完成之後,執行:
  1. make install  
6、希望能順利完成以上步驟。安裝完成後,開啟nginx的配置檔案nginx.conf進行配置:在root前提下執行下面命令開啟nginx.conf
  1. gedit /usr/local/nginx/conf/nginx.conf  
7、nginx.conf中增加rtmp的支援,下面是修改過得檔案,以後可能還會修改更新:
  1. #user  nobody;  
  2. worker_processes  1;  
  3. #error_log  logs/error.log;  
  4. #error_log  logs/error.log  notice;  
  5. #error_log  logs/error.log  info;  
  6. #pid        logs/nginx.pid;  
  7. events {  
  8.     worker_connections  1024;  
  9. }  
  10.     rtmp {    
  11.         server {    
  12.             listen 1935;    
  13.             application myapp {    
  14.                 live on;    
  15.             }    
  16.             application hls {    #設定一個應用
  17.                 live on;    
  18.                 hls on;    
  19.                 hls_path /tmp/hls;    #檔案存放地址,這裡/tmp是和/usr評級的目錄
  20.             }    
  21.         }    
  22.     }    
  23. http {  
  24.     include       mime.types;  
  25.     default_type  application/octet-stream;  
  26.     #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '  
  27.     #                  '$status $body_bytes_sent "$http_referer" '  
  28.     #                  '"$http_user_agent" "$http_x_forwarded_for"';  
  29.     #access_log  logs/access.log  main;  
  30.     sendfile        on;  
  31.     #tcp_nopush     on;  
  32.     #keepalive_timeout  0;  
  33.     keepalive_timeout  65;  
  34.     #gzip  on;  
  35.     server {  
  36.         listen       80;  
  37.         server_name  localhost;  
  38.         #charset koi8-r;  
  39.         #access_log  logs/host.access.log  main;  
  40.         location / {  
  41.             root   html;  
  42.             index  index.html index.htm;  
  43.         }  
  44.         #error_page  404              /404.html;  
  45.         # redirect server error pages to the static page /50x.html  
  46.         #  
  47.         error_page   500 502 503 504  /50x.html;  
  48.         location = /50x.html {  
  49.             root   html;  
  50.         }  
  51.         # proxy the PHP scripts to Apache listening on 127.0.0.1:80  
  52.         #  
  53.         #location ~ \.php$ {  
  54.         #    proxy_pass   http://127.0.0.1;  
  55.         #}  
  56.         # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000  
  57.         #  
  58.         #location ~ \.php$ {  
  59.         #    root           html;  
  60.         #    fastcgi_pass   127.0.0.1:9000;  
  61.         #    fastcgi_index  index.php;  
  62.         #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;  
  63.         #    include        fastcgi_params;  
  64.         #}  
  65.         # deny access to .htaccess files, if Apache's document root  
  66.         # concurs with nginx's one  
  67.         #  
  68.         #location ~ /\.ht {  
  69.         #    deny  all;  
  70.         #}  
  71.     }  
  72.     # another virtual host using mix of IP-, name-, and port-based configuration  
  73.     #  
  74.     #server {  
  75.     #    listen       8000;  
  76.     #    listen       somename:8080;  
  77.     #    server_name  somename  alias  another.alias;  
  78.     #    location / {  
  79.     #        root   html;  
  80.     #        index  index.html index.htm;  
  81.     #    }  
  82.     #}  
  83.     # HTTPS server  
  84.     #  
  85.     #server {  
  86.     #    listen       443 ssl;  
  87.     #    server_name  localhost;  
  88.     #    ssl_certificate      cert.pem;  
  89.     #    ssl_certificate_key  cert.key;  
  90.     #    ssl_session_cache    shared:SSL:1m;  
  91.     #    ssl_session_timeout  5m;  
  92.     #    ssl_ciphers  HIGH:!aNULL:!MD5;  
  93.     #    ssl_prefer_server_ciphers  on;  
  94.     #    location / {  
  95.     #        root   html;  
  96.     #        index  index.html index.htm;  
  97.     #    }  
  98.     #}  
  99. }  
8、儲存完配置檔案之後,啟動nginx服務
  1. /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf  
9、啟動時可能會遇到端口占用的問題,
  1. nginx: [emerg] bind() to 0.0.0.0:1935 failed (98: Address already in use)  
  2. nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)  
  3. nginx: [emerg] bind() to 0.0.0.0:1935 failed (98: Address already in use)  
  4. nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)  
  5. nginx: [emerg] bind() to 0.0.0.0:1935 failed (98: Address already in use)  
  6. nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)  
  7. nginx: [emerg] bind() to 0.0.0.0:1935 failed (98: Address already in use)  
  8. nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)  
  9. nginx: [emerg] bind() to 0.0.0.0:1935 failed (98: Address already in use)  
  10. nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)  
  11. nginx: [emerg] still could not bind()  
上述的顯示是因為我的nginx服務已經在運行了,所以再啟動時1935和80埠會佔用,可以使用下面命令來終止服務:
  1. kill -9 nginx  
當遇到不是因為服務已經啟動造成的端口占用時可以通過下面命令來檢視是哪個執行緒佔用的埠,然後殺死他(例如埠號1935):
  1. netstat -tlnp|grep 1935  
然後系統會顯示:
  1. tcp        0      0 0.0.0.0:1935            0.0.0.0:*               LISTEN      20211/nginx.conf  
注意到這個20211就是執行緒號,後面跟著的是服務名字,也就是說nginx佔用著1935埠,可以使用下面命令殺死他:
  1. kill -9 20211  
10、然後再啟動服務,當服務啟動成功的話,在本機瀏覽器輸入:http://localhost/
  1. Welcome to nginx!  
  2. If you see this page, the nginx web server is successfully installed and working. Further configuration is required.  
  3. For online documentation and support please refer to nginx.org.  
  4. Commercial support is available at nginx.com.  
  5. Thank you for using nginx.  
如果出現上述內容,則服務成功啟動。

11、可以通過netstat -ltn 命令可以看到埠的監聽情況.

  1. 啟用Internet連線 (僅伺服器)  
  2. Proto Recv-Q Send-Q Local Address           Foreign Address         State        
  3. tcp        0      0 0.0.0.0:1935            0.0.0.0:*               LISTEN       
  4. tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN       
  5. tcp        0      0 127.0.1.1:53            0.0.0.0:*               LISTEN       
  6. tcp        0      0 127.0.0.1:631           0.0.0.0:*               LISTEN       
  7. tcp6       0      0 ::1:631                 :::*                    LISTEN       
80是nginx預設的http監聽埠。

12、用ffmpeg推本地視訊頭流到nginx,myapp上,用如下命令:

  1. ffmpeg -f dshow -i video="Integrated Webcam" -s 640x360 -vcodec libx264 -b:v 1000k   -ab 128k -f flv rtmp://192.168.31.102:1935/myapp/test1

在VLC上開啟網路串流 ,填寫

rtmp://192.168.31.102:1935/myapp/test1
可以看到監控畫面,有大約2~3秒的延時。

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