nginx+ffmpge+rtmp實現媒體流的直播
阿新 • • 發佈:2019-02-02
首先大概說一下這幾個東西使用的流程。
這幾個的原理就是,先用ffmpeg把媒體資料即視訊或音訊從感測器那裡獲取到,然後按照用於輸入引數的要求壓縮或編碼成一串資料流,再把這一串資料流通過rtmp模組發給nginx,讓他當做html資料傳送給請求的客戶,誰讓他是html伺服器呢。可見,rtmp只有在ffmpge和nginx之間,而nginx與使用者之間不是rtmp協議,而是http協議。
首先下載nginx原始碼和nginx-rtmp-module的原始碼,放在同一個目錄下面,因為編譯nginx時需要使用nginx-rtmp-module的原始碼。
nginx的安裝參考這裡
nginx原始碼下載連結地址 官網下載連結地址
nginx-rtmp-module的原始碼下載頁面 這裡寫連結內容
提醒一下,如果提示找不到OpenSSL的library,而你又能在系統中找到openssl,那多半是因為沒有安裝openssl-devel,使用:
sudo yum install openssl-devel
來安裝。我本來是Fedora23中已經安裝過了,後來在Centos7.0中安裝時發現openssl已經有了,為什麼還是找不到,就是因為缺少了openssl-devel。CentOS環境安裝openssl-devel
注意,配置nginx的時候需要新增狀態統計模組,這樣方便檢視客戶端連線情況:
./configure --prefix=$PWD/installed --add-module=/home/bluez/work/nginx-rtmp-module --with-http_ssl_module --with-debug --with-http_stub_status_module
ffmpeg的安裝我好想已經記錄過了。這裡
接下顯示配置nginx的配置檔案。
#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 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;
# }
#}
#rtmp stat
#location /stat {
# rtmp_stat all;
#rtmp_stat_stylesheet stat.xsl;
# }
#location /stat.xsl {
# root /home/zhangwei/work/60G/my_tools/nginx-rtmp-modules;
# }
location /ngx_status {
stub_status on;
access_log off;
}
}
rtmp{
server {
listen 1935;
#chunk_size 4096;
application myapp {
live on;
}
application htl {
live on;
hls on;
hls_path /tmp/hls;
}
}
}
真正有用的是最後新增的那個rtmp的資料塊。
先執行nginx,我的nginx儲存在的它根目錄的installed/sbin/nginx中
./installed/sbin/nginx -c /home/zhangz/work/60G/my_tools/nginx-1.13.0/installed/conf/nginx.conf
然後執行
ffmpeg -f alsa -i default:CARD=Device -ar 44100 -ac 2 -acodec aac -strict -2 -f flv rtmp://127.0.0.1:1935/myapp/test1
注意,客戶端不要使用vlc測試,使用vlc會出現卡頓的情況,我從app store上下了一個mplayer,測試正常沒有卡頓。