官方源碼:https://github.com/arut/nginx-rtmp-module
戰(zhàn)斗民族俄羅斯人民開發(fā)的一款NGINX的流媒體插件,除了直播發(fā)布音視頻流之外具備流媒體服務(wù)器的常見功能
比如推拉流媒體資源
基于HTTP的FLV/MP4 VOD點(diǎn)播
HLS (HTTP Live Streaming) M3U8的支持
基于http的操作(發(fā)布贮喧、播放掷邦、錄制)
可以很好的協(xié)同現(xiàn)有的流媒體服務(wù)器以及播放器一起工作
在線調(diào)用ffmpeg對流媒體進(jìn)行轉(zhuǎn)碼
H264/AAC音視頻編碼格式的支持
linux/BSD/MAC系統(tǒng)的支持
編譯方法:
在上一篇中,已經(jīng)成功部署了nginx服務(wù)器,但是沒有集成推流的功能.
1\下載該模塊
2\進(jìn)入nginx-1.10.1目錄,重新配置configure
3\增加參數(shù)--add-module=/path/to/nginx-rtmp-module
sudo ./configure --prefix=/usr/local/nginx --with-pcre=/home/wangxiong/Soft/nginx_station/pcre-8.37 --with-zlib=/home/wangxiong/Soft/nginx_station/zlib-1.2.8 --with-openssl=/home/wangxiong/Soft/nginx_station/openssl-1.0.1t --with-http_ssl_module --add-module=/home/wangxiong/Soft/nginx_station/nginx-rtmp-module
4\sudo make
5\sudo make install
安裝完成后nginx目錄:
服務(wù)器配置nginx.conf所在路徑
可以用gedit或者vim打開>
#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 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;
# }
#}
}
默認(rèn)是支持http協(xié)議,然后增加對rtmp協(xié)議,在http{}外面增加,詳細(xì)配置可看官方wiki: https://github.com/arut/nginx-rtmp-module/wiki/Directives
rtmp {
server {
listen 1935;
application myapp {
live on;
}
application hls {
live on;
hls on;
hls_path /tmp/hls;
}
}
}
配置好后重啟nginx;
在本地可以用ffmpeg命令進(jìn)行推流,然后用ffplayer進(jìn)行播放
如果用ffmpeg命令出現(xiàn).so共享庫找不到的問題,解決方法配置全局共享
vim /etc/ld.so.conf
在后面添加一行/usr/local/ffmpeg/lib
按Esc->wq保存退出
sudo ldconfig,生效
./ffmpeg -re -i /home/wangxiong/Documents/VID_20160508_164615.mp4 -vcodec libx264 -vprofile baseline -acodec aac -ar 44100 -strict -2 -ac 1 -f flv -s 1280x720 -q 10 rtmp://localhost:1935/myapp/testav
./ffmpeg -re -i /home/wangxiong/Documents/VID_20160508_164615.mp4 -c copy -f flv rtmp://localhost:1935/myapp/testav
推流結(jié)果
為了方便,直接用雷博士的android推流客戶端,github上克隆或下載simplest_ffmpeg_android_streamer!
接下來,查看本地nginx服務(wù)器的ip地址,這個就是安卓客戶端要填寫的流服務(wù)器地址.
我的ip地址是192.168.1.116
1\用Eclipse導(dǎo)入項(xiàng)目工程,配置好NDK自動編譯.
2\打開MainActivity,填寫輸入文件路徑,和流服務(wù)器地址,當(dāng)然我只是做了判斷,如果EditText不填寫,我就給一個默認(rèn)的值.
String folderurl = Environment.getExternalStorageDirectory()
.getPath();
String urltext_input = urlEdittext_input.getText().toString();
if (TextUtils.isEmpty(urltext_input)) {
urltext_input = "sintel.mp4";
}
String inputurl = folderurl + "/" + urltext_input;
String outputurl = urlEdittext_output.getText().toString();
String info = "";
if (TextUtils.isEmpty(outputurl)) {
outputurl = "rtmp://192.168.1.116:1935/myapp/livestream";
// 192.168.1.116
}
stream(inputurl, outputurl); //native方法
Log.e("inputurl", inputurl);
Log.e("outputurl", outputurl);
運(yùn)行build程序,點(diǎn)擊 Start 按鈕,開始將SD卡下的視頻推到服務(wù)器.
注意,因?yàn)橹辈?我這里是在安卓上點(diǎn)擊推流,然后在ubuntu打開瀏覽器訪問rtmp流.
ps:安裝VLC播放器:sudo apt-get install vlc 一條命令搞定.
然后選擇播放應(yīng)用程序,我已經(jīng)安裝了VLC播放器,就用它了.
直播開始啦,只要手機(jī)一直有視頻推上來,播放器可以一直播放!!!