前言:不廢話上實操。
步驟一:安裝Homebrow
Homebrew
簡稱brew
,是Mac OSX上的軟件包管理工具,能在Mac中方便的安裝軟件或者卸載軟件,可以說Homebrew
就是mac下的apt-get
丑瞧、yum
神器。
查看本地是否安裝過
$ brew
安裝brew
$ /usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
卸載brew
$ /usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/uninstall)"
安裝不成功自己查一下資料吧蜀肘,這里不過多演示绊汹。
步驟二:安裝nginx服務器
插看是否安裝過nginx
$ brew search nginx
安裝nginx
$ brew tap denji/homebrew-nginx
$ brew install nginx-full --with-rtmp-module
注意??: --with-rtmp-module
,一定要加上rtmp模塊
扮宠,不然添加rtmp服務時就會報錯誤:unknown directive "rtmp" in /usr/local/etc/nginx/nginx.conf:135
西乖。
解決上述問題,只能卸載掉nginx-full
坛增,然后重裝nginx-full
$ brew uninstall nginx-full // 卸載nginx-full
$ brew install nginx-full --with-rtmp-module // 安裝nginx-full
步驟三:修改nginx配置文件
打開文件/usr/local/etc/nginx/nginx.conf
$ open /usr/local/etc/nginx/nginx.conf
使用Xcode/文件編輯器打開nginx.conf
修改配置文件:
1.找到http下的server添加HLS配置
http {
server {
#HLS配置開始获雕,這個配置為了‘客戶端‘能夠以http協(xié)議獲取HLS的拉流
#HLS配置開始,這個配置為了‘客戶端‘能夠以http協(xié)議獲取HLS的拉流
location /hls{
# Serve HLS fragments
types {
application/vnd.apple.mpegurl m3u8;
video/mp2t ts;
}
root /usr/local/var/www;
add_header Cache-Control no-cache;
}
#HLS配置結(jié)束
}
}
2.文件拖拽到最后收捣,在沒有{}的地方
添加rtmp配置
# 在http節(jié)點后面加上rtmp配置:
rtmp {
server {
listen 1935;
ping 30s;
notify_method get;
application zbcs {
live on;
record off;
}
application live {
live on;
record off;
max_connections 1024;
}
#增加對HLS支持開始
application hls {
live on;
hls on;
hls_path /usr/local/var/www/hls;
hls_fragment 5s;
}
#增加對HLS支持結(jié)束
}
}
修改后的整體內(nèi)容如下:
#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;
}
#HLS配置開始届案,這個配置為了‘客戶端‘能夠以http協(xié)議獲取HLS的拉流
location /hls{
# Serve HLS fragments
types {
application/vnd.apple.mpegurl m3u8;
video/mp2t ts;
}
root /usr/local/var/www;
add_header Cache-Control no-cache;
}
#HLS配置結(jié)束
#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;
# }
#}
include servers/*;
}
# 在http節(jié)點后面加上rtmp配置:
rtmp {
server {
listen 1935;
ping 30s;
notify_method get;
application zbcs {
live on;
record off;
}
application live {
live on;
record off;
max_connections 1024;
}
#增加對HLS支持開始
application hls {
live on;
hls on;
hls_path /usr/local/var/www/hls;
hls_fragment 5s;
}
#增加對HLS支持結(jié)束
}
}
說明:
live on
開啟實時
hls on
開啟hls
hls_path
ts文件存放路徑
hls_fragment 5s
每個TS文件包含5秒的視頻內(nèi)容
讓配置文件生效:
方法一:(親測有坑,有時候不生效)
$ nginx -s reload // 親測有坑
方法二:
查看啟動的nginx
$ ps -ef|grep nginx
找到master process
的進程端口號是549
停掉這個master process
進程
$ kill -QUIT 549
讓配置文件生效
$ nginx -t -c /usr/local/etc/nginx/nginx.conf
然后就可以啟動服務了罢艾。
$ nginx
在瀏覽器里打開http://localhost:8080
如果看到如下頁面楣颠,說明配置成功了!
想要停止服務咐蚯,命令:(stop是強制退出童漩,quit是執(zhí)行完任務后退出)
$ nginx -s quit
// 或者
$ nginx -s stop
第四步:ffmpeg
安裝ffmpeg
$ brew install ffmpeg
查看安裝成功了沒
$ ffmpeg
安裝失敗自己找辦法。
RTMP的方式推拉流
測試推流
$ ffmpeg -re -i /Users/xxx/Desktop/sample_iPod.mp4 -vcodec h264 -acodec aac -strict -2 -f flv rtmp://localhost:1935/live/room
測試拉流
開啟推流后仓蛆,用VLC播放器播放下面直播地址視頻:
$ rtmp://localhost:1935/live/room
注意??: 要在推流完成之前playU龆?娲骸看疙!因為這是直播流豆拨,推完就沒啦!D芮臁施禾!
這樣一個簡單的本地直播服務就搭建好了!
對于視頻直播服務搁胆,如果需要支持多路流輸入
的話弥搞,很簡單,在Nginx配置文件里多配幾個Application就可以了渠旁,像下面這樣:
HLS的方式推拉流
測試推流
$ ffmpeg -re -i /Users/xxx/Desktop/sample_iPod.mp4 -vcodec h264 -acodec aac -strict -2 -f flv rtmp://localhost:1935/hls/demo
此時在/usr/local/var/www/hls
目錄下會產(chǎn)生流媒體文件
測試推流
打開Safari瀏覽器輸入
http://ip:8080/hls/demo.m3u8
HLS直播延時
我們知道hls協(xié)議是將直播流分成一段一段的小段視頻去下載播放的攀例,所以假設列表里面的包含5個ts文件,每個TS文件包含5秒的視頻內(nèi)容顾腊,那么整體的延遲就是25秒粤铭。因為當你看到這些視頻時,主播已經(jīng)將視頻錄制好上傳上去了杂靶,所以時這樣產(chǎn)生的延遲梆惯。當然可以縮短列表的長度和單個ts文件的大小來降低延遲,極致來說可以縮減列表長度為1吗垮,并且ts的時長為1s垛吗,但是這樣會造成請求次數(shù)增加,增大服務器壓力烁登,當網(wǎng)速慢時回造成更多的緩沖怯屉,所以蘋果官方推薦的ts時長時10s,所以這樣就會大改有30s的延遲饵沧。參考資料:
https://developer.apple.com/library/ios/documentation/NetworkingInternet/Conceptual/StreamingMediaGuide/FrequentlyAskedQuestions/FrequentlyAskedQuestions.html