Windows10環境下 Nginx+ffmpeg自搭伺服器製作RTMP直播流
Windows10環境下 Nginx+ffmpeg自搭伺服器製作RTMP直播流
學習筆記
所需條件:
-
nginx-rtmp-module(帶rtmp模組) ,連結:https://link.jianshu.com/?t=http%3A%2F%2Fnginx-win.ecsds.eu%2Fdownload%2Fnginx%201.7.11.3%20Gryphon.zip
-
ffmpeg,連結:https://pan.baidu.com/s/1XItEYzDjpGrkAUinBwQTUw
提取碼:o0fg -
VLC播放器,https://www.videolan.org/
安裝nginx:
1、下載後,直接解壓到自己想解壓的地方,然後進入conf資料夾,修改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; } 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 8888; 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; # } #} } rtmp_auto_push on; rtmp { server { listen 1935; application myapp { live on; } } } #主要上上面這兩段rtmp模組程式碼,在程式碼的最後加上一個rtmp模組
2、然後,啟動nginx,nginx命令:
啟動:strat nginx
關閉:nginx -s stop
重啟:nginx -s reload
注意:
修改完配置必須重啟
注意:
如果出現錯誤,說nginx.pid不存在,則要在logs目錄下建立nginx.pid,error.log不存在則建立error.log
3、啟動後,發現命令列並沒有什麼顯示,這時開啟localhost:8888,看到nginx的頁面就發現nginx已經啟動
注意:
埠號在nginx.conf的LISTEN配置
安裝ffmpeg:
1、解壓到自己喜歡的地方
2、新增電腦環境變數,這樣就可以全域性使用ffmpeg命令,進入環境變數PATH,進行編輯,加入如:D:/ffmpeg/bin,即可
3、測試是否成功,cmd命令列下輸入ffmpeg即可檢視
安裝VLC:
到官網下載即可(用於播放流直播)
進行推流:
進入一個具有測試視訊test.mp4的資料夾,在cmd命令列下進行推流,
ffmpeg -re -i test.mp4 -vcodec libx264 -acodec aac -f flv rtmp://localhost:1935/myapp/pc
解釋:
1、myapp為application的名字,由nginx的conf中定義
2、pc自定義,由推流方確定,組成一個地址rtmp://localhost:1935/myapp/pc
3、-vcodec libx264 代表264視訊編碼
4、-acodec aac -f flv 代表aac音訊編碼
檢視直播源:
在VLC的媒體/開啟網路串流/網路,輸入推流的地址:rtmp://localhost:1935/myapp/pc,就可以看到直播啦
這篇部落格主要用來記錄自己的筆記,還有給一些需要參考的人提供下,互相學習,如有不對或者不好的地方,大家可以提出批評和建議,o(∩_∩)o
最後感謝提供參考的部落格和博主:
Silence瀟湘夜雨
https://www.jianshu.com/p/59b558bac8a7